Ansible playbook循环实践总结<一>
1、标准Loops
标准loops可以直接减少task的次数,如下:
[root@zero01 playbook]# vi loops.yaml --- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name ------> {{ item }}" with_items: - one - two
运行 loops.yaml 如下:
[root@zero01 playbook]# ansible-playbook loops.yaml -l 192.168.79.129 PLAY [all] *********************************************************************************************** TASK [debug loops] *************************************************************************************** "changed": false, "item": "one", "msg": "name ------> one" } ok: [192.168.79.129] => (item=two) => { "changed": false, "item": "two", "msg": "name ------> two" } PLAY RECAP *********************************************************************************************** 192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
其中with_items的值是python list数据结构,每个task会循环读取list里面的值。with_items后也可以跟python字典,如下:
[root@zero01 playbook]# vi loops.yaml --- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name ------> {{ item.key }} value -----> {{ item.value }}" with_items: - {key: "one", value: "VALUE1"} - {key: "one", value: "VALUE2"}
[root@zero01 playbook]# ansible-playbook loops.yaml -l 192.168.79.129 PLAY [all] *********************************************************************************************** TASK [debug loops] *************************************************************************************** ok: [192.168.79.129] => (item={u'key': u'one', u'value': u'VALUE1'}) => { "changed": false, "item": { "key": "one", "value": "VALUE1" }, "msg": "name ------> one value -----> VALUE1" } ok: [192.168.79.129] => (item={u'key': u'one', u'value': u'VALUE2'}) => { "changed": false, "item": { "key": "one", "value": "VALUE2" }, "msg": "name ------> one value -----> VALUE2" } PLAY RECAP *********************************************************************************************** 192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
嵌套 Loops 也是常见的循环,主要实现一对多或者多对多的合并,如下:
[root@zero01 playbook]# vi loops.yaml --- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name ------> {{ item[0] }} value ------> {{ item[1] }}" with_nested: - ['A'] - ['a','b','c']
[root@zero01 playbook]# ansible-playbook loops.yaml -l 192.168.79.129 PLAY [all] ******************************************************************************** TASK [debug loops] ************************************************************************ ok: [192.168.79.129] => (item=[u'A', u'a']) => { "changed": false, "item": [ "A", "a" ], "msg": "name ------> A value ------> a" } ok: [192.168.79.129] => (item=[u'A', u'b']) => { "changed": false, "item": [ "A", "b" ], "msg": "name ------> A value ------> b" } ok: [192.168.79.129] => (item=[u'A', u'c']) => { "changed": false, "item": [ "A", "c" ], "msg": "name ------> A value ------> c" } PLAY RECAP ******************************************************************************** 192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
one=['A']
two=['a','b','c']
[i + y for i in one for y in two]
['Aa', 'Ab', 'Ac']