<p>There's a lot that you can do, but I'm not sure with the existing filters you easily augment a dict.</p>

<p>Creating a <a href="https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#filter-plugins" rel="nofollow noreferrer">new filter</a> to do it was easier.</p>

<pre><code>$ cat filter_plugins/addhomedir.py
class FilterModule:
    @staticmethod
    def add_home(_val):
        return [dict(user, home='/home/{0}/'.format(user['name'])) for user in
                _val if 'sudo' not in user]

    def filters(self):
        return {
            'add_homedir': self.add_home
        }
$ cat test1.yml
---
- hosts: localhost
  vars:
     users:
         - name: alice
           sudo: yes
         - name: bob
         - name: charlie
  tasks:
      - debug:
          msg: "{{ users | add_homedir | list }}"
</code></pre>

<p>You can use set_facts and loop to get the values you want, but the question becomes what format and use of the data is really needed.  I'm not sure creating a new structure is the Right Way (TM).  It could be easier to just calculate the individual value for each item in the list when it is used.</p>

<pre><code>$ cat test2.yml
---
- hosts: localhost
  vars:
      users:
        - name: alice
          sudo: yes
        - name: bob
        - name: charlie
  tasks:
     - user:
         name: "{{ item['name'] }}"
         create_home: true
         home: "{{ '/home/{}/'.format(item['name'] }}"
       when: "'sudo' not in item"
       loop: "{{ users }}"
</code></pre>

CC BY-SA 4.0
2020-05-03T00:16:14.040Z
2020-05-03T00:16:14.040Z
Arcege
<p>Given a list of users:</p>

<pre><code># Python Syntax
users = [{'name': 'alice', 'sudo': True}, {'name': 'bob'}, {'name': 'charlie'}]

# YAML Syntax
users:
  - name: alice
    sudo: yes
  - name: bob
  - name: charlie
</code></pre>

<p>How would I do the following in an ansible playbook:</p>

<pre><code># Python Syntax
[ dict(user, home="/home/{}/".format(user['name'])) for user in users if not 'sudo' in user]

# more verbose
for user in users:
  if not 'sudo' in user:
    user['home'] = '/home/' + user['name'] + '/'
</code></pre>

<p>resulting in:</p>

<pre><code># Python Syntax
[{'name': 'bob', 'home': '/home/bob/'}, {'name': 'charlie', 'home': '/home/charlie/'}]


# YAML Syntax
users:
  - name: bob
    home: /home/bob/
  - name: charlie
    home: /home/charlie/
</code></pre>

<p>Is this even possible/intended use of playbook language?</p>

CC BY-SA 4.0
2020-05-01T19:11:18.650Z
Did you check [with_items](https://docs.ansible.com/ansible/latest/plugins/lookup/items.html)?
030
CC BY-SA 4.0
2020-05-02T07:26:20.140Z
My Intention is to save the result in a variable for later use. Is that possible with with_items?
RincewindWizzard
CC BY-SA 4.0
2020-05-02T11:48:03.193Z
Could you give it a try? Otherwise you could check [register](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables). With this it is possible to run a script and store the output in a variable. In later steps one could grep this output. [Example](https://github.com/030/ansible-velero/blob/master/tasks/main.yml).
030
<ansible>
Doing map and filter of a list of dictionaries in ansible
RincewindWizzard
# Doing map and filter of a list of dictionaries in ansible

(Asked by: username_0 on 2020-05-01)

Given a list of users:
# Python Syntax
users = [{'name': 'alice', 'sudo': True}, {'name': 'bob'}, {'name': 'charlie'}]

# YAML Syntax
users:
  - name: alice
    sudo: yes
  - name: bob
  - name: charlie

How would I do the following in an ansible playbook:
# Python Syntax
[ dict(user, home="/home/{}/".format(user['name'])) for user in users if not 'sudo' in user]

# more verbose
for user in users:
  if not 'sudo' in user:
    user['home'] = '/home/' + user['name'] + '/'

resulting in:
# Python Syntax
[{'name': 'bob', 'home': '/home/bob/'}, {'name': 'charlie', 'home': '/home/charlie/'}]


# YAML Syntax
users:
  - name: bob
    home: /home/bob/
  - name: charlie
    home: /home/charlie/

Is this even possible/intended use of playbook language?

**Tags:** ansible

> **Comments:**
> **username_1**: Did you check [with_items](https://docs.ansible.com/ansible/latest/plugins/lookup/items.html)?  
> **username_0**: My Intention is to save the result in a variable for later use. Is that possible with with_items?  
> **username_1**: Could you give it a try? Otherwise you could check [register](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables). With this it is possible to run a script and store the output in a variable. In later steps one could grep this output. [Example](https://github.com/username_1/ansible-velero/blob/master/tasks/main.yml).  

---

## 1 answers

### Answer by username_2 on 2020-05-03. Score: 4

There's a lot that you can do, but I'm not sure with the existing filters you easily augment a dict.
Creating a new filter to do it was easier.
$ cat filter_plugins/addhomedir.py
class FilterModule:
    @staticmethod
    def add_home(_val):
        return [dict(user, home='/home/{0}/'.format(user['name'])) for user in
                _val if 'sudo' not in user]

    def filters(self):
        return {
            'add_homedir': self.add_home
        }
$ cat test1.yml
---
- hosts: localhost
  vars:
     users:
         - name: alice
           sudo: yes
         - name: bob
         - name: charlie
  tasks:
      - debug:
          msg: "{{ users | add_homedir | list }}"

You can use set_facts and loop to get the values you want, but the question becomes what format and use of the data is really needed.  I'm not sure creating a new structure is the Right Way (TM).  It could be easier to just calculate the individual value for each item in the list when it is used.
$ cat test2.yml
---
- hosts: localhost
  vars:
      users:
        - name: alice
          sudo: yes
        - name: bob
        - name: charlie
  tasks:
     - user:
         name: "{{ item['name'] }}"
         create_home: true
         home: "{{ '/home/{}/'.format(item['name'] }}"
       when: "'sudo' not in item"
       loop: "{{ users }}"


---

