Ansible playbook undefined variable

137 views
Skip to first unread message

Hernan Sal

unread,
Aug 9, 2023, 4:14:46 PM8/9/23
to Ansible Project
Hi,
I'm running an ansible playbook called create_admin_user.yml and I'm getting
this error

fatal: [172.31.3.117]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'herlit_semaphore' is undefined. 'herlit_semaphore' is undefined\n\nThe error appears to be in '/etc/ansible/roles/create_admin_user/tasks/main.yml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name:  semaphore\n  ^ here\n"}

This is my create_admin_user.yaml file in  the playbooks/users directory
- hosts: all
  gather_facts: yes
  become: yes
  become_user: root
  tasks:
    - ansible.builtin.import_role:
        name: create_admin_user

This is my main.yml in the vars directory
admin:
  - name: 'herlit_semaphore'
    comment: 'Ultimate User'
    uid: '1000'

  - name: 'semaphore'
    comment: 'Semaphore App User'
    uid: '1001'

This is my main.yml in the tasks directory
---
# tasks file for create_admin_user

- name: Add the user with a specific uid and a primary group of "admin"
  ansible.builtin.user:
    name: "{{ herlit_semaphore.name }}"
    comment: "{{ herlit_semaphore.comment }}"
    uid: "{{ herlit_semaphore.uid }}"
    state: present
    group: admin
    append: yes

- name: Add the user "semaphore" with a specific uid and a primary group of "admin"
  ansible.builtin.user:
    name: "{{ semaphore.name }}"
    comment: "{{ semaphore.comment }}"
    uid: "{{ semaphore.uid }}"
    state: present
    group: admin
    append: yes

Sorry for the long email. Any ideas about this error?
THanks
Hernan

Rilindo Foster

unread,
Aug 9, 2023, 4:24:37 PM8/9/23
to ansible...@googlegroups.com
Hi Hernan,

Your variable is actually a data structure of a list of dictionary:

admin:
  - name: 'herlit_semaphore'
    comment: 'Ultimate User'
    uid: '1000'

  - name: 'semaphore'
    comment: 'Semaphore App User'
    uid: '1001'



If you don’t intend to do loop, you simply reference the item directly. For example, for “herlit_semaphore” user, you do:

- name: Add the user with a specific uid and a primary group of "admin"
  ansible.builtin.user:
    name: "{{ admin[0].name }}"
    comment: "{{ admin[0].comment }}"
    uid: "{{ admin[0].uid }}"

    state: present
    group: admin
    append: yes

Where [0] is the first element in the list that contains that user. For the second user, it is going to be:


- name: Add the user "semaphore" with a specific uid and a primary group of "admin"
  ansible.builtin.user:
    name: "{{ admin[1].name }}"
    comment: "{{ admin[1].comment }}"
    uid: "{{ admin[1].uid }}"

    state: present
    group: admin
    append: yes


Where [1] is the second element in the list and th one that contains the “semaphore” user.

You can confirm the placement by pasting:

admin:
  - name: 'herlit_semaphore'
    comment: 'Ultimate User'
    uid: '1000'

  - name: 'semaphore'
    comment: 'Semaphore App User'
    uid: ‘1001'

Into:



And click on “YAML Viewer” to browse around the list.

Note that unless there is a good reason, it would be better to loop around that data structure, as it mean much less code.

- Rilindo


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/269931b4-2003-41cf-8321-7e04ac730488n%40googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Aug 10, 2023, 8:03:18 AM8/10/23
to 'Rowe, Walter P. (Fed)' via Ansible Project
Are you setting herlit_semaphore? If not, how is it being declared?

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Hernan Sal

unread,
Aug 10, 2023, 10:00:22 AM8/10/23
to Ansible Project
Thanks a lot Rilindo. It worked like a charm.I keep forgetting that is all python.
Thank you Walter for your input as well.
Best
Hernan

Reply all
Reply to author
Forward
0 new messages