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:


https://jsonformatter.org/yaml-viewer
Best YAML Viewer Online
jsonformatter.org

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


> On Aug 9, 2023, at 3:05 PM, Hernan Sal <[email protected]> wrote:
> 
> 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
> 
> -- 
> 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 [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/269931b4-2003-41cf-8321-7e04ac730488n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/ansible-project/269931b4-2003-41cf-8321-7e04ac730488n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/711AE5BA-19B9-4C8D-9CB2-1B69D2FD636A%40gmail.com.

Reply via email to