ansible_playbook语法中的循环语句归纳
种类一、标准循环
添加多个用户
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
添加多个用户,并将用户加入不同的组内。
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }
种类二、锚点嵌套循环
分别给用户授予3个数据库的所有权限
- name: give users access to multiple databases
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
with_nested:
- [ 'alice', 'bob' ]
- [ 'clientdb', 'employeedb', 'providerdb' ]
种类三、锚点遍历字典
输出用户的姓名和电话
tasks:
- name: Print phone records
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: {'alice':{'name':'Alice Appleworth', 'telephone':'123-456-789'},'bob':{'name':'Bob Bananarama', 'telephone':'987-654-3210'} }
种类四、锚点并行遍历列表
tasks:
- debug: "msg={{ item.0 }} and {{ item.1 }}"
with_together:
- [ 'a', 'b', 'c', 'd','e' ]
- [ 1, 2, 3, 4 ]
如果列表数目不匹配,用None补全
种类五、锚点遍历列表和索引
- name: indexed loop demo
debug: "msg='at array position {{ item.0 }} there is a value {{ item.1 }}'"
with_indexed_items: [1,2,3,4]
item.0 为索引,item.1为值
种类六、锚点遍历文件列表的内容
---
- hosts: all
tasks:
- debug: "msg={{ item }}"
with_file:
- first_example_file
- second_example_file
种类七、锚点遍历目录文件
with_fileglob匹配单个目录中的所有文件,非递归匹配模式。
---
- hosts: all
tasks:
- file: dest=/etc/fooapp state=directory
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
with_fileglob:
- /playbooks/files/fooapp/*
当在role中使用with_fileglob的相对路径时,Ansible解析相对于roles/<rolename>/files目录的路径。
种类八、锚点遍历ini文件
lookup.ini
[section1]
value1=section1/value1
value2=section1/value2
[section2]
value1=section2/value1
value2=section2/value2
- debug: msg="{{ item }}"
with_ini: value[1-2] section=section1 file=lookup.ini re=true
获取section1 里的value1和value2的值
种类九、锚点重试循环 until
- action: shell /usr/bin/foo
register: result
until: result.stdout.find("all systems go") != -1
retries: 5
delay: 10
"重试次数retries" 的默认值为3,"delay"为5。
锚点查找第一个匹配文件
tasks:
- debug: "msg={{ item }}"
with_first_found:
- "/tmp/a"
- "/tmp/b"
- "/tmp/default.conf"
依次寻找列表中的文件,找到就返回。如果列表中的文件都找不到,任务会报错。
种类十、锚点随机选择with_random_choice
随机选择列表中得一个值
- debug: msg={{ item }}
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
循环程序的结果
tasks:
- debug: "msg={{ item }}"
with_lines: ps aux
种类十一、锚点循环子元素
定义好变量
#varfile
---
users:
- name: alice
authorized:
- /tmp/alice/onekey.pub
- /tmp/alice/twokey.pub
mysql:
password: mysql-password
hosts:
- "%"
- "127.0.0.1"
- "::1"
- "localhost"
privs:
- "*.*:SELECT"
- "DB1.*:ALL"
- name: bob
authorized:
- /tmp/bob/id_rsa.pub
mysql:
password: other-mysql-password
hosts:
- "db1"
privs:
- "*.*:SELECT"
- "DB2.*:ALL"
---
- hosts: web
vars_files: varfile
tasks:
- user: name={{ item.name }} state=present generate_ssh_key=yes
with_items: "{{ users }}"
- authorized_key: "user={{ item.0.name }} key='{{ lookup('file', item.1) }}'"
with_subelements:
- "{{ users }}"
- authorized
- name: Setup MySQL users
mysql_user: name={{ item.0.name }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join('/') }}
with_subelements:
- "{{ users }}"
- mysql.hosts
{{ lookup('file', item.1) }} 是查看item.1文件的内容
with_subelements 遍历哈希列表,然后遍历列表中的给定(嵌套)的键。
种类十二、锚点在序列中循环with_sequence
with_sequence以递增的数字顺序生成项序列。 您可以指定开始,结束和可选步骤值。 参数应在key = value对中指定。 'format'是一个printf风格字符串。
数字值可以以十进制,十六进制(0x3f8)或八进制(0600)指定。 不支持负数。
---
- hosts: all
tasks:
# 创建组
- group: name=evens state=present
- group: name=odds state=present
# 创建格式为testuser%02x 的0-32 序列的用户
- user: name={{ item }} state=present groups=evens
with_sequence: start= end= format=testuser%02x
# 创建4-16之间得偶数命名的文件
- file: dest=/var/stuff/{{ item }} state=directory
with_sequence: start= end= stride=
# 简单实用序列的方法:创建4 个用户组分表是组group1 group2 group3 group4
- group: name=group{{ item }} state=present
with_sequence: count=4
种类十三、锚点随机选择with_random_choice
随机选择列表中得一个值
- debug: msg={{ item }}
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
合并列表
安装所有列表中的软件
- name: flattened loop demo
yum: name={{ item }} state=installed
with_flattened:
- [ 'foo-package', 'bar-package' ]
- [ ['one-package', 'two-package' ]]
- [ ['red-package'], ['blue-package']]
注册变量使用循环
- shell: echo "{{ item }}"
with_items:
- one
- two
register: echo
- name: Fail if return code is not 0
fail:
msg: "The command ({{ item.cmd }}) did not have a 0 return code"
when: item.rc != 0
with_items: "{{ echo.results }}"
循环主机清单
输出所有主机清单里的主机
- debug: msg={{ item }}
with_items: "{{ groups['all'] }}"
输出所有执行的主机
- debug: msg={{ item }}
with_items: play_hosts
输出所有主机清单里的主机
- debug: msg={{ item }}
with_inventory_hostnames: all
输出主机清单中不在www中的所有主机
- debug: msg={{ item }}
with_inventory_hostnames: all:!www
改变循环的变量项
# main.yml
- include: inner.yml
with_items:
- 1
- 2
- 3
loop_control:
loop_var: outer_item
# inner.yml
- debug: msg="outer item={{ outer_item }} inner item={{ item }}"
with_items:
- a
- b
- c
作者:Jeson老师
链接:https://www.imooc.com/article/22753
来源:慕课网
ansible_playbook语法中的循环语句归纳的更多相关文章
- js中的循环语句
js中的循环语句可分为三种:1.while:2.do……while:3.for. while的语法为 while (exp) { //statements;} var a=1,b=0; whil ...
- shell脚本中select循环语句用法
shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...
- 详解Python中的循环语句的用法
一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...
- Shell编程-08-Shell中的循环语句
目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结 循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...
- JavaScrip中的循环语句
循环语句 循环语句,也是流程控制语句中不可或缺的一种结构.在 JavaScrip中实现循环的方式有好几个一个来看 1.为什么需要循环 在具体介绍 Javascript中的循环之前,首先我们来明确一个问 ...
- Java中的循环语句
1.1 while 循环语句 while 语句也称为条件判断语句. 循环方式 : 利用一个条件来控制是否要反复执行这个语句. 语法 : while(条件表达式){ 执行语句 } 当条件表达式的返回值为 ...
- Swift中的循环语句
循环语句能够使程序代码重复执行.Swift编程语言支持4种循环构造类型:while.do while.for和for in.for和while循环是在执行循环体之前测试循环条件,而do while是在 ...
- shell编程中的循环语句
while循环直接从文件中读取 while read line do command done < filename until循环 until 条件 do command done for循环 ...
- 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句
所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...
随机推荐
- inputAccessoryView,inputView
我们在使用UITextView和UITextField的时候,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘. 但是当我 ...
- 二次封装arcgis的timeslider
arcgis的timeslider是对dojo slider二次封装,项目需要,所有Map用统一样式的slider,所以写了一个common的dojo class,统一调用生成slider,作为对ti ...
- 慕课-tooltip提示框总结
在慕课上学Waynej老师的tooltip浮动提示框,老师每次讲课都会强调搬砖的流程,这点在上了老师的几节课后宝宝终于体会到了: 分析→设计→编码→优化 分析:分析该功能到底是怎样的,其实就是需求分析 ...
- VSphere随笔 - vCenter6.5安装配置手册
一.前期准备: 1.物理机准备 一台已安装了ESXI虚拟化系统的机器: 2.DNS服务器准备 新建一台DNS服务器,添加vcenter的双向解析. (1)安装一台win2008的机器 (2)开启DNS ...
- [Flask] 异步非阻塞IO实现
Flask默认是不支持非阻塞IO的,表现为: 当 请求1未完成之前,请求2是需要等待处理状态,效率非常低. 在flask中非阻塞实现可以由2种: 启用flask多线程机制 # Flask from f ...
- 购物车1.0版——python第5天
# 输出商品列表,用户输入序号,显示选中商品名称# 商品li = ['手机', '电脑', '耳机', '键盘', '鼠标']# 要求:1.页面显示序号+商品名称如下# 1 手机# 2 电脑# 3 耳 ...
- 洛谷 P1346 电车——dijstra
上一波题目 https://www.luogu.org/problem/P1346 是道水题 路口一开始对着的那条路权值为0 其余路权值为1 然后跑一遍最短路就好了 qwq #include<c ...
- luoguP1314 聪明的质监员 题解(NOIP2011)
P1314 聪明的质监员 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include< ...
- go module管理依赖包
go mod 最大的好处就是摆脱了GOPATH这个限制,在除了GOPATH以外的目录下也能开展你的项目 go mod使用: 1,确保你的go版本是1.1以上 2,创建一个项目目录example,并添加 ...
- js 屏蔽网页快捷键代码
<script> function KeyDown(){ //屏蔽鼠标右键.Ctrl+n.shift+F10.F5刷新.退格键 //alert("ASCII代码是:"+ ...