Playbook剧本之流程控制下

独自空忆成欢 提交于 2019-12-04 20:57:31

toc

触发器

当我们使用某些程序中,需要修改程序配置文件,之后有可能需要重启程序新的配置才能生效,该怎么实现呢?
这就需要用到触发器了
当task的任务被改变时,如果定义了notify就会触发notify,notify会把内容交到handler中,handler会根据notify信息在自己的描述(name)中查找任务执行

## 在其他服务器上复制个httpd配置文件
[root@Ansible project]# scp root@192.168.1.2:/etc/httpd/conf/httpd.conf ./httpd.conf.j2
## 编写httpd的Playbook并加上触发器
[root@Ansible project]# vim httpd.yml
- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: installed

    - name: Configure Httpd Server
      copy:
        src: ./httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server   ## 任务改变,则触发条件

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
  handlers:     ## 根据notify信息执行任务
    - name: Restart Httpd Server
      systemd:
        name: httpd

Playbook的顺序是先执行play里面所有task。当task里面有任务报错,Playbook就会停止,也就不会执行handlers里面的任务,这时可以 force_handlers: yes 强行执行handles里面的任务

- hosts: web
  force_handlers: yes

handlers注意事项

  1. 无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
  2. 只有task发生改变了才会通知handlers,没有改变则不会触发handlers
  3. 不能使用handlers替代tasks

标记(用于调试的场景)

给任务定义标记可以执行这个标记的任务,或者跳过这个标记的任务

## 给上一个http服务每个命令定义标记
[root@Ansible project]# vim httpd.yml
- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: installed
      tags: Install_httpd

    - name: Configure Httpd Server
      copy:
        src: ./httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server
      tags: Config_httpd

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
      tags: Start_httpd 

  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted
## 查看这个Playbook的所有标记
[root@Ansible project]# ansible-playbook httpd.yml --list-tags

playbook: httpd.yml

  play #1 (web): web TAGS: []
      TASK TAGS: [Config_httpd, Install_httpd, Start_httpd]
## 只执行安装任务(用 -t 指定标记)
[root@Ansible project]# ansible-playbook httpd.yml -t Install_httpd
## 执行安装启动任务
[root@Ansible project]# ansible-playbook httpd.yml -t Install_httpd,Start_httpd
## 跳过配置任务
[root@Ansible project]# ansible-playbook httpd.yml --skip-tags Config_httpd

文件调用

多个playbook放到一个playbook执行,用import_playbook或者include都可以但是include执行时会警告,建议是用import_playbook

## 写两个简单的Playbook
[root@Ansible project]# vim install_httpd.yml 
- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: installed

[root@Ansible project]# vim start_httpd.yml
- hosts: web
  tasks:
    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
## 用import_playbook调用两个Playbook
[root@Ansible project]# vim httpd.yml
- import_playbook: install_httpd.yml
- import_playbook: start_httpd.yml

用include_tasks只调用任务

## 写两个简单的Playbook
[root@Ansible project]# vim install_httpd.yml 
- name: Install Httpd Server
  yum:
    name: httpd
    state: installed

[root@Ansible project]# vim start_httpd.yml
- name: Start Httpd Server
  systemd:
    name: httpd
    state: started
## 用include_tasks调用两个task文件
[root@Ansible project]# vim httpd.yml
- hosts: web
  tasks:
    - include_tasks: install_httpd.yml
    - include_tasks: start_httpd.yml

忽略错误

Playbook特性就是执行时报错直接停止执行,但有时候对于不太重要的任务,报错也影响不大,我们不想因为这个报错就停止执行Playbook,这是我们就需要用到忽略错误。该报错就报错,之后继续执行后面的任务

- hosts: webservers
  tasks:
    - name: Ignore False
      command: /bin/false
      ignore_errors: yes     ## 忽略错误
    - name: touch new file
      file: 
        path: /tmp/file.txt 
        state: touch

异常处理

当查询受控端各种状态,没有发生变化,执行过程还是显示被改变了,这种结果会干扰判断。可以用 changed_when: false 改变为ok

- hosts: web
  tasks:
    - name: Get Disk Statistics
      shell: df -h
      register: disk_use
      changed_when: false       ## 执行时把changed改为ok

    - name: OutPut Disk Statistics
      debug:
        msg: "{{ disk_use.stdout_lines }}"

也可以用 changed_when: 变量.stdout.find('字符串') 检测执行结果有没有某个字符串,没有改字符串会报错,报错按照Playbok特性会停止执行

- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: installed
    - name: Configure Httpd Server
      copy:
        src: ./httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server
    - name: Check Httpd Server
      shell: /usr/sbin/httpd -t         ## httpd检测配置文件语法的命令
      register: httpd_check          ## 结果赋予到变量
      changed_when: 
        - httpd_check.stdout.find('OK')    ## 检测变量里面有没有OK,没有回报错
        - false          ## 执行时把changed改为ok

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted

或者用 failed_when: "'字符串' in 变量.stderr" 直接检测

- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: installed
    - name: Configure Httpd Server
      copy:
        src: ./httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server
    - name: Check Httpd Server
      shell: /usr/sbin/httpd -t         ## httpd检测配置文件语法的命令
      register: httpd_check          ## 结果赋予到变量
      failed_when: "'error' in httpd_check.stderr"  ## 变量中有 error 字符串直接报错

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!