Ansible 模块和集合
Ansible 模块
Ansible 通过模块执行任务,例如 ansible.builtin.hostname
用于设置主机名。
ansible.builtin
是模块的命名空间,不同的模块属于不同的命名空间,一般情况下模块名称不会冲突,但是随着模块数量的增加,不排除模块名冲突的可能,所以将不同的模块放在不同的命名空间里用于区分,如果控制节点安装的模块没有模块名冲突的情况,调用模块时可以不写命名空间,如:hostname
。Ansible 是 Python 写的,所以 Ansible 的大部分模块都需要被控主机有 Python 才能使用,少部分模块除外,如:
ansible.builtin.raw
。
以下是一些常用的模块。
文件与目录操作模块
模块名 | 功能描述 |
---|---|
ansible.builtin.copy |
复制文件到远程主机 |
ansible.builtin.template |
渲染 Jinja2 模板并传输 |
ansible.builtin.fetch |
从远程主机获取文件 |
ansible.builtin.file |
设置文件权限、属主等 |
ansible.builtin.stat |
获取文件状态 |
ansible.builtin.unarchive |
解压归档文件 |
ansible.builtin.lineinfile |
修改文件中的一行内容 |
ansible.builtin.blockinfile |
向文件中插入多个行 |
系统管理模块
模块名 | 功能描述 |
---|---|
ansible.builtin.hostname |
设置主机名 |
ansible.builtin.user |
创建/修改/删除用户 |
ansible.builtin.group |
创建/修改/删除用户组 |
ansible.builtin.service |
管理系统服务 |
ansible.builtin.systemd |
管理 systemd 服务 |
ansible.builtin.package |
安装/卸载通用软件包(自动选择后端) |
ansible.builtin.yum |
使用 yum 安装包 |
ansible.builtin.apt |
使用 apt 安装包 |
ansible.builtin.dnf |
使用 dnf 安装包 |
命令执行模块
模块名 | 功能描述 |
---|---|
ansible.builtin.command |
运行普通命令(不支持管道、重定向) |
ansible.builtin.shell |
运行 shell 命令(支持管道、重定向等) |
ansible.builtin.raw |
原始命令执行,不走模块系统 |
ansible.builtin.script |
将本地脚本上传并执行 |
归档与压缩
模块名 | 功能描述 |
---|---|
ansible.builtin.archive |
创建归档文件(如 tar.gz) |
ansible.builtin.unarchive |
解压归档文件 |
权限与认证
模块名 | 功能描述 |
---|---|
ansible.builtin.seboolean |
管理 SELinux 布尔值 |
ansible.builtin.selinux |
设置 SELinux 模式 |
ansible.builtin.authorized_key |
添加 SSH 公钥到用户 ~/.ssh/authorized_keys |
网络与远程连接
模块名 | 功能描述 |
---|---|
ansible.builtin.uri |
发 HTTP 请求 |
ansible.builtin.get_url |
下载文件 |
ansible.builtin.wait_for |
等待端口/文件状态变更 |
控制逻辑模块
模块名 | 功能描述 |
---|---|
ansible.builtin.debug |
输出调试信息 |
ansible.builtin.pause |
暂停执行 |
ansible.builtin.assert |
条件断言 |
ansible.builtin.set_fact |
设置自定义变量 |
这些模块都可以通过
ansible-doc <模块名>
来查看帮助文档,可以通过搜索EXAMPLE
来快速了解模块的使用,也可以通过ansible-doc -s <模块名>
查看模块选项。
举个例子,ansible.builtin.systemd
模块用于服务管理,通过 ansible-doc
查看模块文档并搜索 EXAMPLE
。
[root@awx-1 ansible]# ansible-doc ansible.builtin.systemd
# 用 /EXAMPLE 搜索
EXAMPLES:
- name: Make sure a service unit is running
ansible.builtin.systemd:
state: started
name: httpd
- name: Stop service cron on debian, if running
ansible.builtin.systemd:
name: cron
state: stopped
- name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes
ansible.builtin.systemd:
state: restarted
daemon_reload: true
name: crond
- name: Reload service httpd, in all cases
ansible.builtin.systemd:
name: httpd.service
state: reloaded
- name: Enable service httpd and ensure it is not masked
ansible.builtin.systemd:
name: httpd
enabled: true
masked: no
- name: Enable a timer unit for dnf-automatic
ansible.builtin.systemd:
name: dnf-automatic.timer
state: started
enabled: true
- name: Just force systemd to reread configs (2.4 and above)
ansible.builtin.systemd:
daemon_reload: true
- name: Just force systemd to re-execute itself (2.8 and above)
ansible.builtin.systemd:
daemon_reexec: true
- name: Run a user service when XDG_RUNTIME_DIR is not set on remote login
ansible.builtin.systemd:
name: myservice
state: started
scope: user
environment:
XDG_RUNTIME_DIR: "/run/user/{{ myuid }}"
很多选项都能做到见文知意,比方说 enabled
用于设置是否开机自启,state
用于设置服务状态。
更多模块可以查看官网:https://docs.ansible.com/ansible/latest/collections/all_plugins.html
想要搜索模块也可以在这搜索:https://galaxy.ansible.com/ui/
关于模块,后边在单独写一下常用模块及其常用选项。
Ansible 集合
Ansible-core 自带一组模块,以 ansible.builtin
开头,这些模块是远远不够的,需要其他模块来扩展 Ansible 的功能,为了方便这些模块的管理,所以将这些扩展模块分类,一类模块就组成一个集合。
# 下载集合
ansible-galaxy collection download community.general