ansible 判断和循环

标准循环

复制代码
模式一
- name: add several usersuser: name={{ item }} state=present groups=wheelwith_items:- testuser1- testuser2
or
with_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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266990.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows XP中快速识别真假SVCHOST.EXE

SVCHOST.EXE是基于NT核心技术的操作系统非常重要的进程,它提供许多系统服务,比如远程过程调用系统服务 (RPCSS)、动态主机配置协议(DPCH) 服务等与网络相关的服务。现在广大计算机用户普遍使用的Windows XP、Windows 2003等操作系统都涉及该进…

php require 500,thinkphp5出现500错误怎么办

thinkphp5出现500错误,如下图所示:require(): open_basedir restriction in effect. File(/home/wwwroot/pic/thinkphp/start.php) is not within the allowed解决方法:1、我是lnmp1.4 php5.6,php.ini里面的open_basedir 是注释掉…

eclipse从svn导入maven项目变成普通项目解决办法

右击项目-->configure-->Convert to Maven Project转载于:https://www.cnblogs.com/zhanzhuang/p/9105463.html

php安装soap扩展

例: 1、编译安装 解压对应php版本安装包 进入解压后的ext目录 phpize --生成configure文件,报以下错误,可以忽略 ------------------------------------------------------------------------------ Configuring for: PHP Api Version: …

php yaf 教程,Yaf教程2:入门使用

接下来我们通过最简单的“hello yaf”例子说明 Yaf 的用法,然后一步步走向更加复杂的用法。Yaf的PHP官方手册位置是:http://php.net/manual/zh/book.yaf.php,这比鸟哥(Yaf作者)自己博客 http://www.laruence.com/manual/的文档要新&#xff0…

在移动端项目中使用vconsole

1.先在项目中引入 <script type"text/javascript" src"vconsole.min.js"> 2.初始化vConsole window.vConsole new window.VConsole({defaultPlugins: [system, network, element, storage], // 可以在此设定要默认加载的面板maxLogNumber: 1000 });…

如何创建路径别名

在访问页面时&#xff0c;页面地址会以 DocumentRoot所指定的路径为相对路径&#xff0c;但若不想使用指定的路径&#xff0c;则需要创建路径别名。假如DocumentRoot为/var/www/html &#xff0c;现想将/var/www/html/mail 建立别名/web/mail&#xff0c;该如何修改呢&#xff…

matlab矩阵除以一个数字,matlab矩阵中每一行数除以一个数 | 学步园

例如&#xff1a;用a中每一行数除以x中相对应的每一个数x[5 10 6 8 16 6 8 8 22 11];a[4 4 4 5 4 4 4 4 3 46 8 6 2 6 8 8 6 8 64 4 4 4 6 4 4 4 6 44 6 6 4 6 6 6 4 7 410 14 14 10 12 12 12 10 14 123 5 5 3 6 3 3 4 5 44 6 7 4 4 4 4 4 6 64 6 6 6 5 6 5 5 7 613 16 19 16 1…

Redis(四):Spring + JedisCluster操作Redis(集群)

1.maven依赖&#xff1a; <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> 2.增加spring 配置 <!-- JedisCluster配置 --> <bean …

js对象数组转java对象数组对象数组对象数组对象,前台js数组json字符串,后台json转为对象数组的具体实现...

$("#savaUserSet").click(function(){var JSONArr[];$("i[nameeventName]").each(function() {//获取所有name属性为eventName的i标签,并遍历if(!($(this).hasClass("active"))){var eventCode$(this).attr("id");var eventName$(this…

matlab绘制烟花,[原创]利用MATLAB燃放烟花(礼花)

function firework% 烟花烟花满天飞% CopyRight&#xff1a;xiezhh(谢中华)% 2011.6.25OldHandle findobj( Type, figure, Tag, FireWork ) ;if ishandle(OldHandle)close(OldHandle) ;end% 图形窗口初始化fig figure(units,normalized,position,[0.1 0.1 0.8 0.8],...menuba…

33 -jQuery 属性操作,文档操作(未完成)

转载于:https://www.cnblogs.com/venicid/p/9110130.html

GNU C - 关于8086的内存访问机制以及内存对齐(memory alignment)

接着前面的文章&#xff0c;这篇文章就来说说menory alignment -- 内存对齐. 一、为什么需要内存对齐&#xff1f; 无论做什么事情&#xff0c;我都习惯性的问自己&#xff1a;为什么我要去做这件事情&#xff1f; 是啊&#xff0c;这可能也是个大家都会去想的问题&#xff0c;…

mysql权限说法正确的是,【多选题】下面关于修改 MySQL 配置的说法中,正确的是...

参考答案如下【单选题】4.正常枕先露分娩时&#xff0c;多选的说仰伸发生于()39、题下【单选题】人们常常用来判断一种活动是不是游戏的一项外部指标是( )面关【多选题】S-S法阶段2训练内容包括于修【判断题】痉挛性睑内翻多发生于下睑。配置【判断题】萤火虫不仅成虫会发光,其…

读取exchange邮件的未读数(转载)

protected void Page_Load(object sender, EventArgs e) { Response.Write("administrator的未读邮件数是&#xff1a;" UnReadCount("administratordomainname")); } int UnReadCount(string userMailAddress) {…

嵌入式Linux下Qt的中文显示

一般情况下&#xff0c;嵌入式Qt界面需要中文显示&#xff0c;下面总结自己在项目中用到的可行的办法 1&#xff0c;下载一种中文简体字体&#xff0c;比如我用的是”方正准圆简体“&#xff0c;把字体文件放在ARM开发板系统的Qt字库中&#xff0c;即/usr/lib/fonts下 2&#x…

Robot Framework + Selenium library + IEDriver环境搭建

转载&#xff1a;https://www.cnblogs.com/Ming8006/p/4998492.html#c.d 目录&#xff1a; 1 安装文件准备2 Robot框架结构3 环境搭建 3.1 安装Python 3.2 安装Robot Framework 3.3 安装wxPython 3.4 安装RIDE 3.5 安装Selenium2Library 3.6 安装IEDriverServer 1 安装文…

php静态地图api,静态图API | 百度地图API SDK

百度地图静态图API&#xff0c;可实现将百度地图以图片形式嵌入到您的网页中。您只需发送HTTP请求访问百度地图静态图服务&#xff0c;便可在网页上以图片形式显示您的地图。静态图API较之JavaScript API载入的动态网站&#xff0c;既能满足基本的地图信息浏览&#xff0c;又能…

[XMOVE自主设计的体感方案] XMove Studio管理系统(二)应用开发API简要介绍

一. XMove的开放式应用开发框架简介 XMove4.0以开放式的结构满足扩展性的要求。所有无线协议&#xff0c;底层算法和控制逻辑全部上移到PC端。节点只根据接受的控制逻辑返回传感器数据。新的架构使得开发新应用非常方便。 本节将主要介绍XMove应用开发API及其使用。 二. 注册新…

搭建服务器Apache+PHP+MySql需要注意的问题

参见https://www.cnblogs.com/bytebull/p/7927542.html 一、软件下载的都是用zip压缩文件&#xff0c;三个软件均需手动配置&#xff0c;若想省事&#xff0c;可考虑phpstudy&#xff0c;一键安装。 我的服务器文件目录&#xff1a; 二、安装PHP时需注意&#xff0c;新版本的PH…