Ansible force_handlers delegate委托 playbook语法格式 template模块

force_handlers

复制代码
[root@ansible ~]# vi an-15.yml
[root@ansible ~]# cat an-15.yml
- hosts: webservers
  tasks:
    - name: "创建文件"
      shell: touch /root/aa.txt
      notify: aa

    - name: "错误任务"
      shell: systemctl restart yuiopyuio

  handlers:
    - name: aa
      service:
        name: httpd
        state: restarted
[root@ansible ~]# ansible-playbook an-15.yml

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

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

TASK [创建文件] ***************************************************************                                                                          **************************************************************************
changed: [192.168.92.20]

TASK [错误任务] ***************************************************************                                                                          **************************************************************************
fatal: [192.168.92.20]: FAILED! => {"changed": true, "cmd": "systemctl restart                                                                           yuiopyuio", "delta": "0:00:00.031268", "end": "2026-03-29 20:33:19.189710", "ms                                                                          g": "non-zero return code", "rc": 5, "start": "2026-03-29 20:33:19.158442", "st                                                                          derr": "Failed to restart yuiopyuio.service: Unit yuiopyuio.service not found."                                                                          , "stderr_lines": ["Failed to restart yuiopyuio.service: Unit yuiopyuio.service                                                                           not found."], "stdout": "", "stdout_lines": []}

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

[root@ansible ~]# vi an-15.yml
[root@ansible ~]# cat an-15.yml
- hosts: webservers
  force_handlers: yes
  tasks:
    - name: "创建文件"
      shell: touch /root/aa.txt
      notify: aa

    - name: "错误任务"
      shell: systemctl restart yuiopyuio

  handlers:
    - name: aa
      service:
        name: httpd
        state: restarted
[root@ansible ~]# ansible-playbook an-15.yml

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

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

TASK [创建文件] *****************************************************************************************************************************************
changed: [192.168.92.20]


TASK [错误任务] *****************************************************************************************************************************************
fatal: [192.168.92.20]: FAILED! => {"changed": true, "cmd": "systemctl restart yuiopyuio", "delta": "0:00:00.021511", "end": "2026-03-29 20:35:14.990162", "msg": "non-zero return code", "rc": 5, "start": "2026-03-29 20:35:14.968651", "stderr": "Failed to restart yuiopyuio.service: Unit yuiopyuio.service not found.", "stderr_lines": ["Failed to restart yuiopyuio.service: Unit yuiopyuio.service not found."], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [aa] ************************************************************************************************************************************
changed: [192.168.92.20]

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

第二次编辑an-15.yml文件的时候,加了force_handlers: yes,虽然错误任务没有运行起来,但是触发器成功运行了

delegate委托

下面这张图是an-16.yml

delegate通过委托完成功能而不用新加一条hosts记录,也不用单独写出来一个playbook文件

playbook语法格式

复制代码
[root@ansible ~]# ansible webservers -m user -a "name=a1 password=123456"
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work
properly.
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1204,
    "home": "/home/a1",
    "name": "a1",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1204
} 
[root@ansible ~]# vi an-17.yml 
[root@ansible ~]# cat an-17.yml
- hosts: webservers
  tasks:
    - name: "创建用户,指令参数"
      user:
        name: a2
        password: 123456

    - name: "创建用户,把指令参数写作模块属性"
      user:
        name: a3
        password: 123456

[root@ansible ~]# ansible-playbook an-17.yml

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

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

TASK [创建用户,指令参数] ***************************************                                                                         *****************************************************************                                                                         ********
[WARNING]: The input password appears not to have been hashed. Th                                                                         e 'password' argument must be encrypted for this module to work
properly.
changed: [192.168.92.20]

TASK [创建用户,把指令参数写作模块属性] *************************                                                                         *****************************************************************                                                                         ********
changed: [192.168.92.20]

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

template模块

AnsibleJinJa2

什么是jinja2

。是Python的全功能模板引擎

。ansible需要是要jinja2模板修改被控端的配置文件

。假设:现在5台机器需要安装nginx,每一个主机端口号不一样

。解决方法:可以是要jinja2模板引擎功能:template

如何是要jinja2

。ansible需要是要jinja2,需要借助:template模块实现

复制代码
copy模块:
    它是把指定文件内容,直接复制给指定目标,可以是覆盖替换,里面内容不会发生任何改变
    配置文件内的内容不需要发生改变,就可以是要copy
template模块:
    它会内容做解析:会分析文件,如果你的文件内有变量的,它会帮你把变量转换为对应的
    数值,然后再做覆盖和替换
    如果这个文件分发且每个设备需要的数据不一样,可以定义变量,用template做解析下发

[root@ansible ~]# vi test.yml 
[root@ansible ~]# cat test.yml
- hosts: webservers
  vars:
    - my_port: 80
  tasks:
    - name: "copy"
      copy:
        src: /root/httpd.conf
        dest: /opt/httpd.conf

    - name: "template"
      template:
        src: /root/httpd.conf
        dest: /root/httpd.conf 
[root@ansible ~]# vi httpd.conf 
Listen这里修改为Listen {{ my_port }}
[root@ansible ~]# ansible-playbook test.yml

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

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

TASK [copy] ******************************************************************************************************************************
changed: [192.168.92.20]

TASK [template] **************************************************************************************************************************
changed: [192.168.92.20]

PLAY RECAP *******************************************************************************************************************************
192.168.92.20              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
被控端192.168.92.20 /root/httpd.conf下显示Listen 80
被控端192.168.92.20 /opt/httpd.conf下显示Listen {{ my_port }}

template模块: 它会内容做解析:会分析文件,如果你的文件内有变量的,它会帮你把变量转换为对应的 数值,然后再做覆盖和替换,copy不行

相关推荐
Gofarlic_oms115 小时前
利用API实现ANSYS许可证管理自动化集成
运维·服务器·开发语言·matlab·自动化·负载均衡
档案宝档案管理16 小时前
权限分级管控,全程可追溯,筑牢会计档案安全防线
运维·网络·人工智能
倔强的石头10617 小时前
【Linux指南】基础IO系列(八):实战衔接 —— 给微型 Shell 添加完整重定向功能
linux·运维·服务器
try2find17 小时前
打印ascii码报错问题
java·linux·前端
观北海18 小时前
AiScan-N:AI全自动化渗透测试工具的深度技术解析
运维·自动化
Ujimatsu18 小时前
虚拟机安装Ubuntu 26.04.x及其常用软件(2026.4)
linux·运维·ubuntu
道清茗19 小时前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》常见问题
ansible
一直会游泳的小猫21 小时前
homebrew
linux·mac·工具·包管理
Agent产品评测局21 小时前
制造业生产调度自动化落地,完整步骤与避坑指南:2026企业级智能体选型与实战全景
运维·人工智能·ai·chatgpt·自动化
寒秋花开曾相惜21 小时前
(学习笔记)4.2 逻辑设计和硬件控制语言HCL(4.2.1 逻辑门&4.2.2 组合电路和HCL布尔表达式)
linux·网络·数据结构·笔记·学习·fpga开发