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:
This is my main.yml in the tasks directory
Hi Hernan,
Your variable is actually a data structure of a list of dictionary:
admin:
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:
Into:
[
](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.
Are you setting herlit_semaphore? If not, how is it being declared?
Walter
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