Adding mutiple groups from var file

23 views
Skip to first unread message

John McCoy

unread,
Nov 15, 2018, 6:05:45 PM11/15/18
to Ansible Project
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
---
- name: Linux VM setup
  hosts
: centos7-x64-template
  remote_user
: root

  tasks
:

 
- name: adding groups
    include_vars
:
      file
: our_groups.yml
     
group:
        name
: "{{ item.name }}"
        state
: present
        gid
: "{{ item.gid }}"
      with_items
: "{{ our_groups }}"

...

roles/linux/vars/our_groups.yml
--
our_groups
:
 
- name: devops
    gid
: 710

 
- name: developers1
    gid
: 711

 
- name: developers2
    gid
: 712

 
- name: developers3
    gid
: 713

...

Thanks
John

Brian Coca

unread,
Nov 15, 2018, 6:13:32 PM11/15/18
to ansible...@googlegroups.com
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 }}"



--
----------
Brian Coca

John McCoy

unread,
Nov 15, 2018, 7:09:39 PM11/15/18
to Ansible Project
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:~]$


---
- name: Linux VM setup
  hosts
: centos7-x64-template
  remote_user
: root

  tasks
:


 
- name: adding groups
    include_vars
:

      file
: our_groups.yml

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

...






Brian Coca

unread,
Nov 15, 2018, 7:15:41 PM11/15/18
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

John McCoy, Jr

unread,
Nov 15, 2018, 7:20:59 PM11/15/18
to ansible...@googlegroups.com, Brian Coca
They are there:

 ansible/roles/linux/tasks/main.yml
 ansible/roles/linux/vars/our_groups.yml
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Brian Coca

unread,
Nov 15, 2018, 7:24:22 PM11/15/18
to johnm...@gmail.com, ansible...@googlegroups.com
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.

--
----------
Brian Coca

John McCoy, Jr

unread,
Nov 15, 2018, 7:46:17 PM11/15/18
to Brian Coca, ansible...@googlegroups.com
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

On 11/15/2018 4:23 PM, Brian Coca wrote:
> 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.
>

Brian Coca

unread,
Nov 15, 2018, 7:53:48 PM11/15/18
to johnm...@gmail.com, ansible...@googlegroups.com
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.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages