How to get a list of dict with a cat from command module

36 views
Skip to first unread message

Hiero-nymo

unread,
Mar 22, 2023, 10:02:33 AM3/22/23
to Ansible Project
Hi everyone,

I'm using the cat command with the command module to retrieve remote files content. With a loop within a find module I get filename and call a task, so like this:
[...]
- name: Read the content of all files
  command: /usr/bin/cat {{ _files_folder.path }}/{{ file_item }}.yaml
  register: file_contents
  become: true
  become_user: root
[...]
I receive a list of string (with stdout_lines) or a normal output (with stdout) but I need to have a list because a the end I want to make a diff with another list of dict, like this:
ok: [10.10.10.1] => {
    "daten_content": [
        {
            "agents": [
                "aaa",
                "bbb"
            ],
            "community": {
                "from xyzzy"
            },
            "disableDimensions": true,
            "Dimensions": {
                "HostGroup": "abc",
                "Type": "role",
                "env": "TEST"
            },
            "fields": [
[...]
Unfortunately I cannot retrieve what I need with a split or to pipe in list.
As anyone an idea how to do that or using another module?

Dick Visser

unread,
Mar 22, 2023, 10:18:01 AM3/22/23
to ansible...@googlegroups.com
On Wed, 22 Mar 2023 at 15:02, Hiero-nymo <jer.m...@gmail.com> wrote:
Hi everyone,

I'm using the cat command with the command module to retrieve remote files content. With a loop within a find module I get filename and call a task, so like this:
[...]
- name: Read the content of all files
  command: /usr/bin/cat {{ _files_folder.path }}/{{ file_item }}.yaml
  register: file_contents
  become: true
  become_user: root
[...]
I receive a list of string (with stdout_lines) or a normal output (with stdout) but I need to have a list

You say you receive a list, and you also say you want to have a list. 
So, what is the problem then...?

Also, you didn't say what you are actually trying to achieve. Do you need the file content?  To do what?
In any case you could use the slurp module instead of command+cat:

If you have more context we may be able to help you better 



because a the end I want to make a diff with another list of dict, like this:
ok: [10.10.10.1] => {
    "daten_content": [
        {
            "agents": [
                "aaa",
                "bbb"
            ],
            "community": {
                "from xyzzy"
            },
            "disableDimensions": true,
            "Dimensions": {
                "HostGroup": "abc",
                "Type": "role",
                "env": "TEST"
            },
            "fields": [
[...]
Unfortunately I cannot retrieve what I need with a split or to pipe in list.
As anyone an idea how to do that or using another module?

--
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/d14aa8a7-fd9b-49f1-967e-cdeb38d3a506n%40googlegroups.com.
--
Sent from Gmail Mobile

Rowe, Walter P. (Fed)

unread,
Mar 22, 2023, 10:22:30 AM3/22/23
to ansible...@googlegroups.com
If the file content is JSON data you need to read it like JSON data.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Hiero-nymo

unread,
Mar 22, 2023, 11:37:44 AM3/22/23
to Ansible Project
The goal is to compare 2 files. The content of a file with a template file. First I'm trying to get the content of file then the template file and at the end I'll do the comparaison.
So, here's the result of my cat command:
[...]

"stdout_lines": [

            "# Ansible managed",

            "",

            "- agents:",

            "    - \"aaa\““,

            "    - \“bbb\““,

            "  community:",

            "    \"#from\": \“/etc/xyz\““,

            "  disableDimensions: true",

            "  Dimensions:",

            "    HostGroup: \“abc\““,

            "    Type: \“role\““,

            "    environment: \“TEST\““,

            "  fields:",

            "    - conversion: \“intg\““,

            "      name: \"UCD-SNMP-MIB\"",

            "      oid: \“.1.3…\““,

 […]

]

 
And here the template file:
[...]

"daten_content": [

        {

            "agents": [

                "aaa",

                "bbb"

            ],

            "community": {

               “\“#from\": \“/etc/xyz\“

            },

            "disableDimensions": true,

            "Dimensions": {

                "HostGroup": "abc",

                "Type": "role",

                "env": "TEST"

            },

            "fields": [

                {

                    "conv": „int“,

                    "name": "UCD-SNMP-MIB",

                    "oid": „.1.3…“

                },

 […]

]


I need to have the same format file to compare them. I've try many way with output of cat command but I don't find the right way and perhaps the's an easiest way?? 

Is it really better with slurp module?

Dick Visser

unread,
Mar 22, 2023, 1:18:19 PM3/22/23
to ansible...@googlegroups.com
On Wed, 22 Mar 2023 at 16:37, Hiero-nymo <jer.m...@gmail.com> wrote:
>
> The goal is to compare 2 files. The content of a file with a template file.


Comparing two files (one of them is already an ansible template?) is
not a goal, but rather an intermediate step.
What will you do with the result of this comparison?
If you can show your entire playbook, then it may get clear.

Dick

Hiero-nymo

unread,
Mar 23, 2023, 2:57:19 AM3/23/23
to Ansible Project
When something is added to the template then the file will be updated accordely but if you removed by example some parameters in template then nothing happens in the file. The goal is to compare the file and template to see if something has changed and finally modified it. The template must be always the standard.
Thanks for your help, H

Dick Visser

unread,
Mar 23, 2023, 6:14:23 AM3/23/23
to ansible...@googlegroups.com
I'm sorry but I don't understand.
What you describe sounds exactly like what a simple template task would do?
> --
> 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/ed725307-7456-4ece-8705-f724f187c4afn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages