ansible.builtin(内置模块)
概述
参考:
- ansible.builtin.add_host – Add a host (and alternatively a group) to the ansible-playbook in-memory inventory
- ansible.builtin.apt – Manages apt-packages
- ansible.builtin.apt_key – Add or remove an apt key
- ansible.builtin.apt_repository – Add and remove APT repositories
- ansible.builtin.assemble – Assemble configuration files from fragments
- ansible.builtin.assert – Asserts given expressions are true
- ansible.builtin.async_status – Obtain status of asynchronous task
- ansible.builtin.blockinfile – Insert/update/remove a text block surrounded by marker lines
- ansible.builtin.command – Execute commands on targets
- ansible.builtin.copy – Copy files to remote locations
- ansible.builtin.cron – Manage cron.d and crontab entries
- ansible.builtin.debconf – Configure a .deb package
- ansible.builtin.debug – Print statements during execution
- ansible.builtin.dnf – Manages packages with the dnf package manager
- ansible.builtin.dpkg_selections – Dpkg package selection selections
- ansible.builtin.expect – Executes a command and responds to prompts
- ansible.builtin.fail – Fail with custom message
- ansible.builtin.fetch – Fetch files from remote nodes
- ansible.builtin.file – Manage files and file properties
- ansible.builtin.find – Return a list of files based on specific criteria
- ansible.builtin.gather_facts – Gathers facts about remote hosts
- ansible.builtin.get_url – Downloads files from HTTP, HTTPS, or FTP to node
- ansible.builtin.getent – A wrapper to the unix getent utility
- ansible.builtin.git – Deploy software (or files) from git checkouts
- ansible.builtin.group – Add or remove groups
- ansible.builtin.group_by – Create Ansible groups based on facts
- ansible.builtin.hostname – Manage hostname
- ansible.builtin.import_playbook – Import a playbook
- ansible.builtin.import_role – Import a role into a play
- ansible.builtin.import_tasks – Import a task list
- ansible.builtin.include – Include a play or task list
- ansible.builtin.include_role – Load and execute a role
- ansible.builtin.include_tasks – Dynamically include a task list
- ansible.builtin.include_vars – Load variables from files, dynamically within a task
- ansible.builtin.iptables – Modify iptables rules
- ansible.builtin.known_hosts – Add or remove a host from the known_hosts file
- ansible.builtin.lineinfile – Manage lines in text files
- ansible.builtin.meta – Execute Ansible ‘actions’
- ansible.builtin.package – Generic OS package manager
- ansible.builtin.package_facts – Package information as facts
- ansible.builtin.pause – Pause playbook execution
- ansible.builtin.ping – Try to connect to host, verify a usable python and return pong on success
- ansible.builtin.pip – Manages Python library dependencies
- ansible.builtin.raw – Executes a low-down and dirty command
- ansible.builtin.reboot – Reboot a machine
- ansible.builtin.replace – Replace all instances of a particular string in a file using a back-referenced regular expression
- ansible.builtin.rpm_key – Adds or removes a gpg key from the rpm db
- ansible.builtin.script – Runs a local script on a remote node after transferring it
- ansible.builtin.service – Manage services
- ansible.builtin.service_facts – Return service state information as fact data
- ansible.builtin.set_fact – Set host variable(s) and fact(s).
- ansible.builtin.set_stats – Define and display stats for the current ansible run
- ansible.builtin.setup – Gathers facts about remote hosts
- ansible.builtin.shell – Execute shell commands on targets
- ansible.builtin.slurp – Slurps a file from remote nodes
- ansible.builtin.stat – Retrieve file or file system status
- ansible.builtin.subversion – Deploys a subversion repository
- ansible.builtin.systemd – Manage systemd units
- ansible.builtin.sysvinit – Manage SysV services.
- ansible.builtin.tempfile – Creates temporary files and directories
- ansible.builtin.template – Template a file out to a target host
- ansible.builtin.unarchive – Unpacks an archive after (optionally) copying it from the local machine
- ansible.builtin.uri – Interacts with webservices
- ansible.builtin.user – Manage user accounts
- ansible.builtin.validate_argument_spec – Validate role argument specs.
- ansible.builtin.wait_for – Waits for a condition before continuing
- ansible.builtin.wait_for_connection – Waits until remote system is reachable/usable
- ansible.builtin.yum – Manages packages with the yum package manager
- ansible.builtin.yum_repository – Add or remove YUM repositories
根据 2.9 版本的模块索引文档,我们可以将上面这些内置模块进行类别划分
Commands - 命令模块
官方文档:https://docs.ansible.com/ansible/2.9/modules/list_of_commands_modules.html
- command – Execute commands on targets
- expect – Executes a command and responds to prompts
- psexec – Runs commands on a remote Windows host based on the PsExec model
- raw – Executes a low-down and dirty command
- script – Runs a local script on a remote node after transferring it
- shell – Execute shell commands on targets
- telnet – Executes a low-down and dirty telnet command
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
- bower – Manage bower packages with bower
- bundler – Manage Ruby Gem dependencies with Bundler
- composer – Dependency Manager for PHP
- cpanm – Manages Perl library dependencies
- easy_install – Installs Python libraries
- gem – Manage Ruby gems
- maven_artifact – Downloads an Artifact from a Maven Repository
- npm – Manage node.js packages with npm
- pear – Manage pear/pecl packages
- pip – Manages Python library dependencies
- pip_package_info – pip package information
- yarn – Manage node.js packages with Yarn
OS
- apk – Manages apk packages
- apt – Manages apt-packages
- apt_key – Add or remove an apt key
- apt_repo – Manage APT repositories via apt-repo
- apt_repository – Add and remove APT repositories
- apt_rpm – apt_rpm package manager
- dnf – Manages packages with the dnf package manager
- dpkg_selections – Dpkg package selection selections
- flatpak – Manage flatpaks
- flatpak_remote – Manage flatpak repository remotes
- homebrew – Package manager for Homebrew
- homebrew_cask – Install/uninstall homebrew casks
- homebrew_tap – Tap a Homebrew repository
- installp – Manage packages on AIX
- layman – Manage Gentoo overlays
- macports – Package manager for MacPorts
- openbsd_pkg – Manage packages on OpenBSD
- opkg – Package manager for OpenWrt
- package – Generic OS package manager
- package_facts – package information as facts
- pacman – Manage packages with pacman
- pkg5 – Manages packages with the Solaris 11 Image Packaging System
- pkg5_publisher – Manages Solaris 11 Image Packaging System publishers
- pkgin – Package manager for SmartOS, NetBSD, et al
- pkgng – Package manager for FreeBSD >= 9.0
- pkgutil – Manage CSW-Packages on Solaris
- portage – Package manager for Gentoo
- portinstall – Installing packages from FreeBSD’s ports system
- pulp_repo – Add or remove Pulp repos from a remote host
- redhat_subscription – Manage registration and subscriptions to RHSM using the subscription-manager command
- rhn_channel – Adds or removes Red Hat software channels
- rhn_register – Manage Red Hat Network registration using the rhnreg_ks command
- rhsm_release – Set or Unset RHSM Release version
- rhsm_repository – Manage RHSM repositories using the subscription-manager command
- rpm_key – Adds or removes a gpg key from the rpm db
- slackpkg – Package manager for Slackware >= 12.2
- snap – Manages snaps
- sorcery – Package manager for Source Mage GNU/Linux
- svr4pkg – Manage Solaris SVR4 packages
- swdepot – Manage packages with swdepot package manager (HP-UX)
- swupd – Manages updates and bundles in ClearLinux systems
- urpmi – Urpmi manager
- xbps – Manage packages with XBPS
- yum – Manages packages with the yum package manager
- yum_repository – Add or remove YUM repositories
- zypper – Manage packages on SUSE and openSUSE
- zypper_repository – Add and remove Zypper repositories
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
- assert – Asserts given expressions are true
- async_status – Obtain status of asynchronous task
- debug – Print statements during execution
- fail – Fail with custom message
- import_playbook – Import a playbook
- import_role – Import a role into a play
- import_tasks – Import a task list
- include – Include a play or task list
- include_role – Load and execute a role
- include_tasks – Dynamically include a task list
- include_vars – Load variables from files, dynamically within a task
- pause – Pause playbook execution
- set_fact – Set host facts from a task
- set_stats – Set stats for the current ansible run
- wait_for – Waits for a condition before continuing
- wait_for_connection – Waits until remote system is reachable/usable
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 方法的第二个区别。
反馈
此页是否对你有帮助?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.