Chloe
Chloe

Reputation: 191

linux awk command in ansible looping

i am a beginner in using awk. Using ansible, i want to print the received output of ping command then write it to file. pings are done from hosts 10.0.0.3 and 10.0.0.6 to 10.0.0.1 and 10.0.0.2, respectively. The following is ansible task that I have:

- name: CHECK PING
  shell: ping {{ item[0] }} -c 2 -s {{ item[1] }} | grep -i received | awk '{print "{{item[1]}} :\n"," {{item[0]}} :", $4}'
  register: p
  with_nested:
  - [ '10.0.0.1', '10.0.0.2' ]
  - [ '10.0.0.3', '10.0.0.6' ]

- copy:
    content: "{{ p.results | map(attribute='stdout') | join('\n')}}"
    dest: ping.txt

keep in mind that item[1] is the ping source (10.0.0.3 and 10.0.0.6) and the item[0] is the ping destination. the following is the contents of ping.txt

10.0.0.3 :
   10.0.0.1 : 2
10.0.0.6 :
   10.0.0.1 : 2
10.0.0.3 :
   10.0.0.2 : 0
10.0.0.6 :
   10.0.0.2 : 0

even though the output is correct, I don't want a double "key", so the output I want is:

10.0.0.3 :
   10.0.0.1 : 2
   10.0.0.2 : 0
10.0.0.6 :
   10.0.0.1 : 2
   10.0.0.2 : 0

(i.e. concatenate data 10.0.0.3 and 10.0.0.6 so that their "key" is not double). what changes to the script need to be made to get this result?

Upvotes: 1

Views: 383

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

Configure awk to print valid YAML

    p:
      results:
        - stdout: '{10.0.0.3: {10.0.0.1: 2}}'
        - stdout: '{10.0.0.6: {10.0.0.1: 2}}'
        - stdout: '{10.0.0.3: {10.0.0.2: 0}}'
        - stdout: '{10.0.0.6: {10.0.0.2: 0}}'

Create a list of grouped keys

    - set_fact:
        l1: "{{ p.results|
                map(attribute='stdout')|
                map('from_yaml')|
                map('dict2items')|
                flatten|
                groupby('key') }}"

gives

l1:
  - - 10.0.0.3
    - - key: 10.0.0.3
        value:
          10.0.0.1: 2
      - key: 10.0.0.3
        value:
          10.0.0.2: 0
  - - 10.0.0.6
    - - key: 10.0.0.6
        value:
          10.0.0.1: 2
      - key: 10.0.0.6
        value:
          10.0.0.2: 0

Now, combine the dictionary

    - set_fact:
        d1: "{{ d1|d({})|combine({_key: _val}) }}"
      loop: "{{ l1 }}"
      vars:
        _key: "{{ item.0 }}"
        _val: "{{ item.1|map(attribute='value')|list }}"

gives

d1:
  10.0.0.3:
    - 10.0.0.1: 2
    - 10.0.0.2: 0
  10.0.0.6:
    - 10.0.0.1: 2
    - 10.0.0.2: 0

Combine the value items if you want to get dictionaries instead of the lists

        _val: "{{ item.1|map(attribute='value')|combine }}"

gives the expected result

  d1:
    10.0.0.3:
      10.0.0.1: 2
      10.0.0.2: 0
    10.0.0.6:
      10.0.0.1: 2
      10.0.0.2: 0

Upvotes: 3

Related Questions