标准循环

模式一
- name: add several usersuser: name={{ item }} state=present groups=wheelwith_items:- testuser1- testuser2  orwith_items: "{{ somelist }}"

模式2. 字典循环- name: add several usersuser: name={{ item.name }} state=present groups={{ item.groups }}with_items:- { name: 'testuser1', groups: 'wheel' }- { name: 'testuser2', groups: 'root' }

嵌套循环

---
- name: testhosts: masterstasks:- name: give users access to multiple databasescommand: "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 Appleworthtelephone: 123-456-7890bob:name: Bob Bananaramatelephone: 987-654-3210可以访问的变量
tasks:- name: Print phone recordsdebug: 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=600with_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: alltasks:# create groups- group: name=evens state=present- group: name=odds state=present# create some test users- user: name={{ item }} state=present groups=evenswith_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=directorywith_sequence: start=4 end=16 stride=2# a simpler way to use the sequence plugin# create 4 groups- group: name=group{{ item }} state=presentwith_sequence: count=4

随机选择(with_random_choice)

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

Do-util

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

第一个文件匹配(with_first_found)

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

循环一个执行结果(with_lines)

---
- name: testhosts: alltasks:- name: Example of looping over a command resultshell: 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: testhosts: all tasks:- name: Example of looping over a command resultshell:  echo {{ item }}with_flattened: - [1, 2, 3]- [[3,4 ]]- [ ['red-package'], ['blue-package']]:resultchanged: [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- tworegister: 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/falseregister: resultignore_errors: True- command: /bin/somethingwhen: result|failed- command: /bin/something_elsewhen: result|succeeded- command: /bin/still/something_elsewhen: result|skippedtasks:- 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.ymlwhen: "'reticulating splines' in output"#when 和角色
- hosts: webserversroles:- { role: debian_stock_config, when: ansible_os_family == 'Debian' }

转载于:https://www.cnblogs.com/wangjq19920210/p/9104668.html

ansible 判断和循环相关推荐

  1. smali to java_Smali —— 数学运算,条件判断,循环

    通过上一篇 Smali 语法解析--Hello World 的学习,了解了 Smali 文件的基本格式.这一篇从最基本的数学运算,条件判断,循环等开始,更加详细的了解 Smali 语法. 数学运算 加 ...

  2. Python时间序列模型推理预测实战:时序推理数据预处理(特征生成、lstm输入结构组织)、模型加载、模型预测结果保存、条件判断模型循环运行

    Python时间序列模型推理预测实战:时序推理数据预处理(特征生成.lstm输入结构组织).模型加载.模型预测结果保存.条件判断模型循环运行 目录

  3. python重复元素判定编程_从零开始学Python编程四:条件判断与循环

    前面已经介绍了不少Python基础知识,大家不要觉得不耐烦,想要学好Python,做好Python开发,一定要打牢基础.大家也发现了,Python中很多基础知识和数学算法是一样的,比如今天要教给大家的 ...

  4. Java判断语句 循环语句

    主题: 判断语句 循环语句 一.判断语句 1.单分支 1 if(条件){ 2 3 //此处插入代码,表示if中的条件成立执行的语句 4 } 注意: 1.条件 必须 boolean 2.if只带 一行代 ...

  5. Sass学习笔记 -- 初步了解函数、运算、条件判断及循环

    函数 sass定义了很多函数可供使用,当然你也可以自己定义函数,以@fuction开始.sass的官方函数链接为:sass fuction,实际项目中我们使用最多的应该是颜色函数,而颜色函数中又以li ...

  6. 打开高效文本编辑之门_Linux Awk之条件判断与循环

    Linux awk之条件判断与循环 声明与简介 AWK 是一种处理文本文件的语言,是一个强大的文本分析工具.awk通常用来处理结构化(固定格式)的文本文件, awk每接收文件的一行,然后执行相应的命令 ...

  7. linux循环条件,shell脚本编写 之 条件选择,条件判断,循环语句

    1 概述 编写shell脚本,一般离不开条件选择,条件判断以及循环语句.掌握这三个语法,将大大提高脚本的编写效率,使得脚本编写更加灵活,完成X相对复杂的工作 2 条件选择if语句 if语句选择执行,逐 ...

  8. python条件输出_python基础(二)条件判断、循环、格式化输出

    继续上一篇,今天主要总结一下条件判断.循环.格式化输出 一.条件判断 python中条件判断使用if else来判断,多分支的话使用if elif ... else,也就是如果怎么怎么样就怎么怎么样, ...

  9. python指定条件分类输出_python基础(二)条件判断、循环、格式化输出

    继续上一篇,今天主要总结一下条件判断.循环.格式化输出 一.条件判断 python中条件判断使用if else来判断,多分支的话使用if elif ... else,也就是如果怎么怎么样就怎么怎么样, ...

最新文章

  1. Android开发实践:如何设置NDK的编译选项
  2. 联手IBM布局云计算,王健林如何再造一个新万达?
  3. BCH接下来如何走?且看这场大会传达了什么思想
  4. Tomcat服务器的常用配置
  5. leetcode算法题--1~n整数中1出现的次数
  6. Elicpse创建Maven项目
  7. android html邮件 messagecompose,android email 转发附件丢失问题
  8. AtCoder Beginner Contest 137 解题报告(A ~ E)
  9. 修改R语言安装包的默认路径 r包安装位置
  10. SSD1306 OLED驱动芯片 详细介绍
  11. ajax get提交中文参数乱码
  12. 最强大脑记忆曲线(2)——创建数据库
  13. EBS功能_Oracle ERP系统借贷关系
  14. PAT1003 我要通过! (20 分)(C语言)
  15. Java学习day07-Java高级编程-多线程
  16. Sprite Renderer
  17. SpringBoot多环境开发
  18. 苹果用计算机知道密码,苹果电脑钥匙串登录密码忘了怎么办
  19. Unity-3d小游戏开发-----走迷宫
  20. Java中的CAS(compare and swap)

热门文章

  1. 使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容
  2. C#,pdf文件转换成图片文件。
  3. 菜鸟的jQuery源码学习笔记(三)
  4. 【unity3d游戏开发之基础篇】利用射线实现鼠标控制角色转向和移动(角色移动一)...
  5. [转]C++的坑真的多吗?
  6. 前端学习总结——CSS布局方式之传统布局
  7. 华为交换机同一vlan不同网段的通信
  8. 填坑-十万个为什么?(13)
  9. abstract class 和 interface 区别
  10. Win系统利用本地安全策略全面禁止360等软件的安装与运行-1