Ansible and Iteration

Description: This is for iteration of a list in tasks. The list is from a stdout_lines output defined to a variable.

# Information
In order to iterate file2 list. We will do this in main.yml
#################################################
Main.yml

– include: Step1.yml

– include: Step2.yml
with_items: “{{file2|default([]) }}”

Main.yml EOF
#################################################
Step1.yml

– name: cat file or list of files to variable.
command: cat /file/to/path
register: file1

– name: set fact to variable
file2: “{{file1.stdout_lines}}”

Step1 EOF
#################################################
Step2.yml

– name: output variable.
debug:
var: {{ item }}”

– name: output junk
debug:
msg: “This is a test”

Step2.yml EOF
#################################################

# Information
File Output
cat /file/to/path
info1
info2
info3

Playbook Output:
info1
This is a test
info2
This is a test
info3
This is a test

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.