when

tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian" tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result|failed
- command: /bin/something_else
when: result|success
- command: /bin/still/something_else
when: result|skipped # fact
ansible hostname.example.com -m setup tasks:
- shell: echo "only on Red Hat 6, derivatives, and later"
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6 # 变量
vars:
epic: true tasks:
- shell: echo "This certainly is epic!"
when: epic tasks:
- shell: echo "This certainly isn't epic!"
when: not epic # jinja2模板
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 not defined # loop
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5

Loading in Custom Facts

tasks:
- name: gather site specific fact data
action: site_facts
- command: /usr/bin/thingy
when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'

Applying ‘when’ to roles and includes

- include: tasks/sometasks.yml
when: "'reticulating splines' in output" - hosts: webservers
roles:
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }

Conditional Imports

---
- hosts: all
remote_user: root
vars_files:
- "vars/common.yml"
- [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
tasks:
- name: make sure apache is running
service: name={{ apache }} state=running ---
# for vars/CentOS.yml
apache: httpd
somethingelse: 42 # for facter
ansible -m yum -a "pkg=facter ensure=installed"
ansible -m yum -a "pkg=ruby-json ensure=installed" # for ohai
ansible -m yum -a "pkg=ohai ensure=installed"

Selecting Files And Templates Based On Variables

- name: template a file
template: src={{ item }} dest=/etc/myapp/foo.conf
with_first_found:
- files:
- {{ ansible_distribution }}.conf
- default.conf
paths:
- search_location_one/somedir/
- /opt/other_location/somedir/

Register Variables

- name: test play
hosts: all
tasks:
- shell: cat /etc/motd
register: motd_contents - shell: echo "motd contains the word hi"
when: motd_contents.stdout.find('hi') != -1 - name: registered variable usage as a with_items list
hosts: all
tasks:
- name: retrieve the list of home directories
command: ls /home
register: home_dirs - name: add home dirs to the backup spooler
file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
with_items: home_dirs.stdout_lines
# same as with_items: home_dirs.stdout.split()

ansible使用6-Conditionals的更多相关文章

  1. Ansible_编写循环和条件任务

    一.利用循环迭代任务 1️⃣:Ansible支持使用loop关键字对一组项目迭代任务,可以配置循环以利用列表中的各个项目.列表中各个文件的内容.生成的数字序列或更为复杂的结构来重复任务 1.简单循环 ...

  2. Ansible Playbook Conditionals

    通常,play的结果可能取决于变量的值,facts(有关远程系统的知识)或先前的任务结果. 在某些情况下,变量的值可能取决于其他变量. 此外,可以创建其他组,以根据主机是否与其他条件匹配来管理主机. ...

  3. ansible基础-playbooks

    1. playbooks介绍 如果说ansible的modules是工具,inventory配置文件是原材料,那么playbook就是一封说明书,这里会记录任务是如何如何执行的,当然如果你愿意,这里也 ...

  4. 【Ansible 文档】【译文】Playbooks 变量

    Variables 变量 自动化的存在使得重复的做事情变得很容易,但是我们的系统不可能完全一样. 在某些系统中,你可能想要设置一些与其他系统不一样的行为和配置. 同样地,远程系统的行为和状态也可以影响 ...

  5. 【Ansible 文档】【译文】网络支持

    Networking Support 网络支持 Working with Networking Devices 使用网络设备 自从Ansible 2.1开始,你现在可以使用成熟模型 - 编写 play ...

  6. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)

    无书面许可请勿转载 高级Playbook Extra variables You may have seen in our template example in the previous chapt ...

  7. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)

    如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. Th ...

  8. Ansible实践总结

    Ansible playbook 根据条件动态设置变量 首先新建 inventory,主机列表如下: node-01 ansible_host=192.168.64.30 node-02 ansibl ...

  9. ansible 配置文件设置

    目录 ansible 配置文件设置 一.ansible configuration settings 二.ansible 配置文件查找顺序(从上到下,依次查找) 三.附录ansible配置参数 ans ...

  10. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(三)

    未经书面许可.请勿转载 一张图简单概括 Simple Playbooks Ansible is useful as a command-line tool for making small chang ...

随机推荐

  1. 网络中常用的各种协议(针对TCP/IP协议组)

    网络层: ip协议->网络互连协议 用途:将多个包在网络中联系起来,传输数据包(不可靠传输),最基本功能就是寻址和分段功能,不提供端到端,路由到路由的确认,不提供重发和流量控制.是计算机网络能狗 ...

  2. P3166 [CQOI2014]数三角形

    传送门 直接求还要考虑各种不合法情况,不好计数 很容易想到容斥 把所有可能减去不合法的情况剩下的就是合法情况 那么我们只要任取不同的三点就是所有可能,不合法情况就是三点共线 对于两点 $(x_1,y_ ...

  3. PyCharm出现module 'matplotlib' has no attribute 'verbose'解决方案

    其实不是你安装错了,也不是你代码问题,这就是PyCharm的锅! 虽然有三种解法办法,我觉得还是改IDE配置是最佳方法 把这个钩去掉就行了...... # -*- coding: utf-8 -*- ...

  4. 2015苏州大学ACM-ICPC集训队选拔赛(2)1004

    草爷要的数 Problem Description 今天校队队员们准备放松一下,我们队就准备选一些数字玩,然而每个人喜欢的数字是不同的,刻盘喜欢x(1<=x<=1^9),凯凯喜欢y(1&l ...

  5. hdu2067 小兔的棋盘

    小兔的棋盘 时间限制:1000/1000 MS(Java / Others)内存限制:32768/32768 K(Java / Others)总提交内容:13029接受的提交内容:6517 问题描述 ...

  6. mysql 存储引擎介绍

    一  存储引擎解释 首先确定一点,存储引擎的概念是MySQL里面才有的,不是所有的关系型数据库都有存储引擎这个概念,后面我们还会说,但是现在要确定这一点. 在讲清楚什么是存储引擎之前,我们先来个比喻, ...

  7. linux系统延时和定时任务

    系统延时任务延时任务:只做一次的at命令: 系统定时及延时任务 延时任务:**有输出任务**不会输出到终端上而是发送邮件给你/var/mail/root/执行 mail at          时间 ...

  8. maven set MAVEN_OPTS

    http://juvenshun.iteye.com/blog/240257 https://docs.alfresco.com/5.1/tasks/alfresco-sdk-install-mave ...

  9. 配置中心:Nacos, Apollo, Consul, Etcd

    Nacos, Apollo, Consul, Etcd 服务.应用不同粒度的配置更丰富的路由规则集中式管理的动态参数规则

  10. Windows Server2008服务器可以远程桌面,但在内网中却Ping不通--解决方法

    Windows Server2008服务器可以通过远程桌面登录,但在内网中却Ping不通.所以要考虑的是防火墙是不是做了限制. 最简单粗暴的方法是直接把防火墙给关掉 还有个其他的方法是参考别人写的,我 ...