标准循环

模式一
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
or
with_items: "{{ somelist }}"
模式2. 字典循环

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

嵌套循环

---
- name: test
hosts: masters
tasks:
- name: give users access to multiple databases
command: "echo name={{ item[0] }} priv={{ item[1] }} test={{ item[2] }}"
with_nested:
- [ 'alice', 'bob' ]
- [ 'clientdb', 'employeedb', 'providerdb' ]
- [ '1', '2', ]
result:
changed: [localhost] => (item=[u'alice', u'clientdb', u'1'])
changed: [localhost] => (item=[u'alice', u'clientdb', u'2'])
changed: [localhost] => (item=[u'alice', u'employeedb', u'1'])
changed: [localhost] => (item=[u'alice', u'employeedb', u'2'])
changed: [localhost] => (item=[u'alice', u'providerdb', u'1'])
changed: [localhost] => (item=[u'alice', u'providerdb', u'2'])
changed: [localhost] => (item=[u'bob', u'clientdb', u'1'])
changed: [localhost] => (item=[u'bob', u'clientdb', u'2'])
changed: [localhost] => (item=[u'bob', u'employeedb', u'1'])
changed: [localhost] => (item=[u'bob', u'employeedb', u'2'])
changed: [localhost] => (item=[u'bob', u'providerdb', u'1'])
changed: [localhost] => (item=[u'bob', u'providerdb', u'2'])

 字典循环(with_dict)

假设字典如下
---
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210 可以访问的变量
tasks:
- name: Print phone records
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"

文件循环(with_file, with_fileglob)

  with_file 是将每个文件的文件内容作为item的值

  with_fileglob 是将每个文件的全路径作为item的值, 在文件目录下是非递归的, 如果是在role里面应用改循环, 默认路径是roles/role_name/files_directory

