Filtering_data manipulation

56 views
Skip to first unread message

Veera

unread,
Jul 31, 2023, 1:11:16 PM7/31/23
to Ansible Project
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 . 









Todd Lewis

unread,
Jul 31, 2023, 2:30:58 PM7/31/23
to Ansible Project
What does your "result" look like?

Veera

unread,
Jul 31, 2023, 2:43:20 PM7/31/23
to Ansible Project
it looks like 

'servera',  'serverb', 'serverc', 'serverd'


and I want result to be like ..
$cat /tmp/my_inv
servera
serverb
serverc
serverd

Todd Lewis

unread,
Jul 31, 2023, 2:47:12 PM7/31/23
to Ansible Project
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.

Veera

unread,
Jul 31, 2023, 3:09:56 PM7/31/23
to Ansible Project
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"]

Abhijeet Kasurde

unread,
Jul 31, 2023, 3:41:39 PM7/31/23
to ansible...@googlegroups.com
Use template module

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

server.j2
```
{% for server in servers %}
{{ server }}
{% endfor %}
```

# cat output.txt
servera
serverb
serverc


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a7a2ddf1-1da3-4576-96fe-252da18d8394n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Todd Lewis

unread,
Jul 31, 2023, 4:04:10 PM7/31/23
to ansible...@googlegroups.com, uto...@gmail.com
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: |
          {% 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. :)

Felix Fontein

unread,
Aug 1, 2023, 1:34:35 AM8/1/23
to ansible...@googlegroups.com
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: |
> {% 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. :)

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

Veera

unread,
Aug 7, 2023, 8:28:29 AM8/7/23
to Ansible Project
Thanks all ..
I am able to get the desired output with templates ..

Reply all
Reply to author
Forward
0 new messages