ansible-playbook的循环(Loops)
1.标准的Loops:也是最常用的一种循环
例:当需要安装10个软件包时,不用写10次任务,只需要写一次然后进行循环即可
#注:已经安装的软件系统即不在安装 [root@nfs-server playbook]# cat yum_list.yml --- - hosts: webservers remote_user: root gather_facts: False tasks: - name: "需要安装的软件清单" yum: name={{ item }} state=present with_items: - lrzsz - vim - sysstat [root@nfs-server playbook]# ansible-playbook yum_list.yml PLAY [webservers] ************************************************************************************************************************************** TASK [需要安装的软件清单] *************************************************************************************************************************************** changed: [192.168.2.101] => (item=[u‘lrzsz‘, u‘vim‘, u‘sysstat‘]) changed: [192.168.2.111] => (item=[u‘lrzsz‘, u‘vim‘, u‘sysstat‘]) PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.111 : ok=1 changed=1 unreachable=0 failed=0
2.字典格式的循环:with_items
#写法一: [root@nfs-server playbook]# cat dict_list.yml --- - hosts: webservers remote_user: root gather_facts: False tasks: - name: "字典格式的循环" debug: msg="name ---->{{ item.name }},age---->{{ item.age }}" with_items: - {name: "Liu Zhengwei",age: 28} - {name: "Jia Dongli",age: 25} #写法二: [root@nfs-server playbook]# cat dict_list.yml --- - hosts: webservers remote_user: root gather_facts: False tasks: - name: "字典格式的循环" debug: msg="name ---->{{ item.name }},age---->{{ item.age }}" with_items: - name: "Liu Zhengwei" age: 28 - name: "Jia Dongli" age: 25 #注:以上两种写法效果是一样的 [root@nfs-server playbook]# ansible-playbook dict_list.yml PLAY [webservers] ************************************************************************************************************************************** TASK [字典格式的循环] ***************************************************************************************************************************************** ok: [192.168.2.101] => (item={u‘age‘: 28, u‘name‘: u‘Liu Zhengwei‘}) => { "item": { "age": 28, "name": "Liu Zhengwei" }, "msg": "name ---->Liu Zhengwei,age---->28" } ok: [192.168.2.101] => (item={u‘age‘: 25, u‘name‘: u‘Jia Dongli‘}) => { "item": { "age": 25, "name": "Jia Dongli" }, "msg": "name ---->Jia Dongli,age---->25" } ok: [192.168.2.111] => (item={u‘age‘: 28, u‘name‘: u‘Liu Zhengwei‘}) => { "item": { "age": 28, "name": "Liu Zhengwei" }, "msg": "name ---->Liu Zhengwei,age---->28" } ok: [192.168.2.111] => (item={u‘age‘: 25, u‘name‘: u‘Jia Dongli‘}) => { "item": { "age": 25, "name": "Jia Dongli" }, "msg": "name ---->Jia Dongli,age---->25" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=1 changed=0 unreachable=0 failed=0
3.嵌套Loops(列表格式的循环,用于1对多或者多对多关系时)--> with_nested
[root@nfs-server playbook]# cat netsted_list.yml --- - hosts: webservers remote_user: root gather_facts: False tasks: - name: "实现1对多关系的循环" debug: msg="name--->{{ item[0] }},value--->{{ item[1] }}" with_nested: - [‘A‘] - [‘a‘,‘b‘,‘c‘ ] [root@nfs-server playbook]# ansible-playbook netsted_list.yml -l 192.168.2.101 PLAY [webservers] ************************************************************************************************************************************** TASK [实现1对多关系的循环] ************************************************************************************************************************************** ok: [192.168.2.101] => (item=[u‘A‘, u‘a‘]) => { "item": [ "A", "a" ], "msg": "name--->A,value--->a" } ok: [192.168.2.101] => (item=[u‘A‘, u‘b‘]) => { "item": [ "A", "b" ], "msg": "name--->A,value--->b" } ok: [192.168.2.101] => (item=[u‘A‘, u‘c‘]) => { "item": [ "A", "c" ], "msg": "name--->A,value--->c" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=1 changed=0 unreachable=0 failed=0
4.散列loops:with_dict(支持更丰富的数据结构)
注:with_dict的写法在新版本中进行了改变,必须要写成字典的形式,跟我下面写的格式一样
[root@nfs-server playbook]# cat with_dict.yml --- - hosts: webservers gather_facts: False remote_user: root vars: user: shencan: name: shencan shell: bash ruifengyun: name: ruifengyun shell: zsh tasks: - name: debug loops debug: "msg=name--->{{ item.key }} value--->{{ item.value.name }} shell--->{{ item.value.shell }}" with_dict: "{{ user }}" [root@nfs-server playbook]# ansible-playbook with_dict.yml -l 192.168.2.101 PLAY [webservers] ************************************************************************************************************************************** TASK [debug loops] ************************************************************************************************************************************* ok: [192.168.2.101] => (item={‘key‘: u‘ruifengyun‘, ‘value‘: {u‘shell‘: u‘zsh‘, u‘name‘: u‘ruifengyun‘}}) => { "item": { "key": "ruifengyun", "value": { "name": "ruifengyun", "shell": "zsh" } }, "msg": "name--->ruifengyun" } ok: [192.168.2.101] => (item={‘key‘: u‘shencan‘, ‘value‘: {u‘shell‘: u‘bash‘, u‘name‘: u‘shencan‘}}) => { "item": { "key": "shencan", "value": { "name": "shencan", "shell": "bash" } }, "msg": "name--->shencan" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=1 changed=0 unreachable=0 failed=0
本文出自 “激情燃烧的岁月” 博客,请务必保留此出处http://liuzhengwei521.blog.51cto.com/4855442/1962259
原文地址:http://liuzhengwei521.blog.51cto.com/4855442/1962259