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

 

posted @ 2020-10-07 15:01  六月OvO  阅读(165)  评论(0编辑  收藏  举报