【Ansible】ansible循环
Ansible 循环
一、简单介绍
在ansible2.5之前,大多数人使”with_XXX”类型的关键字来操作循环,但是从2.6版本开始,官方推荐是”loop”关键字代替” with_XXX”。
1.我们先看下一个小例子,使用loop关键字进行最简单的循环:
[root@localhost cycle]# cat cycle..yml
---
- name: cycletest
hosts: test
gather_facts: no
tasks:
- name: debug cycle
debug:
msg: "{{ item }}"
loop:
- test1
- test2
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test] ***************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=test1) => {
"msg": "test1"
}
ok: [192.168.15.10] => (item=test2) => {
"msg": "test2"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .17s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
实例中所以loop关键字,替换之前的with_XXX关键字,它们的效果是完全相同的。
2.我们可以使用loop关键字和dict插件代替”with_dict”关键字,示例如下:
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test2
hosts: test
gather_facts: no
vars:
dicts:
China:
America:
tasks:
- name: debug cycle
debug:
msg: "{{ item.key }} is no.{{ item.value }}"
loop: "{{ lookup('dict',dicts) }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test2] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item={'key': u'America', 'value': }) => {
"msg": "America is no.2"
}
ok: [192.168.15.10] => (item={'key': u'China', 'value': }) => {
"msg": "China is no.1"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .18s
Playbook finished: Mon Dec :: , total tasks. :: elapsed
示例已经在上一个例子中讲述,再次不在讲述
3.上个例子中使用”loop+lookup”的方式完成循环,而在2.6版本的官网中推荐使用”loop+filter”方式老代替”loop+loopup”的方式,什么意思呢? 我们来看个小例子,如下:
---
- name: cycle test3
hosts: test
gather_facts: no
vars:
dicts:
China:
America:
tasks:
- name: debug cycle
debug:
msg: "{{ item.key }} is no.{{ item.value }}"
loop: "{{ dicts | dict2items }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test3] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item={'key': u'America', 'value': }) => {
"msg": "America is no.2"
}
ok: [192.168.15.10] => (item={'key': u'China', 'value': }) => {
"msg": "China is no.1"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .18s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
如上例所示,在使用loop关键字操作对于的字典变量users时,并没有借助dict插件,而是使用dict2items的过滤器。
二、具体示例
1.With_list
#loop可以替代with_list,当处理嵌套的列表时,列表不会被拉平
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test4
hosts: test
gather_facts: no
vars:
dicts:
- A
- B
- [c,D]
tasks:
- name: debug cycle
debug:
msg: "{{ item }}"
loop: "{{ dicts }}"
结果:
[root@localhost cycle]# ansible-playbook cycle.4.yml
PLAY [cycle test4] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=A) => {
"msg": "A"
}
ok: [192.168.15.10] => (item=B) => {
"msg": "B"
}
ok: [192.168.15.10] => (item=[u'c', u'D']) => {
"msg": [
"c",
"D"
]
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
debug cycle ------------------------------------------------------------- 0.19s
Playbook finished: Mon Dec 24 18:25:41 2018, 1 total tasks. 0:00:00 elapsed.
2.With_flattened
#flatten过滤器可以替代with_flattened,当处理多层嵌套的列表时,列表中所有的嵌套层级都会被拉平
[root@localhost cycle]# cat cycle.5.yml
---
- name: cycle test5
hosts: test
gather_facts: no
vars:
dicts:
- A
- B
- [c,D]
tasks:
- name: debug cycle
debug:
msg: "{{ item }}"
loop: "{{ dicts | flatten }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test5] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=A) => {
"msg": "A"
}
ok: [192.168.15.10] => (item=B) => {
"msg": "B"
}
ok: [192.168.15.10] => (item=c) => {
"msg": "c"
}
ok: [192.168.15.10] => (item=D) => {
"msg": "D"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .19s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
3.With_items
#flatten过滤器(加参数)可以替代with_items,当处理多层嵌套的列表时,只有列表中的第一层会被拉平
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test6
hosts: test
gather_facts: no
vars:
dicts:
- A
- B
- [c,D]
tasks:
- name: debug cycle
debug:
msg: "{{ item }}"
loop: "{{ dicts | flatten(levels=1) }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test6] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=A) => {
"msg": "A"
}
ok: [192.168.15.10] => (item=B) => {
"msg": "B"
}
ok: [192.168.15.10] => (item=c) => {
"msg": "c"
}
ok: [192.168.15.10] => (item=D) => {
"msg": "D"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .29s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
#PS 嗯 处理下客户问题先。Weblogic真毒~
4.With_indexed_items
#flatten过滤器(加参数),再配合loop_control关键字,可以替代with_indexed_items
#当处理多层嵌套的列表时,只有列表中的第一层会被拉平,flatten过滤器的bug暂且忽略
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test7
hosts: test
gather_facts: no
vars:
dicts:
- A
- B
- [c,D]
tasks:
- name: debug cycle
debug:
msg: " {{ index}}--{{ item }}"
loop: "{{ dicts | flatten(levels=1) }}"
loop_control:
index_var: index
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test7] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=A) => {
"msg": " 0--A"
}
ok: [192.168.15.10] => (item=B) => {
"msg": " 1--B"
}
ok: [192.168.15.10] => (item=c) => {
"msg": " 2--c"
}
ok: [192.168.15.10] => (item=D) => {
"msg": " 3--D"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .21s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
“loop_control”关键字可以用于控制循环的行为,比如在循环是获取元素的索引。
“index_var “是”loop_control”的一个设置选项,”index_var”可以让我们指定变量,”loop_control”会将元素索引值存放在指定变量中
5.With_togeher
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test8
hosts: test
gather_facts: no
vars:
testlist1: [ A,B,C,D ]
testlist2: [ ,, ]
testlist3: [ x,y ]
tasks:
- name: debug cycle with_together
debug:
msg: " {{ item.0 }} - {{ item.1 }} - {{ item.2 }}"
with_together:
- "{{ testlist1 }}"
- "{{ testlist2 }}"
- "{{ testlist3 }}"
- name: debug cycle loop+zip_logest
debug:
msg: " {{ item.0 }} - {{ item.1 }} - {{ item.2 }}"
loop: "{{ testlist1 | zip_longest(testlist2,testlist3) | list }}
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test8] **************************************************************************************
TASK [debug cycle with_together] ************************************************************************
ok: [192.168.15.10] => (item=[u'A', , u'x']) => {
"msg": " A - 110 - x"
}
ok: [192.168.15.10] => (item=[u'B', , u'y']) => {
"msg": " B - 120 - y"
}
ok: [192.168.15.10] => (item=[u'C', , None]) => {
"msg": " C - 911 - "
}
ok: [192.168.15.10] => (item=[u'D', None, None]) => {
"msg": " D - - "
}
TASK [debug cycle loop+zip_logest] **********************************************************************
ok: [192.168.15.10] => (item=[u'A', , u'x']) => {
"msg": " A - 110 - x"
}
ok: [192.168.15.10] => (item=[u'B', , u'y']) => {
"msg": " B - 120 - y"
}
ok: [192.168.15.10] => (item=[u'C', , None]) => {
"msg": " C - 911 - "
}
ok: [192.168.15.10] => (item=[u'D', None, None]) => {
"msg": " D - - "
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle with_together ----------------------------------------------- .25s
debug cycle loop+zip_logest --------------------------------------------- .21s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
示例中同时写出2中方法方便进行比较。
但多个列表使用”with_together”进行对比合并时,如果列表长度不同,这使用最长的列表长度进行对其,由于短列表中元素不足,所以使用空值与长列表中元素进行对齐,zip_longest过滤器也和”with_together”一样,对列表进行组合,但是还需要借助list过滤器,将组合的数据列表化。
可以指定字符代替空值
- debug:
msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
loop: "{{ testlist1 | zip_longest(testlist2,testlist3,fillvalue='None') | list }}"
和最短的列表进行对齐
- debug:
msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
loop: "{{ testlist1 | zip(testlist2,testlist3) | list }}"
.With_nested/With_cartesian
#product过滤器配合list过滤器,可以替代with_nested和with_cartesian
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test4
hosts: test
gather_facts: no
vars:
list1: [ ,, ]
list2: [ a,b,c,d ]
tasks:
- name: debug cycle
debug:
msg: "{{ item.0 }} ---- {{ item.1 }}"
loop: "{{ list1 | product(list2) | list }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test4] **************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=[, u'a']) => {
"msg": "1 ---- a"
}
ok: [192.168.15.10] => (item=[, u'b']) => {
"msg": "1 ---- b"
}
ok: [192.168.15.10] => (item=[, u'c']) => {
"msg": "1 ---- c"
}
ok: [192.168.15.10] => (item=[, u'd']) => {
"msg": "1 ---- d"
}
ok: [192.168.15.10] => (item=[, u'a']) => {
"msg": "2 ---- a"
}
ok: [192.168.15.10] => (item=[, u'b']) => {
"msg": "2 ---- b"
}
ok: [192.168.15.10] => (item=[, u'c']) => {
"msg": "2 ---- c"
}
ok: [192.168.15.10] => (item=[, u'd']) => {
"msg": "2 ---- d"
}
ok: [192.168.15.10] => (item=[, u'a']) => {
"msg": "3 ---- a"
}
ok: [192.168.15.10] => (item=[, u'b']) => {
"msg": "3 ---- b"
}
ok: [192.168.15.10] => (item=[, u'c']) => {
"msg": "3 ---- c"
}
ok: [192.168.15.10] => (item=[, u'd']) => {
"msg": "3 ---- d"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .31s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
7.With_random_choice
#使用random函数可以替代with_random_choice,由于random函数是随机取出列表中的一个值,并不涉及循环操作,所以并不用使用loop关键字。
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test
hosts: test
gather_facts: no
vars:
list: [a,b,c]
tasks:
- name: debug cycle
debug:
msg: "{{ list | random }}"
- debug:
msg: "{{ list | random }}"
- debug:
msg: "{{ list | random }}
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test] ***************************************************************************************
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => {
"msg": "a"
}
TASK [debug] ********************************************************************************************
ok: [192.168.15.10] => {
"msg": "a"
}
TASK [debug] ********************************************************************************************
ok: [192.168.15.10] => {
"msg": "c"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle ------------------------------------------------------------- .18s
------------------------------------------------------------------------ .16s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
8.with_dict
#除了上文总结的dict2items过滤器,dictsort过滤器也可以替代with_dict
[root@localhost cycle]# cat cycle..yml
---
- name: cycle test2
hosts: test
gather_facts: no
vars:
dicts:
China:
America:
aaa:
bbb:
ccc:
tasks:
- name: debug cycle dict2items
debug:
msg: "{{ item.key }} is no.{{ item.value }}"
loop: "{{ dicts | dict2items }}"
- name: debug cycle
debug:
msg: "{{ item.0 }} is no.{{ item.1 }}"
loop: "{{ dicts | dictsort }}"
结果:
[root@localhost cycle]# ansible-playbook cycle..yml
PLAY [cycle test2] **************************************************************************************
TASK [debug cycle dict2items] ***************************************************************************
ok: [192.168.15.10] => (item={'key': u'China', 'value': }) => {
"msg": "China is no.1"
}
ok: [192.168.15.10] => (item={'key': u'America', 'value': }) => {
"msg": "America is no.2"
}
ok: [192.168.15.10] => (item={'key': u'aaa', 'value': }) => {
"msg": "aaa is no.3"
}
ok: [192.168.15.10] => (item={'key': u'bbb', 'value': }) => {
"msg": "bbb is no.4"
}
ok: [192.168.15.10] => (item={'key': u'ccc', 'value': }) => {
"msg": "ccc is no.5"
}
TASK [debug cycle] **************************************************************************************
ok: [192.168.15.10] => (item=[u'aaa', ]) => {
"msg": "aaa is no.3"
}
ok: [192.168.15.10] => (item=[u'America', ]) => {
"msg": "America is no.2"
}
ok: [192.168.15.10] => (item=[u'bbb', ]) => {
"msg": "bbb is no.4"
}
ok: [192.168.15.10] => (item=[u'ccc', ]) => {
"msg": "ccc is no.5"
}
ok: [192.168.15.10] => (item=[u'China', ]) => {
"msg": "China is no.1"
}
PLAY RECAP **********************************************************************************************
192.168.15.10 : ok= changed= unreachable= failed= skipped=
debug cycle dict2items -------------------------------------------------- .26s
debug cycle ------------------------------------------------------------- .22s
Playbook finished: Mon Dec :: , total tasks. :: elapsed.
【Ansible】ansible循环的更多相关文章
- Ansible的循环
Ansible的循环 1. 前言 有可能在一个任务中,可能要做很多事情,例如创建多个用户,安装很多个包等,那么就有可能用到循环. 2. 标准循环 重复的任务可以用下面的方式: ...
- ansible Ansible Galaxy ansible-playbook 安装 使用 命令 笔记 生成密钥 管控机 被管控机 wget epel源
笔记 ansible 安装 与salt对比 相同 都是为了同时在多台机器上执行相同的命令 都是python开发 不同 agent(saltstack需要安装.ansible不需要) 配置(salt配置 ...
- Ansible Playbook 循环
Standard Loops 为了节省一些打字,重复的任务可以写成如下: - name: add several users user: name: "{{ item }}" st ...
- Ansible playbook循环实践总结<一>
1.标准Loops 标准loops可以直接减少task的次数,如下: [root@zero01 playbook]# vi loops.yaml --- - hosts: all gather_fac ...
- 自动运维:Ansible -ansible tower
文档主页:http://docs.ansible.com/参考文档:http://docs.ansible.com/ansible/参考文档:http://docs.ansible.com/ansib ...
- Ansible系列(六):循环和条件判断
本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...
- ansible笔记(14):循环(一)
在使用ansible的过程中,我们经常需要处理一些返回信息,而这些返回信息中,通常可能不是单独的一条返回信息,而是一个信息列表,如果我们想要循环的处理信息列表中的每一条信息,我们该怎么办呢?这样空口白 ...
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...
- Ansible详解(二)
Ansible系列命令 Ansible系列命令有如下: ansible:这个命令是日常工作中使用率非常高的命令之一,主要用于临时一次性操作: ansible-doc:是Ansible模块文档说明,针对 ...
- Ansible系列(五):playbook应用和roles自动化批量安装示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
随机推荐
- c++ 浅拷贝和深拷贝 指针和引用的区别 malloc(free)和new(delete)的区别 重载重写重定义
4.malloc(free)和new(delete)的区别 malloc()函数: 1.1 malloc的全称是memory allocation,中文叫动态内存分配. 原型:extern void ...
- Zookeeper入门(一)之概述
今天主要讲这么几个方面? 1.分布式应用: 2.什么是Zookeeper: 3.使用Zookkeeper有什么好处: ZooKeeper是一种分布式协调服务,用于管理大型主机.在分布式环境中协调和管理 ...
- 使用CSS3的“transition ”属性控制长宽度的缓慢变化
有时候我们可能会想要改变某个资源信息的长宽度,比如改变某个div的宽度,而且需要让这个宽度缓慢改变,而不是突然就改变了.这时候你可能会想到使用jquery的animate()函数,不过这个方法既得引用 ...
- D. Timetable
http://codeforces.com/problemset/problem/946/D Ivan is a student at Berland State University (BSU). ...
- CSU 1547: Rectangle (思维题加一点01背包)
1547: Rectangle Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: ...
- css3 学习图谱
摘要 最近看了一篇文章,建立最近的知识体系,对于工作了几年的大牛需要,但对学生学习同样也需要,我试着做了自己的css知识总图,类似目录,引导我的学习路线. 3-28日总结 根据该图谱,我学习得更有方向 ...
- jQuery----JQuery动画(hide()和show())(上)
hide()和show()方法,可以设置动画效果,本文对这两种方法效果加以说明. hide(参数1,参数2): 参数1:时间,单位为毫秒,表示对象隐藏所用的时间 参数2:回调函数,该函数在对象隐藏后触 ...
- 单片机实现简易版shell的方法和原理
Rt-thread 中有一个完整的finsh(shell )系统,使用串口做命令行输入输出.但是想要用这个炫酷的工具就必须要上rtthread系统,或者花大力气将其移植出来.于是我就自己写了一个类似于 ...
- JavaWeb基础—上传与下载
1.上传(不能使用BaseServlet): 上传的作用,略 上传的要求(对表单和Servlet都有要求): 1.必须使用表单,而不能是超链接,method="post" 文件明显 ...
- 补交20145226蓝墨云班课 -- MyCP
蓝墨云班课 -- MyCP.java 具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt ...