# Adding mutiple groups from var file

**URL:** <https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040>\
**Category:** Ansible Project\
**Created:** [November 15, 2018, 11:05pm UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040 "2018-11-15T23:05:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McCoy](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_McCoy](https://forum.ansible.com/u/John_McCoy)\
**Post date:** [November 15, 2018, 11:05pm UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/1 "2018-11-15T23:05:45Z")

</div>

Hey All

I’m new to Ansible so I may be trying to do things completely wrong… Ideally I’d like “adding groups” to be a stand alone task, so I can call it with the group file name from other tasks but lets crawl before we run.

I need to add a large number of groups to certain machines (users too, but groups come first). The GID is already defined so I need to pass that along with the group.

I get this error when I try with my code below, I’ve tried replacing “item” with “our\_groups” but get the same error just a change it what is undefined.

`[unix@ansible01:~]$ ansible-playbook -i ansible/inventories/hosts ansible/roles/linux/tasks/main.yml <SNIP> TASK [adding groups] ********************************************************************************************************************************************** fatal: [centos7-x64-template]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/automation/unix/ansible/roles/linux/tasks/main.yml': line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: adding groups\n ^ here\n"}`

roles/linux/tasks/main.yml

`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 15, 2018, 11:13pm UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/2 "2018-11-15T23:13:10Z")

</div>

Several things look wrong, first, your indentation is off, with\_items  
is a 'task keyword' but you indented it as part of the module's  
options:

- name: adding groups  
&nbsp;&nbsp;&nbsp;include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: our\_groups.yml  
&nbsp;&nbsp;&nbsp;group:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gid: "{{ item.gid }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ our\_groups }}"

The second thing is that you seem to have 2 actions in the same action  
... you need each action separate:

- name: adding groups  
&nbsp;&nbsp;&nbsp;include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: our\_groups.yml

- group:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gid: "{{ item.gid }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ our\_groups }}"

---

<div class="post-metadata">

**Author:** ![John\_McCoy](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_McCoy](https://forum.ansible.com/u/John_McCoy)\
**Post date:** [November 16, 2018, 12:09am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/3 "2018-11-16T00:09:39Z")

</div>

Thanks for the help, adjusted as suggested.

The formatting is tripping me up for sure, was hoping Atom would do a better job at keeping me aligned there, but it’s a bit more, you have an error fix it at this point.

I get a different error now when I run it, one I fought with before, the file exists for sure:

`  
[unix@ansible01:~]$ ansible-playbook -i ansible/inventories/hosts ansible/roles/linux/tasks/main.yml

PLAY [Linux VM setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [centos7-x64-template]

TASK [adding groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [centos7-x64-template]: FAILED! =\> {“ansible\_facts”: {}, “ansible\_included\_var\_files”: , “changed”: false, “message”: “Could not find or access ‘our\_groups.yml’\nSearched in:\n\t/automation/unix/ansible/roles/linux/tasks/vars/our\_groups.yml\n\t/automation/unix/ansible/roles/linux/tasks/our\_groups.yml\n\t/automation/unix/ansible/roles/linux/tasks/vars/our\_groups.yml\n\t/automation/unix/ansible/roles/linux/tasks/our\_groups.yml on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”}  
to retry, use: --limit @/automation/unix/ansible/roles/linux/tasks/main.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
centos7-x64-template : ok=1 changed=0 unreachable=0 failed=1  
`

`  
[unix@ansible01:~]$ find . -name our\_groups.yml -ls  
1575401 4 -rw-r–r-- 1 unix operations 154 Nov 15 15:58 ./ansible/roles/linux/vars/our\_groups.yml

[unix@ansible01:~]$ find /automation -name our\_groups.yml -ls  
1575401 4 -rw-r–r-- 1 unix operations 154 Nov 15 15:58 /automation/unix/ansible/roles/linux/vars/our\_groups.yml  
[unix@ansible01:~]$  
`

`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2018, 12:15am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/4 "2018-11-16T00:15:19Z")

</div>

I expect the error, you are referring to the file that is not in the  
expected paths, it should either be in a vars/ directory adjacent to  
the play you are executing or you should 'include\_vars' from within  
the 'linux' role.

Ansible will not search all your roles for files, specially in roles  
that are not referenced in the play.

---

<div class="post-metadata">

**Author:** ![John\_McCoy](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_McCoy](https://forum.ansible.com/u/John_McCoy)\
**Post date:** [November 16, 2018, 12:20am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/5 "2018-11-16T00:20:46Z")

</div>

They are there:

ansible/roles/linux/tasks/main.yml  
ansible/roles/linux/vars/our\_groups.yml

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2018, 12:23am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/6 "2018-11-16T00:23:57Z")

</div>

I see where they are, but how does ansible know to search for them  
there? it is not in any of the directories ansible expects.

---

<div class="post-metadata">

**Author:** ![John\_McCoy](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_McCoy](https://forum.ansible.com/u/John_McCoy)\
**Post date:** [November 16, 2018, 12:46am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/7 "2018-11-16T00:46:03Z")

</div>

OK, I moved the our\_groups.yml file to the tasks directory not vars and it worked like a charm.

What would have been the proper way to list the file if I want to keep it in the vars/ directory?

```
 include\_vars:
   file: vars/our\_groups\.yml

```

Thank you very much!

John

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2018, 12:53am UTC](https://forum.ansible.com/t/adding-mutiple-groups-from-var-file/29040/8 "2018-11-16T00:53:26Z")

</div>

you can keep it in a vars dir adjacent to your play, or when the  
'linux' role you can keep it where it was, you cannot do what you were  
doing wich was keep it in a role and use it from an unrelated play.
