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

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

相关推荐
闻哥2 小时前
Docker Swarm 负载均衡深度解析:VIP vs DNSRR 模式详解
java·运维·jvm·docker·容器·负载均衡
YMWM_2 小时前
【问题修复】ubuntu24.04打不开windows的D盘
linux
淼淼爱喝水2 小时前
Ansible Ad-Hoc 命令基础实战(Linux 系统)
linux·服务器·数据库
@土豆2 小时前
混合云组网-基于公有云产品实现(非开源方法)
运维·网络·开源
yy_xzz2 小时前
【Linux开发】04Linux 线程的销毁
linux
HenryLiuu2 小时前
Ubuntu 20.04, cuda 12.1版本安装flash attention 2教程
linux·ubuntu
wd5i8kA8i10 小时前
自研多线程 SSH 极速文件传输助手(附 GitHub 源码)
运维·ssh·github
Boop_wu10 小时前
[Java 算法] 字符串
linux·运维·服务器·数据结构·算法·leetcode
菱玖12 小时前
SRC常见漏洞情况分类
运维·安全·安全威胁分析