How to register a dynamically named variable in an Ansible task

ansible

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

Best Answer

I suppose there's no easy way for that. And register with with_items loop just puts all results of them into an array variable.results. Try the following tasks:

  tasks:
    - name: Determine GIDs
      shell: "getent group {{ item }} | cut -d : -f 3"
      register: gids
      changed_when: false
      with_items:
        - syslog
        - utmp
    - debug:
        var: gids
    - assert:
        that:
          - item.rc == 0
      with_items: gids.results
    - set_fact:
        gid_syslog: "{{gids.results[0]}}"
        gid_utmp: "{{gids.results[1]}}"
    - debug:
        msg: "{{gid_syslog.stdout}} {{gid_utmp.stdout}}"

You cannot either use variable expansion in set_fact keys like this:

    - set_fact:
        "gid_{{item.item}}": "{{item}}"
      with_items: gids.results
Related Question