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 ...
随机推荐
- Linux下 FACL详解
1. 什么是ACL ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置 ACL可以针 ...
- bzoj 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏【Floyd】
先跑一遍取max版的Floyd,直接用数组回答询问即可 #include<iostream> #include<cstdio> using namespace std; con ...
- C#命名空间 using的用法
using的用法: 1. using指令:引入命名空间 这是最常见的用法,例如: using System; using Namespace1.SubNameSpace; 2. using stati ...
- 莫队算法/二分查找 FZU 2072 Count
题目传送门 题意:问区间内x的出现的次数分析:莫队算法:用一个cnt记录x的次数就可以了.还有二分查找的方法 代码: #include <cstdio> #include <algo ...
- WebSphere Application Server切换JAVA SDK版本
最近在Windows Server 2008 R2服务器中搭建了一套IHS+WAS8.5集群环境,测试一个简单的demo应用没有问题,可是在部署正式应用时总是报类版本错误.换了好几个JDK对项目进行编 ...
- ASP.NET MVC应用程序中支持用户使用腾讯QQ和微信以及新浪微博的第三方登录
什么是第三方授权登录,就是一些大家都会有的帐号如QQ.微信.淘宝.微博等账户.通过那些巨头公司提供的api直接实现登录. 当然,我们是不可能得到你的用户名和密码的.不了解的人,可能会存在这个疑虑.我们 ...
- BST二叉查找树转双向链表DoubleLinke
问题:在不创建任何新的节点的情况下,实现将一颗BST变成有序的双向链表. 分析: 在结构上,如图的一颗BST,每个节点都有left right指针分别指指向左右儿子.结构上和双向链表节点是完全相同的. ...
- from scipy import spatial 出现 from .qhull import * ImportError: DLL load failed: The specified module could not be found. 错误
错误描述: 本人机器window8.1 64位,python2.7. Traceback (most recent call last): File "C:/Users/Hamid/Docu ...
- firefox浏览器中 bootstrap 静态弹出框中select下拉框不能弹出(解决方案)
问题出现场景1: 在firefox浏览器中在bootstrap弹出的modal静态框中再次弹出一个静态框时 select下拉框不能弹出选项 解决方案:去掉最外层静态框的 tabindex=" ...
- 【Python-2.7】换行符和制表符
在Python中换行符“\n”表示接下来的内容将会换到下一行显示,制表符“\t”表示下面的内容显示时在前面留出空白,如打印如下内容: Dear: I love you forever! 上面的一段话分 ...