zoukankan      html  css  js  c++  java
  • playbook条件语句

    一、playbook条件语句

    1.判断主机

    [root@m01 ~]# cat lnmp5.yml 
    - hosts: nfs_group
      tasks:
        - name: Install nfs Server
          yum:
            name: nfs-utils
            state: present
    
        - name: Install rpcbind Server
          yum:
            name: rpcbind
            state: present
    
        - name: Config nfs Server
          copy:
            content: /data/wp-content 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
            dest: /etc/exports      
          when: ansible_fqdn == "nfs"
    
        - name: Mkdir data
          file:
            path: /data
            state: directory
            owner: www
            group: www
          when: ansible_fqdn == "nfs"
    
        - name: Start nfs Server
          systemd:
            name: nfs
            state: started
          when: ansible_fqdn == "nfs"
    
        - name: Copy wp-content to NFS
          copy:
            src: /root/package/wp-content
            dest: /data
            owner: www
            group: www
          when: ansible_fqdn == "nfs"
        
        - name: Start rpcbind Server
          systemd:
            name: rpcbind
            state: started
          when: ansible_fqdn != "nfs"
    
        - name: Mount nfs
          mount:
            src: 172.16.1.31:/data/wp-content
            path: /code/wordpress/wp-content/
            fstype: nfs
            opts: defaults
            state: mounted
          when: ansible_fqdn != "nfs"

    2.多条件判断

    [root@m01 ~]# cat lnmp5.yml 
    - hosts: all
      tasks:
        - name: Install nfs Server
          yum:
            name: nfs-utils
            state: present
          when: (ansible_fqdn == "nfs") or (ansible_fqdn is match "web*")
    
        - name: Install rpcbind Server
          yum:
            name: rpcbind
            state: present
          when: (ansible_fqdn == "nfs") or (ansible_fqdn is match "web*")
    
        - name: Config nfs Server
          copy:
            content: /data/wp-content 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
            dest: /etc/exports      
          when: ansible_fqdn == "nfs"
    
        - name: Mkdir data
          file:
            path: /data
            state: directory
            owner: www
            group: www
          when: ansible_fqdn == "nfs"
    
        - name: Start nfs Server
          systemd:
            name: nfs
            state: started
          when: ansible_fqdn == "nfs"
    
        - name: Copy wp-content to NFS
          copy:
            src: /root/package/wp-content
            dest: /data
            owner: www
            group: www
          when: ansible_fqdn == "nfs"
        
        - name: Start rpcbind Server
          systemd:
            name: rpcbind
            state: started
          when: (ansible_fqdn == "nfs") or (ansible_fqdn is match "web*")
    
        - name: Mount nfs
          mount:
            src: 172.16.1.31:/data/wp-content
            path: /code/wordpress/wp-content/
            fstype: nfs
            opts: defaults
            state: mounted
          when: ansible_fqdn is match "web*"

    3.判断服务是否安装

    [root@m01 ~]# cat lnmp3.yml 
    - hosts: web_group
      tasks:
        - name: Tar php.tar.gz
          unarchive:
            src: /root/package/php.tar.gz
            dest: /tmp/
    
        - name: Get PHP Install status
          shell: "rpm -qa | grep php"
          ignore_errors: yes
          register: get_php_install_status
        
        #打印注册的变量信息,没有任何作用,只是为了获取安装状态判断参数 rc
        - name: print PHP Install status
          debug:
            msg: "{{ get_php_install_status }}"
    
        - name: Install PHP Server
          shell: yum localinstall -y /tmp/*.rpm
          when: get_php_install_status.rc != 0
    
        - name: Config php Server
          copy:
            src: /root/conf/php.ini
            dest: /etc/
    
        - name: Config php Server
          copy:
            src: /root/conf/www.conf
            dest: /etc/php-fpm.d/
    
        - name: Start php Server
          systemd:
            name: php-fpm
            state: started
            enabled: yes

    4.判断系统

    [root@m01 ~]# vim web.yml
    - hosts: nfs
      tasks:
        - name: Install httpd
          shell: "yum install -y httpd"
          when: ansible_distribution == "CentOS"
    
        - name: Install apache2
          shell: "apt-get apache2"
          when: ansible_distribution == "Ubuntu"

    5.判断系统版本

    1)方式一:

    [root@m01 ~]# cat version.yml 
    - hosts: nfs
      tasks:
        - name: CentOS6 Start httpd
          shell: "service httpd start"
          when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "6")
    
        - name: CentOS7 Start httpd
          shell: "systemctl start httpd"
          when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "7")

    2)方式二

    #多条件判断的第二种方式,列表的形式(只能表示并且and和)
    [root@m01 ~]# cat version.yml 
    - hosts: nfs
      tasks:
        - name: CentOS6 Start httpd
          shell: "service httpd start"
          when: 
            - ansible_distribution == "CentOS"
            - ansible_distribution_major_version == "6"
    
        - name: CentOS7 Start httpd
          shell: "systemctl start httpd"
          when: 
            - ansible_distribution == "CentOS"
            - ansible_distribution_major_version == "7"

    3)一般使用场景

    tasks:
      - shell: echo "only on Red Hat 6, derivatives, and later"
        when: ansible_facts['os_family'] == "RedHat" and ansible_facts['lsb']['major_release']|int >= 6
  • 相关阅读:
    如何把项目部署到OSChina上
    读书笔记----10日摘抄整理(05)
    读书笔记----10日摘抄整理(04)
    读书笔记----10日摘抄整理(03)
    读书笔记----10日摘抄整理(02)
    读书笔记----10日摘抄整理(01)
    前五题半
    作业3
    作业: 1.8(圆的面积和周长)编写程序,使用以下的公式计算并显示半径为5.5的圆的面积和周长。
    作业:1.12假设一个跑步者1小时40分钟35秒 内跑了24英里。编写一个程序显示以每小时多少公里为单位的平均速度值(注意,1英里等于1.6公里。)
  • 原文地址:https://www.cnblogs.com/chenlifan/p/13777402.html
Copyright © 2011-2022 走看看