Ansible Playbook Loops循环使用
程序员文章站
2022-07-12 21:09:09
...
1. 标准循环(with_items:)
with_items的值是python list数据结构,每个task会循环读取list里面的值,然后key的名称是item,也支持python字典形式
- 实例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
debug: msg="name --------> {{ item }}"
with_items:
- one
- two
- three
###### 检查yml文件是否正确 ansible-playbook loops.yml --syntax-check
###### [[email protected] playbooks]# ansible-playbook loops.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.10.5]
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=one) => {
"item": "one",
"msg": "name --------> one"
}
ok: [192.168.10.5] => (item=two) => {
"item": "two",
"msg": "name --------> two"
}
ok: [192.168.10.5] => (item=three) => {
"item": "three",
"msg": "name --------> three"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=2 changed=0 unreachable=0 failed=0
2. 嵌套循环(with_nested:)
嵌套循环主要实现一对多,多对多的合并
- 实例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
debug: msg="name --------> {{ item[0] }} key-------> {{ item[1] }}"
with_nested:
- ['A']
- ['1','2','3']
###### [[email protected] playbooks]# ansible-playbook loops-nested.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.10.5]
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=[u'A', u'1']) => {
"item": [
"A",
"1"
],
"msg": "name --------> A key-------> 1"
}
ok: [192.168.10.5] => (item=[u'A', u'2']) => {
"item": [
"A",
"2"
],
"msg": "name --------> A key-------> 2"
}
ok: [192.168.10.5] => (item=[u'A', u'3']) => {
"item": [
"A",
"3"
],
"msg": "name --------> A key-------> 3"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=2 changed=0 unreachable=0 failed=0
3. 散列循环(with_dict:)
散列loops相比标准loops就是变量支持更丰富的数据结构,支持YAML格式的数据变量
- 实例
---
- hosts: all
gather_facts: False
vars:
user:
fengct:
name: fengct
shell: bash
test:
name: test
shell: sh
tasks:
- name: debug loops
debug: msg="name --------> {{ item.key }} keys -------> {{ item.value.name }} shell -------> {{ item.value.shell }}"
with_dict: "{{ user }}"
[[email protected] playbooks] ansible-playbook loops-dict.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item={'key': u'test', 'value': {u'shell': u'sh', u'name': u'test'}}) => {
"item": {
"key": "test",
"value": {
"name": "test",
"shell": "sh"
}
},
"msg": "name --------> test keys -------> test shell -------> sh"
}
ok: [192.168.10.5] => (item={'key': u'fengct', 'value': {u'shell': u'bash', u'name': u'fengct'}}) => {
"item": {
"key": "fengct",
"value": {
"name": "fengct",
"shell": "bash"
}
},
"msg": "name --------> fengct keys -------> fengct shell -------> bash"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=1 changed=0 unreachable=0 failed=0
4. 文件匹配循环(with_fileglob:)
- 实例
---
- hosts: all
gather_facts: False
tasks:
- name: debug loops
debug: msg="YAML files ---------> {{ item }}"
with_fileglob:
- /var/log/*.log
###### [[email protected] playbooks]# ansible-playbook loops-fileglob.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=/var/log/boot.log) => {
"item": "/var/log/boot.log",
"msg": "YAML files ---------> /var/log/boot.log"
}
ok: [192.168.10.5] => (item=/var/log/yum.log) => {
"item": "/var/log/yum.log",
"msg": "YAML files ---------> /var/log/yum.log"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=1 changed=0 unreachable=0 failed=0
5. 随机选择循环(with_random_choice:)
- 实例
---
- hosts: all
gather_facts: False
tasks:
- name: debug loops
debug: msg="name --------> {{ item }}"
with_random_choice:
- one
- two
- three
###### [[email protected] playbooks]# ansible-playbook loops-random_choice.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=one) => {
"item": "one",
"msg": "name --------> one"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=1 changed=0 unreachable=0 failed=0
###### [[email protected] playbooks]# ansible-playbook loops-random_choice.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=three) => {
"item": "three",
"msg": "name --------> three"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=1 changed=0 unreachable=0 failed=0
6. 条件判断Loops(until:)
每5s执行一次cat /root/ansible,然后将结果交给register的host参数,然后判断host.stdout.startswith的内容是否是cat字符串开头,如果条件成立,此task运行完成,如果条件不成立5s后重试,5次还不成立,此task运行失败。这里选用的目录就是一个假目录,所以最后执行结果会是失败的
- 实例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
shell: cat /root/ansible
register: host
until: host.stdout.startswith("name")
retries: 5
delay: 5
###### [[email protected] playbooks]# ansible-playbook loops-until.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.10.5]
TASK [debug loops] **************************************************************************************************************************
FAILED - RETRYING: debug loops (5 retries left).
FAILED - RETRYING: debug loops (4 retries left).
FAILED - RETRYING: debug loops (3 retries left).
FAILED - RETRYING: debug loops (2 retries left).
FAILED - RETRYING: debug loops (1 retries left).
fatal: [192.168.10.5]: FAILED! => {"attempts": 5, "changed": true, "cmd": "cat /root/ansible", "delta": "0:00:00.004363", "end": "2018-01-10 03:43:32.985193", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2018-01-10 03:43:32.980830", "stderr": "cat: /root/ansible: 没有那个文件或目录", "stderr_lines": ["cat: /root/ansible: 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
to retry, use: --limit @/etc/ansible/playbooks/loops-until.retry
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=1 changed=0 unreachable=0 failed=1
7. 文件优先匹配Loops(with_first_found:)
如果你想引用一个文件,而该文件是从一组文件中根据给定条件匹配出来的,可以使用这种模式
- 实例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
debug: msg="YAML files --------> {{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yml"
- "loops.yml"
###### [[email protected] playbooks]# ansible-playbook loops-first_found.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.10.5]
TASK [debug loops] **************************************************************************************************************************
ok: [192.168.10.5] => (item=/etc/ansible/playbooks/loops.yml) => {
"item": "/etc/ansible/playbooks/loops.yml",
"msg": "YAML files --------> /etc/ansible/playbooks/loops.yml"
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=2 changed=0 unreachable=0 failed=0
8. register loops (with_random_choice:)
register适用于task直接互相传递数据的,一般我们会把register用在单一的task中进行变量临时存储,其实register还可以同时接受多个task的结果当做变量临时存储
- 实例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
shell: "{{ item }}"
with_items:
- date
- hostname
- uname
register: num
- name: SYSTEM INFO LOOPS----------->
debug: msg="{% for i in num.results %} {{ i.stdout }} {% endfor %}"
###### [[email protected] playbooks]# ansible-playbook loops-register.yml -l 192.168.10.5
PLAY [all] **********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.10.5]
TASK [debug loops] **************************************************************************************************************************
changed: [192.168.10.5] => (item=date)
changed: [192.168.10.5] => (item=hostname)
changed: [192.168.10.5] => (item=uname)
TASK [SYSTEM INFO LOOPS----------->] ********************************************************************************************************
ok: [192.168.10.5] => {
"msg": " 2018年 01月 10日 星期三 03:50:28 UTC codis01 Linux "
}
PLAY RECAP **********************************************************************************************************************************
192.168.10.5 : ok=3 changed=1 unreachable=0 failed=0
转载于:https://www.jianshu.com/p/100a27316676