• Ansible触发器-tag标签-忽略错误


    触发器

    playbook handlers

    handler`用来执行某些条件下的任务,比如当配置文件发生变化的时候,通过notify触发handler去重启服务。

    在saltstack中也有类似的触发器,写法相对Ansible简单,只需要watch,配置文件即可。

    大白话:监控某一个步骤,一旦该步骤发生了变化,则立马触发该步骤的触发器,执行对应的步骤

    注意:
    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
    
    

    触发器的写法

    - hosts: web01
      task:
        - name: Push Nginx PHP Conf
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
          with_items:
            - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
            - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
            - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
          when: ansible_fqdn is match 'web*'
          notify: Restart Nginx And PHP
          
     # 通过名字的方式来关联触发器,当配置文件有变化的时候,就会引发触发器
      handlers:
        - name: Restart Nginx And PHP
          service:
            name: "{{ item }}"
            state: restarted
          with_items:
            - nginx
            - php-fpm
    
    注意:tasks中的notify名字必须和handlers中的- name名字对应上,否则触发器和任务没有做任何关联
    

    tag标签

    默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务,Ansible的标签(tag)功能可以给单独任务甚至整个playbook打上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。

    打标签的方式

    1.对一个task打一个标签

    我只想推送nginx的配置文件

        - name: Push Nginx PHP Conf
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
          with_items:
            - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
            - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
            - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
          when: ansible_fqdn is match 'web*'
          notify: Restart Nginx And PHP
          tags: config_nginx
    
    ## 运行:
    [root@m01 ansible]# ansible-playbook lnmp.yml  -t config_nginx
    -t:运行指定的tag
    --skip-tags:跳过指定的tag
    

    2.对一个task打多个标签

    有一个功能任务,我安装nginx的时候需要创建www用户,安装nfs的时候,需要创建www用户,安装rsync的时候需要创建www用户

    创建www用户这个功能,有多个任务都需要使用

    tag: install_nginx

    tag: install_nfs

    tag: install_rsync

        - name: Create {{ web_user_group }} Group
          group:
            name: "{{ web_user_group }}"
            gid: 666
            state: present
          tags:
            - install_nginx
            - install_nfs
            - install_rsync
    
    [root@m01 ansible]# ansible-playbook lnmp.yml  -t install_nginx
    
        - name: Push Nginx PHP Conf
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
          with_items:
            - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
            - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
            - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
          when: ansible_fqdn is match 'web*'
          notify: Restart Nginx And PHP
          tags: 
            - congfig_nginx
            - install_nginx
    

    比如在只运行部署代码的步骤,需要创建www用户,只运行部署rsync的时候也需要创建www用户,这个时候就需要把创建www用户这个tasks加上多个标签。

    3.对多个task打一个标签

    我只想重新安装nginx

    1.安装nginx

    tag: install_nginx

    2.配置nginx打一个标签

    tag: install_nginx

        - name: Unarchive Nginx and PHP
          unarchive:
            src: /ansible/web/nginx_php.tgz
            dest: /root
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
        - name: Install Nginx and PHP
          yum:
            name: /root/nginx_php/{{ item }}
            state: present
          with_items: "{{ nginx_php_packages }}"
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
        - name: Push Nginx PHP Conf
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
          with_items:
            - { src: "/ansible/web/nginx.conf",dest: "/etc/nginx/nginx.conf" }
            - { src: "/ansible/web/www.zls.com.conf",dest: "/etc/nginx/conf.d/www.zls.com.conf" }
            - { src: "/ansible/web/www.conf",dest: "/etc/php-fpm.d/www.conf" }
          when: ansible_fqdn is match 'web*'
          notify: Restart Nginx And PHP
          tags:
            - congfig_nginx
            - install_nginx
    
        - name: Create HTML Dir
          file:
            path: /code
            owner: "{{ web_user_group }}"
            group: "{{ web_user_group }}"
            state: directory
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
        - name: Unarchive WordPress Package
          unarchive:
            src: /ansible/web/wordpress.tgz
            dest: /code
            owner: "{{ web_user_group }}"
            group: "{{ web_user_group }}"
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
        - name: Start Nginx Server
          service:
            name: "{{ item }}"
            state: started
            enabled: true
          with_items:
            - nginx
            - php-fpm
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
        - name: Mount NFS Share Directory
          mount:
            path: /code/wordpress/wp-content/uploads
            src: 172.16.1.31:/{{ nfs_dir }}
            fstype: nfs
            state: mounted
          when: ansible_fqdn is match 'web*'
          tags: install_nginx
    
    ## 运行:
    [root@m01 ansible]# ansible-playbook lnmp.yml  -t install_nginx
    
    -t:运行指定的tag
    --skip-tags:跳过指定的tag
    

    比如我只想执行安装nginx的步骤,需要创建www用户、创建站点目录、加入防火墙规则等,把这些步骤打上统一标签,然后调用执行这样就可以只运行指定的相应的步骤及就可以了。

    playbook的复用

    只调用task:include_tasks
    调用整个task文件:include (新版本:import_playbook)

    在saltstack中,叫做top file入口文件。

    示例一:

    [root@m01 m01]# cat task.yml 
    - hosts: web_group
      vars:
        - http_port: 8080
    
      tasks:
        - include_tasks: task_install.yml
        - include_tasks: task_configure.yml
        - include_tasks: task_start.yml
    
      handlers:
        - name: Restart Httpd Server
          systemd:
            name: httpd
            state: restarted
    
    [root@m01 m01]# cat task_install.yml 
    - name: Install Http Server
      yum:
        name: httpd
        state: present
    
    [root@m01 m01]# cat task_configure.yml 
    - name: configure httpd server
      template:
        src: ./httpd.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server
    
    [root@m01 m01]# cat task_start.yml 
    - name: start httpd server
      service:
        name: httpd
        state: started
        enabled: yes
    

    示例二

    - include: httpd.yml
    - include: nfs.yml
    - include: rsync.yml
    

    示例三

    - import_playbook: httpd.yml
    - import_playbook: nfs.yml
    - import_playbook: rsync.yml
    

    忽略错误

    默认playbook会检测task执行的返回状态,如果遇到错误则会立即终止playbook的后续task执行,然而有些时候playbook即使执行错误了也要让其继续执行。

    加入参数:ignore_errors:yes 忽略错误

    [root@m01 ~]# cat ignore.yml
    - hosts: web_group
      tasks:
        - name: Ignore False
          command: /bin/false
          ignore_errors: yes
          
        - name: touch new file
          file:
            path: /tmp/zls.txt
            state: touch
    
  • 相关阅读:
    动态规划(最长公共序列,最长上升序列)
    滴滴笔试--算术转移(20190827)
    线段树和树状数组
    pair和list学习
    数据结构--树(建立、遍历)
    tmux常用命令与快捷键
    机器学习实战-逻辑回归
    字符流中第一个重复的字符
    机器学习实战-朴素贝叶斯
    Python第三方库
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13376493.html
Copyright © 2020-2023  润新知