Ansible include任务复用 tags ignore_errors

include任务复用

复制代码
[root@ansible ~]# ls
3.26.sh   an-10.yml  an-3.yml  an-6.yml  an-9.yml                      apache-tomcat-8.5.100.tar.gz.1  bianliang.yml  group_vars  replace.txt
a.txt     an-11.yml  an-4.yml  an-7.yml  anaconda-ks.cfg               apache-tomcat-8.5.100.tar.gz.2  cp.tar.gz      host_vars
an-1.yml  an-2.yml   an-5.yml  an-8.yml  apache-tomcat-8.5.100.tar.gz  apache-tomcat-9.0.98.tar.gz     cp.txt         httpd.conf
[root@ansible ~]# vi an-12.yml 
[root@ansible ~]# cat an-12.yml
# 作为主控文件
- hosts: webservers
  vars:
    tom8_ver: 8.5.100
    tom9_ver: 9.0.98
    tom_install_dir: /usr/local
  tasks:
    - name: 安装tomcat8+jdk8
      include: setup8.yml
      tags: tom8
    - name: 安装tomcat9+jdk11
      include: setup9.yml
      tags: tom9 
[root@ansible ~]# vi setup8.yml
[root@ansible ~]# cat setup8.yml
# 安装tomcat8和jdk8
- name: 安装jdk8
  yum:
    name: java-1.8.0-openjdk
    state: present

- name: 解压、安装tomcat
  unarchive:
    src: /root/apache-tomcat-{{ tom8_ver }}.tar.gz
    dest: "{{ tom_install_dir }}"

- name: 启动
  shell: cd {{ tom_install_dir }} && mv apache-tomcat-{{ tom8_ver }} tomcat8 && cd tomcat8/bin && nohup ./startup.sh &
[root@ansible ~]# cp setup8.yml setup9.yml
[root@ansible ~]# vi setup9.yml
[root@ansible ~]# cat setup9.yml
# 安装tomcat9和jdk11
- name: 安装jdk11
  yum:
    name: java-11-openjdk
    state: present

- name: 解压、安装tomcat
  unarchive:
    src: /root/apache-tomcat-{{ tom9_ver }}.tar.gz
    dest: "{{ tom_install_dir }}"

- name: 启动
  shell: cd {{ tom_install_dir }} && mv apache-tomcat-{{ tom9_ver }} tomcat9 && cd tomcat9/bin && nohup ./startup.sh & 
[root@ansible ~]# ansible-playbook an-12.yml -t tom9 #这里可以在两个文件中选安装哪一个
[DEPRECATION WARNING]: "include" is deprecated, use include_tasks/import_tasks instead. See https://docs.ansible.com/ansible-
core/2.14/user_guide/playbooks_reuse_includes.html for details. This feature will be removed in version 2.16. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [安装jdk11] ****************************************************************************************************************************************
changed: [192.168.92.20]

TASK [解压、安装tomcat] *********************************************************************************************************************************
changed: [192.168.92.20]

TASK [启动] *********************************************************************************************************************************************
changed: [192.168.92.20]

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

tags

复制代码
[root@ansible ~]# vi an-13.yml
[root@ansible ~]# cat an-13.yml
# 添加方式
# 一对多:给多个任务打多个标签
# 一对一:给一个任务打一个标签
# 多对一:给多个任务打一个标签

# -t 要执行的标签
# --skip-tags 不要执行的任务

- hosts: webservers
  tasks:
    - name: "查询"
      shell: who
      tags: aaa

    - name: "安装nfs服务"
      yum:
        name: nfs-utils
        state: present
      tags:
        - install_nfs
        - in_nfs
        - aaa

    - name: "启动服务"
      service:
        name: nfs-server
        state: started
      tags: start_nfs
[root@ansible ~]# ansible-playbook -t aaa an-13.yml

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [查询] *********************************************************************************************************************************************
changed: [192.168.92.20]

TASK [安装nfs服务] **************************************************************************************************************************************
ok: [192.168.92.20]

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ignore_errors

复制代码
[root@ansible ~]# vi an-10.yml
[root@ansible ~]# cat an-10.yml
# user={{ key.value }}
- hosts: webservers
  tasks:
    - name: "错误任务"
      shell: asdaafiefncnac

    - name: "创建用户组"
      group:
        name: "{{ item }}"
        state: present
      loop:
        - gp1
        - gp2
        - gp3

    - name: "创建用户并设置基本组"
      user:
        name: "{{ item.uname }}"
        group: "{{ item.group }}"
        state: present
      loop:
        - { uname: test1, group: gp1 }
        - { uname: test2, group: gp2 }
        - { uname: test3, group: gp3 }
[root@ansible ~]# ansible-playbook an-10.yml

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [错误任务] *****************************************************************************************************************************************
fatal: [192.168.92.20]: FAILED! => {"changed": true, "cmd": "asdaafiefncnac", "delta": "0:00:00.006170", "end": "2026-03-29 18:02:47.741308", "msg": "non-zero return code", "rc": 127, "start": "2026-03-29 18:02:47.735138", "stderr": "/bin/sh: line 1: asdaafiefncnac: command not found", "stderr_lines": ["/bin/sh: line 1: asdaafiefncnac: command not found"], "stdout": "", "stdout_lines": []}

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
[root@ansible ~]# vi an-10.yml
[root@ansible ~]# cat an-10.yml
# user={{ key.value }}
- hosts: webservers
  tasks:
    - name: "错误任务"
      shell: asdaafiefncnac
      ignore_errors: yes
    - name: "创建用户组"
      group:
        name: "{{ item }}"
        state: present
      loop:
        - gp1
        - gp2
        - gp3

    - name: "创建用户并设置基本组"
      user:
        name: "{{ item.uname }}"
        group: "{{ item.group }}"
        state: present
      loop:
        - { uname: test1, group: gp1 }
        - { uname: test2, group: gp2 }
        - { uname: test3, group: gp3 } 
[root@ansible ~]# ansible-playbook an-10.yml

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [错误任务] *****************************************************************************************************************************************
fatal: [192.168.92.20]: FAILED! => {"changed": true, "cmd": "asdaafiefncnac", "delta": "0:00:00.005330", "end": "2026-03-29 18:04:48.901292", "msg": "non-zero return code", "rc": 127, "start": "2026-03-29 18:04:48.895962", "stderr": "/bin/sh: line 1: asdaafiefncnac: command not found", "stderr_lines": ["/bin/sh: line 1: asdaafiefncnac: command not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [创建用户组] ***************************************************************************************************************************************
ok: [192.168.92.20] => (item=gp1)
ok: [192.168.92.20] => (item=gp2)
ok: [192.168.92.20] => (item=gp3)

TASK [创建用户并设置基本组] *****************************************************************************************************************************
ok: [192.168.92.20] => (item={'uname': 'test1', 'group': 'gp1'})
ok: [192.168.92.20] => (item={'uname': 'test2', 'group': 'gp2'})
ok: [192.168.92.20] => (item={'uname': 'test3', 'group': 'gp3'})

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

错误任务还是失败了,但是跳过了错误任务,后面的还是在运行

相关推荐
未济4 天前
linux 配置环境变量
linux
傲世仙尊4 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.4 天前
进程间通信
linux
AI职业加油站4 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-054 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏4 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz4 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅4 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK4 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid