Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

381
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda