Ansible教程(5)Handlers、Registry、Tags用法

TangLu 系统运维 2023-11-02 9667 0

一、Ansible Handlers

1、Handlers的作用

handlers触发器用于对task进行监控,如果task所指定的任务状态发生变化,则进行notify通知,然后触发额外的一系列操作,看一个示例来帮助理解:

cat apache.yml
- hosts: webservers
  remote_user: root
  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: install configure file for httpd
    copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start httpd service
    service: enabled=true name=httpd state=started

上面的YAML文件存在三个task任务,分别是安装httpd、复制配置文件到远端主机、启动httpd服务。但是当第二个task中的配置文件发生了改变后再次执行playbook的话,会发现新的配置文件虽然会正确的复制到远端主机去,但是却没有重启httpd服务。因为Ansible在执行playbook时发现第三个任务与现在状态是一致的,就不会再次执行任务。为了解决这种问题,就需要使用ansible的handlers功能。handlers是用于监控一个任务的执行状态,如果一个tasks任务最后是changed状态则会触发handlers指定的操作。


2、如何配置handlers

Ansible中通过notify参数来调用定义好的handlers,在上面的示例中如果要实现重新推送更新后的httpd.conf能自动重启服务,配置如下

cat apache.yml
- hosts: webservers
  remote_user: root
  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: install configure file for httpd
    copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd  #通知restart httpd这个触发器
    - check httpd  #可以定义多个触发器
  - name: start httpd service
    service: enabled=true name=httpd state=started
  handlers:  #定义触发器,和tasks同级
  - name: restart httpd  #触发器名字,被notify引用,两边要一致
    service: name=httpd state=restart
  - name: check httpd
    shell: netstat -ntulp | grep 80


3、handlers强制执行选项

Ansible在执行过程中如果某个任务出错的话会导致后续任务都停止执行,但是在实际任务中部分任务可能必须保持某种状态,不受其他任务影响,这个时候可以定义一个handlers并保持执行

- hosts: web
  force_handlers: yes  #强制执行handlers
  tasks:
    - name: touch file
      file: path=/tmp/test.txt state=touch
      notify: restart nginx
    - name: installed packages         #如果没有force_handlers,该步执行错误后会导致handlers不被触发
      yum:
        name: test
        state: present
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted


二、Ansible Registry

Register用于将任务的执行结果传递给一个变量进行存储,这样就方便后续任务的调用

- hosts: webservers
    tasks:
      - name: Get Port
        shell: netstat -ntulp
        register: system_port           #上一步的执行结果将成为system_port变量的值
      - name: debug
        debug:
          msg: "监听端口信息:{{ system_port }}"


三、Ansible Tags

通过设置标签的方式可以为任务进行分组,这样可以通过指定标签的方法来让某些组执行任务,而不是所有主机都执行

- hosts: webservers
  tasks:
    - name: nginx config
      copy: 
        src: files/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        notify: restart nginx
        tags: nginx_server


运行playbook的时候需要声明标签,这样就只有标签下的任务才会操作

ansible-playbook nginx.yml -t "nginx_server"


忽略指定标签下的任务

ansible-playbook nginx.yml --skip-tags "nginx_server"


四、Ansible Tags使用示例

通过不同的tags安装指定版本的tomcat

cat main.yml
- name: Install Tomcat8 Server
  include: install_tomcat_8.yml
  tags: install_tomcat_8


- name: Install Tomcat9 Server
  include: install_tomcat_9.yml
  tags: install_tomcat_9

  
cat install_tomcat_8.yml
- hosts: webservers
  vars:
    - tomcat_data: /data
    - tomcat_version: 8.5.70
  tasks:

    - name: Create Tomcat Directory
      file:
        name: "{{ tomcat_data }}"
        state: directory
    
    - name: Copy Tomcat Server
      unarchive:
        src: "./files/apache-tomcat-{{ tomcat_version }}.tar.gz"
        dest: "{{ tomcat_data }}"
    
    - name: Create Links
      file:
        src: "{{ tomcat_data }}/apache-tomcat-{{ tomcat_version }}"
        dest: "{{ tomcat_data }}/tomcat8"
        state: link
    
    - name: Started Tomcat Server
      shell:
        cmd: "nohup {{ tomcat_data }}/tomcat8/bin/startup.sh &>/dev/null &"
    

ansible-playbook main.yml -t "tomcat_install_8"


评论