https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
If you are falifilar with any of program language, you can understand loop in Ansible as iteration like for
. while
, or etc..
Answer from Cristian was the answer.
directory structure
├── files
│ └── list.txt
└── tasks
└── main.yml
In tasks/main.yml
,
---
- debug:
msg: "{{ item }}"
loop: "{{ lookup('file', 'files/list.txt').splitlines() }}"
In files/list.txt
,
This is the first line.
I'm second line.
Last line
Playbook resulst
$ ansible-playbook tasks/main.yml --connection=local
...
TASK [Check_locale : debug] ******************************************************************************
ok: [localhost] => (item=This is the first line.) => {
"msg": "This is the first line."
}
ok: [localhost] => (item=I'm second line.) => {
"msg": "I'm second line."
}
ok: [localhost] => (item=Last line) => {
"msg": "Last line"
}
...
directory structure
├── tasks
│ └── main.yml
└── vars
└── main.yml
In vars/main.yml
,
# TCP keepalive
tcp_kernel_parameters:
- {parameter: "net.ipv4.tcp_keepalive_time", value: 30}
- {parameter: "net.ipv4.tcp_keepalive_intvl", value: 15}
- {parameter: "net.ipv4.tcp_keepalive_probes", value: 3}
In tasks/main.yml
,
- name: iteration check.
loop: "{{ tcp_kernel_parameters }}"
when: "tcp_kernel_parameters is defined"
debug:
msg: "{{ item.parameter }} hoo {{ item.value }}"