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

153
Views
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 answers
Answer question

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 Report

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 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!