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

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

相关推荐
vortex518 小时前
Debian 包管理全指南:从底层 dpkg 到高层 apt 及其日志追踪
linux·运维·debian
偶尔上线经常挺尸18 小时前
《每日一命令08:scp——安全的远程复制》
linux·安全·scp·文件传输·运维基础·远程复制
计算机安禾19 小时前
【Linux从入门到精通】第17篇:日志系统——系统运行的黑匣子
linux·运维·服务器
l1t19 小时前
DeepSeek辅助解决windows 11 wsl2中Linux版Dbeaver显示中文
linux·运维·windows
pengyi87101520 小时前
独享IP+动态IP结合核心逻辑,破解稳定与灵活的矛盾
linux·运维·网络
阿祖zu20 小时前
本地到生产,解决 AI 全栈最后一公里——构建&部署&运维
运维·架构·aigc
MAVER1CK1 天前
Install VNC in Docker container
运维·docker·容器
橘颂TA1 天前
【Linux】读写锁
大数据·linux·开发语言·c++·读写锁
Strange_Head1 天前
补充知识点`makefile`、`config`、`GLP协议` 1/3 ——《驱动篇》
linux·嵌入式硬件
lcj09246661 天前
数据中心运维升级|磁控U位硬件联动DCIM,破解U位管控难题
运维·人工智能·经验分享·信息可视化