我们在使用ansible的时候会经常用到循环的使用,今天我们来分享一下如何在yml文件里面编写循环
示例
单一循环
如果我们需要做一个变量的循环时,我们可以使用with_items来进行循环,将msg输出结果的位置将之前的变量名字修改为“item”,通过with_items来进行变量pangshare的循环。
- name: hello debug
hosts: ansible-node1
vars:
pangshare:
- test1
- test2
- test3
vars_files :
- "/root/ansible-code/playbook/vars/vars.yml"
tasks:
- name: hello debug
debug:
msg: "{{ item }}"
with_items : "{{ pangshare }}"

多变量循环
当我们需要对多个变量进行循环的时候,我们需要用到with_nested来做多变量的循环。同时在msg输出的时候需要明确输出的元素。
- name: hello debug
hosts: ansible-node1
vars:
pang:
- test1
- test2
- test3
shang:
- hello1
vars_files :
- "/root/ansible-code/playbook/vars/vars.yml"
tasks:
- name: hello debug
debug:
msg: "{{ item[0] }},{{ item[1] }}"
with_nested:
- "{{ pang }}"
- "{{ share }}"

此文章为原创文章,作者:胖哥叨逼叨,如若转载,请与我联系并注明出处:https://www.pangshare.com/2425.htm
微信扫一扫