# Ansible playbook undefined variable

**URL:** <https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316>\
**Category:** Ansible Project\
**Created:** [August 9, 2023, 8:05pm UTC](https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316 "2023-08-09T20:05:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hernan\_Sal](https://avatars.discourse-cdn.com/v4/letter/h/5e9695/32.png) [@Hernan\_Sal](https://forum.ansible.com/u/Hernan_Sal)\
**Post date:** [August 9, 2023, 8:05pm UTC](https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316/1 "2023-08-09T20:05:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [August 9, 2023, 8:24pm UTC](https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316/2 "2023-08-09T20:24:12Z")

</div>

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:

[

| 

 ![yaml-viewer.png](https://us1.discourse-cdn.com/ansible/original/2X/8/89987439feaa82bffad7edc36d023c31d7b23eb6.webp)

 |

| 

[Best YAML Viewer Online](https://jsonformatter.org/yaml-viewer)  
[jsonformatter.org](https://jsonformatter.org/yaml-viewer)

 |

](https://jsonformatter.org/yaml-viewer)

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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 10, 2023, 12:02pm UTC](https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316/3 "2023-08-10T12:02:55Z")

</div>

Are you setting herlit\_semaphore? If not, how is it being declared?

Walter

---

<div class="post-metadata">

**Author:** ![Hernan\_Sal](https://avatars.discourse-cdn.com/v4/letter/h/5e9695/32.png) [@Hernan\_Sal](https://forum.ansible.com/u/Hernan_Sal)\
**Post date:** [August 10, 2023, 1:15pm UTC](https://forum.ansible.com/t/ansible-playbook-undefined-variable/38316/4 "2023-08-10T13:15:28Z")

</div>

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