例如:
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
with_fileglob:
- /playbooks/files/fooapp/*

with_together

  tasks:
- command: echo "msg={{ item.0 }} and {{ item.1 }}"
with_together:
- [ 1, 2, 3 ]
- [ 4, 5 ] result:
changed: [localhost] => (item=[1, 4])
changed: [localhost] => (item=[2, 5])
changed: [localhost] => (item=[3, None])

子元素循环(with_subelements)

  with_subelements 有点类似与嵌套循环, 只不过第一个参数是个dict, 第二个参数是dict下的一个子项.

整数序列(with_sequence)

  with_sequence 产生一个递增的整数序列,

---
- hosts: all tasks: # create groups
- group: name=evens state=present
- group: name=odds state=present # create some test users
- user: name={{ item }} state=present groups=evens
with_sequence: start=0 end=32 format=testuser%02x # create a series of directories with even numbers for some reason
- file: dest=/var/stuff/{{ item }} state=directory
with_sequence: start=4 end=16 stride=2 # a simpler way to use the sequence plugin
# create 4 groups
- group: name=group{{ item }} state=present
with_sequence: count=4

随机选择(with_random_choice)

  with_random_choice:在提供的list中随机选择一个值

Do-util

- action: shell /usr/bin/foo
register: result
until: result.stdout.find("all systems go") != -1
retries: 5
delay: 10

第一个文件匹配(with_first_found)

- name: some configuration template
template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root
with_first_found:
- files:
- "{{ inventory_hostname }}/etc/file.cfg"
paths:
- ../../../templates.overwrites
- ../../../templates
- files:
- etc/file.cfg
paths:
- templates

循环一个执行结果(with_lines)

---
- name: test
hosts: all
tasks:
- name: Example of looping over a command result
shell: touch /$HOME/{{ item }}
with_lines: /usr/bin/cat /home/fg/test

with_lines 中的命令永远都是在controller的host上运行, 只有shell命令才会在inventory中指定的机器上运行

带序列号的list循环(with_indexed_items)

ini 文件循环(with_ini)

[section1]
value1=section1/value1
value2=section1/value2 [section2]
value1=section2/value1
value2=section2/value2
Here is an example of using with_ini: - debug: msg="{{ item }}"
with_ini: value[1-2] section=section1 file=lookup.ini re=true

flatten循环(with_flattened)

---
- name: test
hosts: all
tasks:
- name: Example of looping over a command result
shell: echo {{ item }}
with_flattened:
- [1, 2, 3]
- [[3,4 ]]
- [ ['red-package'], ['blue-package']] :result changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
changed: [localhost] => (item=3)
changed: [localhost] => (item=3)
changed: [localhost] => (item=4)
changed: [localhost] => (item=red-package)
changed: [localhost] => (item=blue-package)

register循环

- shell: echo "{{ item }}"
with_items:
- one
- two
register: echo

变量echo是一个字典, 字典中result是一个list, list中包含了每一个item的执行结果

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

条件判断

  ansible的条件判断非常简单关键字是when, 有两种方式

    1. python语法支持的原生态格式 conditions> 1 or conditions == "ss",   in, not 等等

              2. ;ansible Jinja2 “filters”

tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result|failed
- command: /bin/something_else
when: result|succeeded
- command: /bin/still/something_else
when: result|skipped tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
when: foo is defined - fail: msg="Bailing out. this play requires 'bar'"
when: bar is undefined

条件判断可以个loop role 和include一起混用

#when 和 循环
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5 #when和include
- include: tasks/sometasks.yml
when: "'reticulating splines' in output" #when 和角色
- hosts: webservers
roles:
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }

YAML_18 ansible 判断和循环的更多相关文章

  1. ansible 判断和循环

    标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...

  2. python之--条件判断和循环

    Python之判断 和其他语言一样,python同样具有条件判断和循环的操作,比如我们可以编写一个简单的判断操作:使用if关键字可以达到判断的效果,如下例: >>> test_if ...

  3. python学习第六天 条件判断和循环

    总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...

  4. 初学Java scirpt(判断、循环语句)

    在编写代码时,我们经常需要为不同的判断结果来执行不同的动作以及需要反复执行同一段代码,这时我们就需要使用判断和循环语句来实现. 1.判断语句(if) 判断语句经常用的有(if......else).( ...

  5. python入门(11)条件判断和循环

    python入门(11)条件判断和循环 条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: ag ...

  6. Python学习之条件判断和循环

    #coding= utf-8 # 条件判断和循环 # 如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做 age1 = 20 if age1 >= 18: prin ...

  7. Python第四天 流程控制 if else条件判断 for循环 while循环

    Python第四天   流程控制   if else条件判断   for循环 while循环 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Python第二天 ...

  8. 【转】shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    [转]shell编程下 特殊变量.test / [ ]判断.循环.脚本排错 第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~] ...

  9. javascript中的分支判断与循环

    分支判断与循环 分支结构 单一选择结构(if) 二路选择结构(if/else) 内联三元运算符 ?: 多路选择结构(switch) var condition = true; if (conditio ...

随机推荐

  1. 成员函数内定义static变量(不安全,各对象之间共享)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u012317833/article/de ...

  2. 生物网络,RNA 与疾病关联分析

    题目: 大数据时代下基于网络算法和机器学习的非编码RNA 相关预测研究摘要:最近越来越多的生物实验表明非编码RNA 具有非常重要的生物学功能,参与细胞中的多项重要生命活动,调控许多基本且重要的生物过程 ...

  3. Layui学习笔记(一)—— 关于模块的扩展

    在使用layui的时候,总有官方自带模块不够用想自己扩展的时候,这时候我们就需要扩展模块了. 模块扩展有两种: (一)普通地扩展 layui.define( function (exports) { ...

  4. CentOS 7 - 里面如何以root身份使用图形界面管理文件?

    nautilus 是gnome的文件管理器,但是如果不是root账号下,权限受限,我们可以通过以下方式以root权限使用! 启动shll,随后在shell里面输入下面命令: sudo nautilus

  5. 服务网关ZuulFilter过滤器--pre/post/error的用法(校验请求信息,获取路由后的请求/响应信息,处理服务网关异常)

    微服务中Zuul服务网关一共定义了四种类型的过滤器: pre:在请求被路由(转发)之前调用 route:在路由(请求)转发时被调用 error:服务网关发生异常时被调用 post:在路由(转发)请求后 ...

  6. SpringBoot学习之@Configuration注解和@Bean注解

    @Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...

  7. 浅谈 form 表单提交

    原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7675230.html 若有错误,请评论指出,谢谢! Form 对象代表一个 HTML 表单.在 HTML ...

  8. python(函数调用)

    1.在原文件中调用 def abc(x,y): print x + y abc(2,3) #直接通过函数名加括号进行调用传参 2.同一个包(package)下面调用不同文件中的函数 "&qu ...

  9. elk使用不足及弥补报警措施

    全部都是6.6.2版本,就这还是没有敢选太新的 场景:每个收集点部署filebeat收集响应日志,然后发送到logstash,logstash发送到elasticsearch,和file,这里插一句, ...

  10. springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5

    近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是 使用 redisTemplate 或 ...