coding
coding

Reputation: 181

Ansible looping items in file

I have a file(a_file.txt) like this:

22
23
8080

I need to loop each item in a_file.txt with my host and formatted to be host:22, host:23, host:8080...and so on, so I can use shell module in playbook like this:

---
- hosts: host1
  tasks:
    - name: Remote hostname
      shell: hostname
      register: hostname

    - name: Read items from a_file.txt
      shell: cat a_file.txt
      register: item_output

    - name: Run shell command
      shell: someCommand {{hostname.stdout_line|nice_to_yaml}}:{{item}}
      with_items: item_output.stdout_lines

However, my someCommand failed because I have:

{{hostname.stdout_line|nice_to_yaml}} = - hostname\n
{{<item in a_file.txt>}} = [u'\22, u'\23, u'\8080]

Upvotes: 0

Views: 217

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

you have to use:

- name: Run shell command
  shell: someCommand {{hostname.stdout_line|nice_to_yaml}}:{{item}}
  with_items: "{{ item_output.stdout_lines }}"

Upvotes: 1

Related Questions