Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

382
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!