Adding mutiple groups from var file

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

`

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
   include_vars:
      file: our_groups.yml
   group:
        name: "{{ item.name }}"
        state: present
        gid: "{{ item.gid }}"
   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
   include_vars:
      file: our_groups.yml

- group:
        name: "{{ item.name }}"
        state: present
        gid: "{{ item.gid }}"
   with_items: "{{ our_groups }}"

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:~]$
`

`

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.

They are there:

ansible/roles/linux/tasks/main.yml
ansible/roles/linux/vars/our_groups.yml

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.

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

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.