I'm trying to retrieve the group ID of two groups (syslog
and utmp
) by name using an Ansible task. For testing purposes I have created a playbook to retrieve the information from the Ansible host itself.
---
- name: My playbook
hosts: enabled
sudo: True
connection: local
gather_facts: False
tasks:
- name: Determine GIDs
shell: "getent group {{ item }} | cut -d : -f 3"
register: gid_{{item}}
failed_when: gid_{{item}}.rc != 0
changed_when: false
with_items:
- syslog
- utmp
Unfortunately I get the following error when running the playbook:
fatal: [hostname] => error while evaluating conditional: gid_syslog.rc != 0
How can I consolidate a task like this one into a parametrized form while registering separate variables, one per item, for later use? So the goal is to have variables based on the group name which can then be used in later tasks.
I'm using the int
filter on gid_syslog.stdout
and gid_utmp.stdout
to do some calculation based on the GID in later tasks.
I also tried using gid.{{item}}
and gid[item]
instead of gid_{{item}}
to no avail.
The following works fine in contrast to the above:
---
- name: My playbook
hosts: enabled
sudo: True
connection: local
gather_facts: False
tasks:
- name: Determine syslog GID
shell: "getent group syslog | cut -d : -f 3"
register: gid_syslog
failed_when: gid_syslog.rc != 0
changed_when: false
- name: Determine utmp GID
shell: "getent group utmp | cut -d : -f 3"
register: gid_utmp
failed_when: gid_utmp.rc != 0
changed_when: false