Re: Ansible dict date to csv file

325 views
Skip to first unread message
Message has been deleted

Sagan

unread,
Sep 10, 2022, 12:44:02 PM9/10/22
to Ansible Project
IF you have your closters has dicts in your variable you can use a loop with dict2items and loop around it, as to writing it in your file you can use this module

On Saturday, September 10, 2022 at 5:47:07 PM UTC+2 ahar...@gmail.com wrote:
Hello All,

I'm looking for help on below guery. As  bit new to Anisble I could not find solution thought I sent lot of time..

Thank you in advance.

I have data getting through set_fact/ json_query using  loop  for multiple clusters as below example.
I have saved date to variable: Clusters
 
It would be like this when I read variable saved data.

Clusters:
- node: node1
  state: up

- node: nod2
   state: down

-node: node3
  state: up

and so on....

How do I get this date to csv file like below?

Clusters,192.168.1.1,node1,up
Clusters,192.168.1.2,node2,down
Clusters,192.168.1.3,node3,up

Stefan Hornburg (Racke)

unread,
Sep 10, 2022, 12:49:49 PM9/10/22
to ansible...@googlegroups.com
On 10/09/2022 18:44, Sagan wrote:
> IF you have your closters has dicts in your variable you can use a loop with dict2items and loop around it, as to writing it in your file you can use this module
> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html

Using lineinfile doesn't make sense when your write the whole file. In this case the template module is the better choice.

Regards

             Racke


>
> On Saturday, September 10, 2022 at 5:47:07 PM UTC+2 ahar...@gmail.com wrote:
>
> Hello All,
>
> I'm looking for help on below guery. As  bit new to Anisble I could not find solution thought I sent lot of time..
>
> Thank you in advance.
>
> I have data getting through set_fact/ *json_query* using *loop*  for multiple clusters as below example.
> I have saved date to /*variable*/: *Clusters*
> It would be like this when I read variable saved data.
>
> *Clusters*:
> 192.168.1.1 <http://192.168.1.1>:
> - node: node1
>   state: up
>
> 192.168.1.2 <http://192.168.1.2>:
> - node: nod2
>    state: down
>
> 192.168.1.3 <http://192.168.1.3>:
> -node: node3
>   state: up
>
> and so on....
>
> How do I get this date to csv file like below?
>
> Clusters,192.168.1.1,node1,up
> Clusters,192.168.1.2,node2,down
> Clusters,192.168.1.3,node3,up
>
> --
> 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/24e2bebe-0759-4512-9347-d993a0b10f87n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/24e2bebe-0759-4512-9347-d993a0b10f87n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

Vladimir Botka

unread,
Sep 10, 2022, 2:04:40 PM9/10/22
to Aharonu, ansible...@googlegroups.com
Create the list. Put the below declarations into *vars*

csv_query: '[].[`Clusters`, key, value[].node, value[].state]'
csv: "{{ Clusters|dict2items|json_query(csv_query)|
map('flatten')|map('join', ',')|list }}"

gives

csv:
- Clusters,192.168.1.1,node1,up
- Clusters,192.168.1.2,node2,down
- Clusters,192.168.1.3,node3,up

Write the file

- copy:
dest: /tmp/Clusters.csv
content: "{{ csv|join('\n') }}"

gives

shell> cat /tmp/Clusters.csv
Clusters,192.168.1.1,node1,up
Clusters,192.168.1.2,node2,down
Clusters,192.168.1.3,node3,up

--
Vladimir Botka
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages