普通循环

with_itemes  变量组

  { item }  循环变量

示例

- name: 覆盖配置文件
copy: src=/root/{{ item }} dest=/root/test/{{ item }}
with_items:
- a.txt
- b.txt
- c.txt
- d.txt
- shell (目录)
##会依次将abcd 四个txt文件和 shell目录拷贝到目标文件夹下

引用sc的一个示例:

字典循环

#添加用户

- name: add users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2 - name: add users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'testuser1', groups: 'test1' }
- { name: 'testuser2', groups: 'test2' }

示例二

users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:              #key
name: Bob Bananarama    #value1
telephone: 987-654-3210   #value2 tasks:
- name: Print records
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"

示例三

执行结果

嵌套循环

---
- name: test
hosts: test
tasks:
- name: test
command: "echo name={{ item[0] }} priv={{ item[1] }}"
with_nested:
- [ 'alice', 'bob' ]
- [ 'clientdb', 'employeedb', 'providerdb' ] ##item[0]是循环的第一个列表的值['alice','bob'];item[1]是第二个列表的值。

打印结果:

文件循环

inventory循环(with_inventory_hostnames)

# show all the hosts in the inventory
- debug: msg={{ item }}
with_inventory_hostnames: all # show all the hosts matching the pattern, ie all but the group www
- debug: msg={{ item }}
with_inventory_hostnames: all:!www

with_fileglob:

---
- hosts: test
gather_facts: False
tasks:
- name: display value    ##列举某个目录下的某些文件
debug: msg="file is {{ item }}"
with_fileglob:
- /root/*.zip

条件判断when与register变量

register官网示例

register 可接受多个task的结果作为变量临时存储,示例:

---
- hosts: test
gather_facts: False
tasks:
- name: display value
shell: "{{ item }}"
with_items:
- hostname
- uname
register: ret
- name: display loops
debug: msg="{% for i in ret.results %} {{ i.stdout }} {% endfor %}"

 执行结果:

when 官网示例

其它示例:

判断分支是否在git分支列表,如果不在,运行某个脚本

- command: chdir=/path/to/project  git branch
register: git_branches - command: /path/to/project/git-clean.sh
when: "('release-v2' no in git_branches.stdout)"

判断某个文件是否存在

- stat: path=/etc/hosts
register: host_file - copy: src=/path/to/local/file dest=/path/to/remote/file
when: host_file.stat.exists == false

其它示例

changed_when && failed_when

对命令运行结果进行判断。

示例

比如判断某个软件是否安装成功

- name: install git
yum: name=git state=latest
register: git_install changed_when: " 'nothing to install or upgrade' not in git_install.stdout"    #表示输出的结果中没有 notthing to install ..才返回change结果

ignore_errors: true 屏蔽错误信息

ansible 循环与条件判断when的更多相关文章

  1. python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器

    python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 1. 创建一个 zqxt_tm ...

  2. Ansible系列(六):循环和条件判断

    本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  3. PHP和JS在循环、条件判断中的不同之处

    一.条件判断: php中算  false 的情况 1. boolean:false 2. 整形:0 3.浮点型:0 4.字符串:"" "0"(其他都对) 5.空 ...

  4. Java 控制语句:循环、条件判断

    基础很重要,基础很重要,基础很重要.重要的事情说三遍,. 程序设计中的控制语句主要有三种:顺序.分支和循环.我们每天写的代码,除了业务相关,里面会包含大量的控制语句.但是控制语句的基本使用,是否有些坑 ...

  5. Ansible Playbook 使用条件判断语句

    先介绍一下 gather_facts 参数,该参数用于指定在执行任务前,是否先执行 setup 模块获取主机相关信息,以便给后面的任务使用 [root@localhost ~]# ansible 19 ...

  6. Go语言之循环与条件判断

    一.for循环 Go 语言中没有 while 循环,只有一个 for 循环 for 变量初始化;条件;变量自增/自减 { 循环体内容 } 1.基本使用 for i := 0; i < 10; i ...

  7. ansible实践3-playbook条件判断

    --- - hosts: testhost   user: root   gather_facts: True   tasks:     - name: use when       shell: t ...

  8. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

  9. shell条件判断与流程控制

    一 条件判断式语句 1.按文件类型进行判断 测试类型 作用 -b 文件 判断文件是否存在,并且是否为块设备文件(是块设备文件为真) -c 文件 判断文件是否存在,并且是否为字符设备文件(是字符设备设备 ...

随机推荐

  1. 《Linux内核设计与分析》第十七章读书笔记

    设备与模块 关于设备驱动和设备管理,四种内核成分. 设备类型:在所有Unix 系统中为了统一普通设备的操作所采用的分类. 模块: Linux 内核中用于按需加载和卸载目标码的机制. 内核对象:内核数据 ...

  2. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  3. 数据驱动测试之—— Excel+TestNG

    对于利用Webdriver做自动化的童鞋,对于如何将元素或者输入数据如何和编码分离都应该不会太陌生,本着一边学习一边分享的心态,大概总结了一下关于利用CSV.XML以及Excel来存放数据,然后在结合 ...

  4. Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析

    Hashtable 是一个很常见的数据结构类型,前段时间阿里的面试官说只要搞懂了HashTable,hashMap,HashSet,treeMap,treeSet这几个数据结构,阿里的数据结构面试没问 ...

  5. PHP5.5特性

    1.PHP生成器(使用yield关键字) <?php //使用yield关键字实现平方的生成器,在循环结构中则生成的是数组 function do2pos($n){ for($i=1; $i&l ...

  6. windows常见数据类型

    一,常见数据类型 WORD:                16位无符号整形数据 DWORD:             32位无符号整型数据(DWORD32) DWORD64:         64位 ...

  7. Educational Codeforces Round 58 Div. 2 自闭记

    明明多个几秒就能场上AK了.自闭. A:签到. #include<iostream> #include<cstdio> #include<cmath> #inclu ...

  8. So you want to be a 2n-aire? UVA - 10900(概率)

    题意: 初始值为1, 每次回答一个问题,如果答对初始值乘2,答错归0,结束,一共有n个问题,求在最优的策略下,最后值的期望值 解析: 注意题中的一句话  每个问题的答对概率在t和1之间均匀分布  也就 ...

  9. hdu 4897 Little Devil I (树链剖分+线段树)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...

  10. 【ZOJ2277】The Gate to Freedom

    BUPT2017 wintertraining(16) #4 E ZOJ - 2277 题意 输出\(n^n\)的首位的数字. 题解 用科学计数法表示\(n^n=k\cdot 10^b\),那么\(n ...