ansible.builtin(内置模块)

概述

参考:

根据 2.9 版本的模块索引文档,我们可以将上面这些内置模块进行类别划分

Commands - 命令模块

官方文档:https://docs.ansible.com/ansible/2.9/modules/list_of_commands_modules.html

command - 在受管理节点上执行命令

官方文档: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html

script - 将本地脚本传输到受管理节点上并运行

官方文档: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html

Files - 文件处理模块

详见《Files 处理模块

Packaging - 包模块

官方文档:https://docs.ansible.com/ansible/2.9/modules/list_of_packaging_modules.html

Language

OS

yum - 使用主机上的 yum 工具管理包

官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html

参数

  • name(STRING) # 包的名称
  • state(STRING) # 指定要执行的操作,是安装还是移除包。可用的值有如下几个:
    • absent 与 removed # 移除指定的包
    • installed 与 present # 简单得确保安装了指定的包
    • latest # 安装最新版本的包,若当前包不是最新版本,则更新它。

应用示例

安装 bash-completion 与 vim 的最新版本的包

ansible all -m yum -a "name=net-bash-completion,vim state=latest"
- name: 安装运维工具
  yum:
    name: ['bash-completion','vim']
    state: latest

System - 系统模块

详见 《System 模块

Utilities - 实用程序模块

Helper

Logic

debug - 输出变量或表达式

官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html

该模块可以在 playbook 执行期间,输出指定的内容,而不用停止 playbook。常用于调试变量或者表达式。比如使用 shell 模块的时候,可以通过 debug 模块来输出 shell 模块定义的语句的执行结果

常与 when 指令一起使用。

- name: Print the gateway for each host when defined
  ansible.builtin.debug:
    msg: System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}
  when: ansible_default_ipv4.gateway is defined

- name: Get uptime information
  ansible.builtin.shell: /usr/bin/uptime
  register: result

- name: Print return information from the previous task
  ansible.builtin.debug:
    var: result
    verbosity: 2

- name: Display all variables/facts known for a host
  ansible.builtin.debug:
    var: hostvars[inventory_hostname]
    verbosity: 4

- name: Prints two lines of messages, but only if there is an environment value set
  ansible.builtin.debug:
    msg:
      - "Provisioning based on YOUR_KEY which is: {{ lookup('env', 'YOUR_KEY') }}"
      - "These servers were built using the password of '{{ password_used }}'. Please retain this for later use."

fail - 终止任务的执行,并输出自定义的消息

官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fail_module.html

import 与 include 模块 - 在主任务中导入其他任务或变量

参考:

随着要管理的服务不断增多,我们的 Playbook 也会越来越大,内容越来越多,管理起来也会随着复杂。这时,我们可以将某些 tasks 分散到很多文件中,通过 import 和 include 相关模块,实现 tasks 文件之间的相互调用。

说白了,就是聚合多个文件的 tasks。

一共有如下几种模块可以实现这类功能:

  • import_playbook
  • import_role #
  • import_tasks # Static(静态) 方法,在 playbooks 解析阶段将父 task 变量和子 task 变量全部读取并加载
  • include
  • include_tasks # Dynamic(动态) 方法,在执行 play 之前才会加载自己变量
  • include_vars

动态与静态导入 tasks 的区别

区别一

  • import_tasks(Static) 方法会在 playbooks 解析阶段将父 task 变量和子 task 变量全部读取并加载
  • include_tasks(Dynamic) 方法则是在执行 play 之前才会加载自己变量

可能有点懵,举例说明

下面两个例子是 test.yml 里的 task 调用 test2.yml,不同之处是一个使用 import_tasks 另一个使用 include_tasks

import_tasks:在执行 tasks 之前,ansible 解释器会先加载 test.yml 里的变量同时再加载 test2.yml 里的变量,那么 ansible_os_family 变量会有一个覆盖现象产生,最终的参数应为“BlackHat”,所以当 test.yml 里执行 when 语句时,ansible_os_family 被判定为“BlackHat”,when 的判断结果为 false,也就不会调用 test2.yml 了

执行结果如下,test2.yml 里的 task 都被 skip 了:

include_tasks:ansible 会在完全执行完 test.yml 里的 task 后才会加载 test2.yml 里的变量,所以当执行 when 语句时,ansible_os_family 的参数应为“RedHat”,此时 when 语句判断结果是 true,也就是 test2.yml 里的 tasks 会被执行。

将第一张图 test.yaml 里的 import_tasks 换成 include_tasks,执行结果如下:

我们发现自 test2.yml 里的 task 被执行了,并且在 test2.yml 里 ansible_os_family 的参数变为了“BlackHat”。

这就是 include_tasts 和 import_tasks 方法的第一个区别。

区别二

  • include_tasks 方法调用的文件名称可以加变量
  • import_tasks 方法调用的文件名称不可以有变量

这个区别比较简单,直接上示例:

当调用的文件名称有变量时,使用 include_tasks 方法:

能够正常调用 test2.yml,执行结果如下:

当使用 import_tasks 方法时,执行报错。

ansible 也给出了错误原因,当使用 static include 时,是不能使用变量的:

这就是 include_tasts 和 import_tasks 方法的第二个区别。


最后修改 February 25, 2025: ansible, clickhouse (2282fe84)