前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >06-Ansible判断

06-Ansible判断

原创
作者头像
小朋友呢
修改2020-01-14 17:50:35
7830
修改2020-01-14 17:50:35
举报
文章被收录于专栏:小朋友学云计算

条件语句

when

根据不同的系统,安装不同的Apache版本

代码语言:txt
复制
[student@workstation ansible]$ cat one.yml
- hosts: all
  remote_user: root
  tasks:
  - name: 0.clean yum
    shell: rm -rf /etc/yum.repos.d/*
    when: ansible_os_family == "RedHat"

  - name: 1.install ali source
    get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo mode=0644 force=yes
    when: ansible_os_family == "RedHat"
  
  - name: 2.change yum
    shell: sed -ri 's#\$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo && yum makecache
    when: ansible_os_family == "RedHat"

  - name: 3-1.Redhat family install web server
    yum: name=httpd state=latest
    when: ansible_os_family == "RedHat"

  - name: 3-2.Debian faily install web server
    apt: name=apache2 state=latest
    when: ansible_os_family == "Debian"

when in

判断变量是否在某个列表里

代码语言:txt
复制
[student@workstation ansible]$ cat when_in.yml 
- hosts: all
  tasks:
  - yum:
     name: httpd
     state: latest
    #如果当前主机在webservers组中就执行安装httpd的任务
    when: ansible_hostname in groups['webservers']

判断变量是否不在某个列表里

代码语言:txt
复制
- hosts: all
  tasks:
  - shell: 'reboot now'
    #如果不是admin组的主机就直接重启
    when: ansible_hostname not in groups['admin']

运算符

比较运算符

逻辑运算符

==

等于

and

!=

不等于

or

大于

not

<

小于

()

组合

=

大于等于

and

<=

小于等于

or

操作系统是Redhat7的6版本或者7版本并且主机名不是host2

代码语言:txt
复制
- hosts: all
  remote_user: root

  tasks:
  - name: compare os version

    debug:
     msg: "System is RedHat6 or Redhat7 And host not host2"

    when: ansible_distribution == "RedHat"  and (ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7") and (not ansible_nodename == "host2")

变量定义

defined

变量已经定义

undefined

变量未定义

none

变量定义了,没有值

代码语言:txt
复制
[student@workstation ansible]$ cat var_define.yml
- hosts: servera
  gather_facts: no
  vars:
    keys:
  tasks:
  - debug:
     msg: "ansible_hostname is not defined"
    when: ansible_hostname is undefined
  
  - debug:
     msg: "key is defined"
    when: keys is defined

  - debug:
     msg: "keys is none"
    when: keys is none

执行结果

succeeded

通过任务返回信息,执行成功返回真

failed

通过任务返回信息,执行失败返回真

change

通过任务返回信息,执行状态为change返回真

Skipped

通过任务返回信息,任务没有满足条件跳过执行,返回真

代码语言:txt
复制
[student@workstation ansible]$ cat status.yml
- hosts: servera
  gather_facts: no
  tasks:
  - shell: ls /home/
	register: retmsg

  - debug:
     msg: 
     - "runing successfully"
     - " {{ retmsg.stdout }}"
	when: retmsg is succeeded
	
  - debug:
     msg: 
     - "runing successfully"
     - " {{ retmsg.stdout }}"
	when: retmsg is failed
	
  - debug:
     msg: 
     - "runing successfully"
     - " {{ retmsg.stdout }}"
    when: retmsg is change
    
  - debug:
     msg: 
     - "runing successfully"
     - " {{ retmsg.stdout }}"
    when: retmsg is skipped

其他判断

string

全部是字符串返回真

lower

字符串全部小写返回真

upper

字符串全部大写返回真

even

偶数返回真

odd

奇数返回真

subset

一个list是另一个的子集返回真

superset

一个list是另一个的父集返回真

number

全部是数字返回真

代码语言:txt
复制
[student@workstation ansible]$ cat when_other.yml
- hosts: servera
  gather_facts: no
  vars:
   comment: "wolala wolala wolalala"
   age: 13
  tasks:
  - debug:
     msg: "This is string"
    when: comment is string

  - debug:
     msg: "This is lower"
    when: comment is lower

  - debug:
     msg: "This not upper"
    when: comment is not upper

  - debug:
     msg: "This is not even"
    when: age is not even

  - debug:
     msg: "This is odd"
    when: age is odd

  - debug:
     msg: "This is number"
    when: age is number

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 条件语句
    • when
      • when in
        • 运算符
          • 变量定义
            • 执行结果
              • 其他判断
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档