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

155
Visualizações
how to traverse through each line of a text file in ansible

I have to traverse through package list file which contains list of packages with their architecture. How can I feed those input to my playbook file? I found a way to get the package names alone but architecture version is not coming. This is my package_list file

nginx | x86_64
telnet| x86_64
openssh | i386

This is my playbook

 - name: get contents of package.txt
   command: cat "/root/packages.txt"
   register: _packages
 - name: get contents of architecture from packages.txt
   command: cat "/root/packages.txt" | awk '{print $3}'
   register: _arch

 - name: Filter
   theforeman.foreman.content_view_filter:
     username: "admin"
     password: "mypass"
     server_url: "myhost"
     name: "myfilter"
     organization: "COT"
     content_view: "M_view"
     filter_type: "rpm"
     architecture: "{{ _arch }}"
     package_name: "{{ item }}"
     inclusion: True
   loop: "{{ _packages.stdout_lines }}"
   loop: "{{ _arch.stdout_lines }}"

Any help would be appreciated

The required output is package name and architecture should be read from packages.txt file through ansible-playbook

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

try this playbook:

- name: Reproduce issue
  hosts: localhost
  gather_facts: no
  tasks:
    - name: get contents of package.txt
      command: cat "/root/packages.txt"
      register: _packages
    - debug: 
        msg: "package: {{ line.0 }}, arch: {{ line.1 }}"         
      loop: "{{ _packages.stdout_lines }}"
      vars:
        line: "{{ item.split('|')|list }}"

result:

ok: [localhost] => (item=nginx | x86_64) => {
    "msg": "package: nginx , arch:  x86_64 "
}
ok: [localhost] => (item=telnet| x86_64) => {
    "msg": "package: telnet, arch:  x86_64 "
}
ok: [localhost] => (item=openssh | i386) => {
    "msg": "package: openssh , arch:  i386 "
}

for your case:

 - name: Filter
   theforeman.foreman.content_view_filter:
     :
     :
     architecture: "{{ line.1 }}"
     package_name: "{{ line.0 }}"
     inclusion: True
   loop: "{{ _packages.stdout_lines }}"
   vars:
     line: "{{ item.split('|')|list }}"

following the version of ansible you could write too line: "{{ item | split('|') | list }}"

over 4 years ago · Santiago Trujillo Relatório

0

You need to split up the line into the necessary values by filtering.

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Gahter package list
    shell:
      cmd: cat package.txt
    register: _packages

  - name: Show packages
    debug:
      msg: "Name: {{ item.split('|')[0] }}, Arch: {{ item.split('|')[1] }}"
    loop: "{{ _packages.stdout_lines }}"

Further Documentation

  • Playbook filters - Manipulating strings
  • Jinja Template Designer - Filters

Further Q&A

  • Split string into list in Jinja?
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