一、playbook条件语句
不管是shell还是各大编程语言中,流程控制,条件判断这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用频率极其高。
例如:
1.我们使用不同的系统的时候,可以通过判断系统来对软件包进行安装。
2.在nfs和rsync安装过程中,客户端服务器不需要推送配置文件,之前我们都是写多个play,会影响效率。
3.我们在源码安装nginx的时候,执行第二遍就无法执行了,此时我们就可以进行判断是否安装过。
1.判断主机
- name: Create www Group group: name: www gid: 666 state: present when: ansible_fqdn == "web01" - name: Create www User user: name: www uid: 666 group: www create_home: false shell: /sbin/nologin state: present when: ansible_fqdn == "web01"
2.如何判断服务是否安装
#执行shell,判断服务是否安装,并把结果注册一个变量 - name: Get PHP Install Status shell: "rpm -qa | grep php" ignore_errors: yes register: get_php_install_status #调用变量,查看内容,如果知道使用什么值判断可以不写 - name: Get get_php_install_status debug: msg: "{{ get_php_install_status }}" #调用变量结果,rc值不等于0时执行安装命令 - name: Install PHP Server shell: "yum localinstall -y /tmp/*.rpm" when: get_php_install_status.rc != 0
3.判断系统
- hosts: web01 tasks: - name: Install CentOS apache shell: "yum install -y httpd" when: ansible_distribution == "CentOS" - name: Install Ubuntu apache shell: "apt-get apache" when: ansible_distribution == "Ubuntu"
4.判断系统版本
- hosts: web01 tasks: - name: Start CentOS6 apache shell: "/etc/init.d/httpd start" when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "6") - name: Install Ubuntu apache shell: "apt-get apache" when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "7") #指定多个条件,除了用and,还可以使用列表形式 - hosts: web01 tasks: - name: Start CentOS6 apache shell: "/etc/init.d/httpd start" when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" - name: Install Ubuntu apache shell: "apt-get apache" when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7"
二、playbook循环语句
1.定义变量循环安装
- hosts: db01 tasks: - name: Install Mariadb Server yum: name: "{{ package }}" state: present vars: package: - mariadb-server - MySQL-python
2.定义变量启动服务
#错误配置方法 - name: Start PHP Server systemd: name: "{{ server }}" state: started enabled: yes vars: server: - php-fpm - nginx #正确写法 - name: Start PHP and Nginx Server systemd: name: "{{ item }}" state: started enabled: yes with_items: - nginx - php-fpm
3.字典定义变量
#创建用户组 - name: Create www Group group: name: "{{ item.name }}" gid: "{{ item.gid }}" state: present with_items: - { name: "www", gid: "666" } - { name: "lhd", gid: "777" } - { name: "test", gid: "888" } when: ansible_fqdn == "web01" #创建多个用户 - name: Create www User user: name: "{{ item.name }}" uid: "{{ item.uid }}" group: "{{ item.group }}" create_home: "{{ item.create_home }}" shell: "{{ item.shell }}" state: present with_items: - { name: "www", uid: "666", group: "www", create_home: "false", shell: "/sbin/nologin" } - { name: "lhd", uid: "777", group: "lhd", create_home: "ture", shell: "/sbin/nologin" } - { name: "test", uid: "888", group: "test", create_home: "false", shell: "/bin/bash" } #创建多个目录 - name: Chown Code Dir file: path: "{{ item.path }}" state: directory owner: "{{ item.user }}" group: "{{ item.user }}" recurse: yes with_items: - { path: "/code", user: "www" } - { path: "/data", user: "lhd" }
三、ansible handlers(触发器)
1.handlers作用
handler用来执行某些条件下的任务,比如当配置文件发生变化的时候,通过notify触发handler去重启服务。
在saltstack中也有类似的触发器,写法相对Ansible简单,只需要watch,配置文件即可。
2.配置触发器
- name: Config Nginx Server copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/root/nginx.conf", dest: "/etc/nginx/" } - { src: "/root/wordpress.conf", dest: "/etc/nginx/conf.d/" } notify: restart_nginx handlers: - name: restart_nginx systemd: name: nginx state: restarted
3.触发器注意事项
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。
3.Handlers只会在每一个play的末尾运行一次;如果想在一个playbook中间运行Handlers,则需要使用meta模块来实现。例如: -meta: flush_handlers。
4.如果一个play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用meta模块的–force-handlers选项来强制执行Handlers,即使Handlers所在的play中途运行失败也能执行。
5.不能使用handlers替代tasks