playbook条件语句
1 2 3 4 5 |
不管是shell还是各大编程语言中,流程控制,条件判断这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用频率极其高。 例如: 1.我们使用不同的系统的时候,可以通过判断系统来对软件包进行安装。 2.在nfs和rsync安装过程中,客户端服务器不需要推送配置文件,之前我们都是写多个play,会影响效率。 3.我们在源码安装nginx的时候,执行第二遍就无法执行了,此时我们就可以进行判断是否安装过。 |
判断主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
[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" |
多条件判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
[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*" |
判断服务是否安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
[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 |
判断系统
1 2 3 4 5 6 7 8 9 10 |
[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" |
判断系统版本
1)方式一
1 2 3 4 5 6 7 8 9 10 |
[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)方式二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#多条件判断的第二种方式,列表的形式(只能表示并且\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)一般使用场景
1 2 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 |