zoukankan      html  css  js  c++  java
  • ansible-playbook流程控制-when条件判断

    1. ansible-playbook添加判断
         when相当于shell脚本里的if 判断,when语句就是用来实现这个功能的,它是一个jinja2的语法,但是不需要双大括号,用法很简单
      1.1) 示例1:

     1 [root@test-1 when]# vim when_test1.yaml 
     2 [root@test-1 when]# cat when_test1.yaml 
     3 ---
     4 - hosts: web1
     5   gather_facts: yes
     6  
     7   tasks:
     8   - name: "IP if"
     9     debug: msg={{ansible_default_ipv4.address}}
    10     when: ansible_default_ipv4.address == '192.168.200.133'
    11 
    12     # 注意 Ansible facts和vars 比如 ansible_os_family 应能被引用
    13     # 直接写,不带双大括号。

      1.2) 示例2:使用括号对条件进行分组

     1 [root@test-1 when]# vim when_test2.yaml 
     2 [root@test-1 when]# cat when_test2.yaml 
     3 ---
     4 - hosts: localhost
     5   gather_facts: yes
     6  
     7   tasks:
     8     - name: "shut down CentOS 6 and Debian 7 systems"
     9       command: ls -a
    10       when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "6") or
    11             (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "7")

    1.3) 示例3:所有需要为true的多条件判读(逻辑“and”) 也可以指定为列表

     1 [root@test-1 when]# vim when_test3.yaml 
     2 [root@test-1 when]# cat when_test3.yaml 
     3 ---
     4 - hosts: localhost
     5   gather_facts: yes
     6  
     7   tasks:
     8     - name: "shut down CentOS 7 systems"
     9       command: ls -a
    10       when:
    11         - ansible_facts['distribution'] == "CentOS"
    12         - ansible_facts['distribution_major_version'] == "7"

    2. 案例:
      2.1) ansible-playbook进行when判断是centos还是Ubuntu系统后在安装http

     1 [root@test-1 when]# vim when_mc.yaml 
     2 [root@test-1 when]# cat when_mc.yaml 
     3 ---
     4 - hosts: localhost
     5   gather_facts: yes
     6  
     7   tasks:
     8   - name: "update apache version - yum"
     9     yum: name=httpd state=present
    10     when:
    11       - ansible_pkg_mgr == 'yum'
    12     notify: restart httpd
    13 
    14   - name: "Update apache version - apt"
    15     apt: name=apache2 state=present update_cache=yes
    16     when:
    17       - ansible_pkg_mgr == 'apt'
    18     notify: restart apache2
    19 
    20 
    21   handlers:
    22     - name: restart apache2
    23       service: name=apache2 state=restarted
    24     - name: restart httpd
    25       service: name=httpd state=restarted

      2.2) 配置文件检查

    1 [root@test-1 when]# ansible-playbook  --syntax-check when_mc.yaml 
    2 
    3 playbook: when_mc.yaml

      2.3) 执行远程安装脚本

     1 [root@test-1 when]# ansible-playbook   when_mc.yaml 
     2 
     3 PLAY [localhost] *******************************************************************************************************************************
     4 
     5 TASK [Gathering Facts] *************************************************************************************************************************
     6 ok: [192.168.200.131]
     7 
     8 TASK [update apache version - yum] *************************************************************************************************************
     9 changed: [192.168.200.131]
    10 
    11 TASK [Update apache version - apt] *************************************************************************************************************
    12 skipping: [192.168.200.131]
    13 
    14 RUNNING HANDLER [restart httpd] ****************************************************************************************************************
    15 changed: [192.168.200.131]
    16 
    17 PLAY RECAP *************************************************************************************************************************************
    18 192.168.200.131            : ok=3    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 
  • 相关阅读:
    /etc/sysctl.conf 控制内核相关配置文件
    python 并发编程 非阻塞IO模型
    python 并发编程 多路复用IO模型
    python 并发编程 异步IO模型
    python 并发编程 阻塞IO模型
    python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
    python 并发编程 基于gevent模块实现并发的套接字通信
    python 并发编程 io模型 目录
    python 并发编程 socket 服务端 客户端 阻塞io行为
    python 并发编程 IO模型介绍
  • 原文地址:https://www.cnblogs.com/scajy/p/11561416.html
Copyright © 2011-2022 走看看