# Filtering\_data manipulation

**URL:** <https://forum.ansible.com/t/filtering-data-manipulation/38283>\
**Category:** Ansible Project\
**Created:** [July 31, 2023, 5:11pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283 "2023-07-31T17:11:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [July 31, 2023, 5:11pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/1 "2023-07-31T17:11:16Z")

</div>

While collecting the inventory from a dynamic source , I am able to filter the hostname and print them as below as expected.

servera  
serverb  
serverc  
serverd

when set to facts with set\_fact also fine and print in the order one below the other.  
However when I try to write into a file with copy module ,

- name: Write to the file  
ansible.builtin.copy:  
content: “{{ result[‘json’][‘value’] |map(attribute=‘hostname’) }}”  
dest: “{{my\_inv}}”

the format collapses as below.

‘servera’, ‘serverb’, ‘serverc’, ‘serverd’

How can I write the output in the list format(line by line) as this file will be used to read as an inventory for other tasks .

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 31, 2023, 6:30pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/2 "2023-07-31T18:30:58Z")

</div>

What does your “result” look like?

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [July 31, 2023, 6:43pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/3 "2023-07-31T18:43:20Z")

</div>

it looks like  
‘servera’, ‘serverb’, ‘serverc’, ‘serverd’

and I want result to be like …  
$cat /tmp/my\_inv  
servera  
serverb  
serverc  
serverd

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 31, 2023, 6:47pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/4 "2023-07-31T18:47:12Z")

</div>

I mean, what does the output of

- name: Dump result  
ansible.builtin.debug:  
msg: “{{ result }}”

look like? It presumably has a ‘json’ field? Which itself contains a ‘value’?  
Without a reasonable approximation of your input, I can’t possibly suggest how to create expected output from it.

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [July 31, 2023, 7:09pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/5 "2023-07-31T19:09:56Z")

</div>

Here it is

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“servera”,  
“serverb”,  
“serverc”,  
“serverd”,  
“centos8-latest”  
]  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “The List of VMs is [‘servera’, ‘serverb’, ‘serverc’, ‘serverd’, ‘centos8-latest’]”  
}

TASK [write the server list to the file /tmp/output.txt] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=9 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

$ cat /tmp/output.txt  
[“servera”, “serverb”, “serverc”, “serverd”, “centos8-latest”]

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 31, 2023, 7:41pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/6 "2023-07-31T19:41:11Z")

</div>

Use template module

```auto

---
- hosts: localhost
gather_facts: no
vars:
servers:
- servera
- serverb
- serverc
tasks:
- template:
src: server.j2
dest: output.txt

```

server.j2

```auto
{% for server in servers %}
{{ server }}
{% endfor %}

```

# cat output.txt

servera  
serverb  
serverc

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 31, 2023, 8:03pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/7 "2023-07-31T20:03:54Z")

</div>

Yes. Or if you feel like ignoring the warnings\* about using variables in copy’s “content”:

```auto
    - name: Write to the file 
      ansible.builtin.copy:
        content: |
          {% for server in servers %}
          {{ server }}
          {% endfor %}
        dest: "{{my_inv}}" 

```

\* I do it quite often, looking for cases where the promised unexpected surprises happen, but I haven’t found them yet. It’ll probably happen some day when it least convenient. 🙂

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 1, 2023, 5:34am UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/8 "2023-08-01T05:34:14Z")

</div>

Hi,

> Yes. Or if you feel like ignoring the warnings^\* about using  
> variables in copy's "content":
> 
> ```
> \- name: Write to the file
> ansible\.builtin\.copy:
> content: |
> 
> ```
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for server in servers %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ server }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
> dest: "{{my\_inv}}"
> 
> ^\* I do it quite often, looking for cases where the promised  
> unexpected surprises happen, but I haven't found them yet. It'll  
> probably happen some day when it least convenient. 🙂

I got bitten by this somewhen in the past when the input for `content`  
turned out to be valid JSON. Ansible parsed it and converted it to the  
Python representation of the dictionary, which wasn't valid JSON  
anymore (`"` converted to `'`).

But that's something rather special, for many simple templates such as  
yours it's no problem at all (and I also use `content` for that).

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 7, 2023, 12:28pm UTC](https://forum.ansible.com/t/filtering-data-manipulation/38283/9 "2023-08-07T12:28:29Z")

</div>

Thanks all …  
I am able to get the desired output with templates …
