1. 標(biāo)準(zhǔn)循環(huán)(with_items:)
with_items的值是python list數(shù)據(jù)結(jié)構(gòu),每個(gè)task會(huì)循環(huán)讀取list里面的值武学,然后key的名稱是item,也支持python字典形式
- 實(shí)例
---
- 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
###### [root@codis01 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. 嵌套循環(huán)(with_nested:)
嵌套循環(huán)主要實(shí)現(xiàn)一對(duì)多,多對(duì)多的合并
- 實(shí)例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
debug: msg="name --------> {{ item[0] }} key-------> {{ item[1] }}"
with_nested:
- ['A']
- ['1','2','3']
###### [root@codis01 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. 散列循環(huán)(with_dict:)
散列l(wèi)oops相比標(biāo)準(zhǔn)loops就是變量支持更豐富的數(shù)據(jù)結(jié)構(gòu)合是,支持YAML格式的數(shù)據(jù)變量
- 實(shí)例
---
- 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 }}"
[root@codis01 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. 文件匹配循環(huán)(with_fileglob:)
- 實(shí)例
---
- hosts: all
gather_facts: False
tasks:
- name: debug loops
debug: msg="YAML files ---------> {{ item }}"
with_fileglob:
- /var/log/*.log
###### [root@codis01 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. 隨機(jī)選擇循環(huán)(with_random_choice:)
- 實(shí)例
---
- hosts: all
gather_facts: False
tasks:
- name: debug loops
debug: msg="name --------> {{ item }}"
with_random_choice:
- one
- two
- three
###### [root@codis01 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
###### [root@codis01 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執(zhí)行一次cat /root/ansible,然后將結(jié)果交給register的host參數(shù)锭环,然后判斷host.stdout.startswith的內(nèi)容是否是cat字符串開頭聪全,如果條件成立,此task運(yùn)行完成辅辩,如果條件不成立5s后重試难礼,5次還不成立娃圆,此task運(yùn)行失敗。這里選用的目錄就是一個(gè)假目錄蛾茉,所以最后執(zhí)行結(jié)果會(huì)是失敗的
- 實(shí)例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
shell: cat /root/ansible
register: host
until: host.stdout.startswith("name")
retries: 5
delay: 5
###### [root@codis01 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: 沒(méi)有那個(gè)文件或目錄", "stderr_lines": ["cat: /root/ansible: 沒(méi)有那個(gè)文件或目錄"], "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. 文件優(yōu)先匹配Loops(with_first_found:)
如果你想引用一個(gè)文件讼呢,而該文件是從一組文件中根據(jù)給定條件匹配出來(lái)的,可以使用這種模式
- 實(shí)例
---
- hosts: all
gather_facts: True
tasks:
- name: debug loops
debug: msg="YAML files --------> {{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yml"
- "loops.yml"
###### [root@codis01 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直接互相傳遞數(shù)據(jù)的谦炬,一般我們會(huì)把register用在單一的task中進(jìn)行變量臨時(shí)存儲(chǔ)悦屏,其實(shí)register還可以同時(shí)接受多個(gè)task的結(jié)果當(dāng)做變量臨時(shí)存儲(chǔ)
- 實(shí)例
---
- 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 %}"
###### [root@codis01 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