Correct way to create dynamic lists in Ansible
<p>I'm looking for advice. I have the following code that creates a list dynamically that I can then later use in a template. </p>

<p>This is a copy of the test code I put together - for the actual role I just added the admins|regex_replace variable into the j2 template.</p>

<pre><code>    ---

  - hosts: localhost
    gather_facts: false

    vars:
      # define empty admins var first so ansible doesn't complain
      admins:

      admin_accounts:
      - name: john
        uid: 1000
        group: sysadmin
        shell: /bin/bash
        comment: "Unix Administrator"
      - name: paul
        uid: 1001
        group: sysadmin
        shell: /bin/bash
        comment: "Unix Administrator"
      - name: george
        uid: 1002
        group: sysadmin
        shell: /bin/bash
        comment: "Unix Administrator"
      - name: ringo
        uid: 1003
        group: sysadmin
        shell: /bin/bash
        comment: "Unix Administrator"

    tasks:

      - name: build array of admin user names
        set_fact: admins="{{ admins}} {{ item.name }}"
        with_items: "{{ admin_accounts }}"

      # print out the fact piping through two jinja2 filters
      # careful with word wrapping
      - debug: msg={{ admins | regex_replace( '\s+',', ' ) | regex_replace`(',\s(.*)','\\1') }}`
</code></pre>

<p>This gives me the following:</p>

<pre><code>    PLAY [localhost] ***************************************************************

TASK [build array of admin user names] *****************************************
ok: [localhost] =&gt; (item={u'comment': u'Unix Administrator', u'shell': u'/bin/bash', u'group': u'sysadmin', u'name': u'john', u'uid': 1000})
ok: [localhost] =&gt; (item={u'comment': u'Unix Administrator', u'shell': u'/bin/bash', u'group': u'sysadmin', u'name': u'paul', u'uid': 1001})
ok: [localhost] =&gt; (item={u'comment': u'Unix Administrator', u'shell': u'/bin/bash', u'group': u'sysadmin', u'name': u'george', u'uid': 1002})
ok: [localhost] =&gt; (item={u'comment': u'Unix Administrator', u'shell': u'/bin/bash', u'group': u'sysadmin', u'name': u'ringo', u'uid': 1003})

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    "msg": "john, paul, george, ringo"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
</code></pre>

<p>So...I get what I need, but am I going about it the right way?</p>

<p>Ansible version is 2.0.2.0 running on Centos 7.2.</p>

<p>Thanks in advance.</p>

<hr>

<p>Edit: The resultant filter ended up looking like this:</p>

<pre><code>  - name: build list of admin user names
    set_fact:
      admin_list: "{{ admin_accounts | selectattr('state', 'equalto', 'present') | map(attribute='name') | join(', ') }}"
  - debug: msg={{ admin_list }}
</code></pre>

<p>Having added another parameter to the yaml:</p>

<pre><code>state: absent
</code></pre>

<p>Ringo was left out, as desired.</p>