Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda