ansible playbooks loop循环
在一个task中循环某个操作
1、标准循环
- name: add several usersuser:name: "{{ item }}"state: presentgroups: "wheel"loop:- testuser1- testuser2#如果已经在变量文件中,定义了yaml列表,可以这么写loop: "{{ somelist }}"
- name: optimal yumyum:name: "{{list_of_packages}}"state: present- name: non optimal yum, not only slower but might cause issues with interdependenciesyum:name: "{{item}}"state: presentloop: "{{list_of_packages}}"
- name: add several usersuser:name: "{{ item.name }}"state: presentgroups: "{{ item.groups }}"loop:- { name: 'testuser1', groups: 'wheel' }- { name: 'testuser2', groups: 'root' }
2、复杂的循环
- name: give users access to multiple databasesmysql_user:name: "{{ item[0] }}"priv: "{{ item[1] }}.*:ALL"append_privs: yespassword: "foo"loop: "{{ query('nested', [ 'alice', 'bob' ], [ 'clientdb', 'employeedb', 'providerdb' ]) }}"
3、使用lookup 与 用loop查询
loop: "{{ query('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb']) }}"loop: "{{ lookup('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb'], wantlist=True) }}"
4、Do-Until循环
- shell: /usr/bin/fooregister: resultuntil: result.stdout.find("all systems go") != -1retries: 5delay: 10
5、使用loop中的register
- shell: "echo {{ item }}"loop:- "one"- "two"register: echo
{"changed": true,"msg": "All items completed","results": [{"changed": true,"cmd": "echo \"one\" ","delta": "0:00:00.003110","end": "2013-12-19 12:00:05.187153","invocation": {"module_args": "echo \"one\"","module_name": "shell"},"item": "one","rc": 0,"start": "2013-12-19 12:00:05.184043","stderr": "","stdout": "one"},{"changed": true,"cmd": "echo \"two\" ","delta": "0:00:00.002920","end": "2013-12-19 12:00:05.245502","invocation": {"module_args": "echo \"two\"","module_name": "shell"},"item": "two","rc": 0,"start": "2013-12-19 12:00:05.242582","stderr": "","stdout": "two"}]}
- name: Fail if return code is not 0fail:msg: "The command ({{ item.cmd }}) did not have a 0 return code"when: item.rc != 0loop: "{{ echo.results }}"
- shell: echo "{{ item }}"loop:- one- tworegister: echochanged_when: echo.stdout != "one"
6、循环inventory
# show all the hosts in the inventory- debug:msg: "{{ item }}"loop: "{{ groups['all'] }}"#show all the hosts in the current play- debug:msg: "{{ item }}"loop: "{{ ansible_play_batch }}"
# show all the hosts in the inventory- debug:msg: "{{ item }}"loop: "{{ query('inventory_hostnames', 'all') }}"# show all the hosts matching the pattern, ie all but the group www- debug:msg: "{{ item }}"loop: "{{ query('inventory_hostnames', 'all!www') }}"
7、循环控制
# main.yml- include: inner.yml- include_tasks: inner.ymlloop:- 1- 2- 3loop_control:loop_var: outer_item# inner.yml- debug:msg: "outer item={{ outer_item }} inner item={{ item }}"loop:- a- b- c
- name: create serversdigital_ocean:name: "{{ item.name }}"state: presentloop:- name: server1disks: 3gbram: 15Gbnetwork:nic01: 100Gbnic02: 10Gb...loop_control:label: "{{ item.name }}"
# main.yml- name: create servers, pause 3s before creating nextdigital_ocean:name: "{{ item }}"state: presentloop:- server1- server2loop_control:pause: 3
- name: count our fruitdebug:msg: "{{ item }} with index {{ my_idx }}"loop:- apple- banana- pearloop_control:index_var: my_idx
8、loops和 includes (2.0版本)
# main.yml- include_tasks: inner.ymlloop:- 1- 2- 3# inner.yml- set_fact:outer_item: "{{ item }}"- debug:msg: "outer item={{ outer_item }} inner item={{ item }}"loop:- a- b- cNote
ansible playbooks loop循环的更多相关文章
- 3、Ansible playbooks(Hosts、Users、tasks、handlers、变量、条件测试(when、迭代)、templates)
Ansible playbooks playbook是由一个或多个“play”组成的列表.play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色.从根本上来讲 ...
- ansible playbook loop 翻译
ansible源文档地址 有时候你想多次重复一个任务. 在计算机编程中,这叫做循环. 常见的 Ansible 循环包括使用文件模块更改几个文件和 / 或目录的所有权,使用用户模块创建多个用户,并重复一 ...
- MySQL中的while、repeat、loop循环
循环一般在存储过程和存储函数中使用频繁,这里只给出最简单的示例 while delimiter $$ create procedure test_while() begin declare sum i ...
- oracle for loop循环以及游标循环
1. for in loop形式 DECLARE CURSOR c_sal IS SELECT employee_id, first_name || last_name ename, salar ...
- Oracle PL/SQL之LOOP循环控制语句
在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...
- PL/SQL中LOOP循环控制语句
在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...
- Oracle loop循环无法插入数据
以下的测试基于scott用户下的emp表 首先用while循环进行测试,向emp表插入999条数据 declare i emp.empno; begin loop insert into emp(em ...
- [转载]Oracle PL/SQL之LOOP循环控制语句
在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...
- Oracle LOOP循环控制语句
在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...
随机推荐
- bzoj 1800: [Ahoi2009]fly 飞行棋【枚举】
在圆里所以没有平行四边形,n^4枚举点即可 #include<iostream> #include<cstdio> using namespace std; const int ...
- Linux学习之路2 Bash的基本操作
一.SHELL的介绍 shell分为两种:CLI(command Line Interface)和GUI(Graphical User Interface) 操作系统中的shell: GUI:GNOM ...
- 多维DP UVA 11552 Fewest Flop
题目传送门 /* 题意:将子符串分成k组,每组的字符顺序任意,问改变后的字符串最少有多少块 三维DP:可以知道,每一组的最少块是确定的,问题就在于组与组之间可能会合并块,总块数会-1. dp[i][j ...
- ACM_Hailstone HOTPO
Hailstone HOTPO Time Limit: 2000/1000ms (Java/Others) Problem Description: The hailstone sequence is ...
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...
- C#基础 for 穷举、迭代
//循环可以解决的问题类型 //穷举,把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. //单位给发了一张150元购物卡, //拿着到超市买三类洗化用品. //洗发水15元,香皂2元,牙 ...
- MacOS 下安装 MySQL8.0 登陆 MySQL
按照 官方教程 ,下载安装包,点击安装后,如需在命令行启动,还需设置命令路径: 在命令行中,打开配置文件 .bash_profile: vim ~/.bash_profile 在最后一行加上: PAT ...
- java excel poi导入 过滤空行的方法 判断是否是空行
private boolean isRowEmpty(Row row){ for (int c = row.getFirstCellNum(); c < row.getLastCellNum() ...
- Pycharm消除波浪线
PyCharm使用了较为严格的PEP8检查规则,稍微有点错误就会出现波浪线提示.那么怎么消除这些波浪线呢?一个简单粗暴的方法就是:在编辑器的右下角有个小人形状的按钮,点开之后有个滚动条,将滚动条滑动到 ...
- Windows 2008 防火墙开放端口
当我们使用新服务器架设新主机时,经常会遇到网站无法访问的情况,当问及客服时,经常会告知,操作系统默认不打开80端口,请先确定80是否打开并确定没有被占用.那么,我们该如何打开80端口呢? 方法/步骤 ...