Ansible 的核心模块是 Ansible 默认提供的一组最基本和常用的模块。这些模块涵盖了各种常见的任务,如文件管理、包管理、系统配置等。以下是一些 Ansible 的核心模块:
1、命令执行模块:
command
: 用于在远程主机上执行简单的命令。它不支持管道、重定向和通配符等 shell 功能。例如:- name: Execute a command command: /usr/bin/hostname
shell
: 与command
类似,但支持管道、重定向和通配符等 shell 功能。例如:- name: Execute a shell command shell: cat /etc/hosts | grep localhost
script
: 用于在远程主机上运行本地脚本。例如:- name: Run a local script on remote hosts script: /path/to/local/script.sh
raw
: 用于执行低级别的 SSH 命令,通常用于在 Python 未安装的情况下执行命令。例如:- name: Execute a raw command raw: apt-get install -y python
2、文件管理模块:
copy
: 用于将文件从本地复制到远程主机。例如:- name: Copy a file to remote hosts copy: src: /path/to/local/file dest: /path/to/remote/file
file
: 用于管理文件和目录的属性,如权限、所有权等。例如:- name: Create a directory file: path: /path/to/directory state: directory mode: '0755'
lineinfile
: 用于在文件中插入、更新或删除特定行。例如:- name: Add a line to a file lineinfile: path: /path/to/file line: 'New line content'
blockinfile
: 用于在文件中插入、更新或删除多行文本块。例如:- name: Add a block of text to a file blockinfile: path: /path/to/file block: | Line 1 Line 2 Line 3
template
: 用于使用 Jinja2 模板渲染文件并复制到远程主机。例如:- name: Render a template and copy to remote hosts template: src: template.j2 dest: /path/to/remote/file
3、包管理模块:
yum
: 用于管理 Red Hat 系列系统的软件包。例如:- name: Install a package using yum yum: name: httpd state: present
apt
: 用于管理 Debian 和 Ubuntu 系统的软件包。例如:- name: Install a package using apt apt: name: apache2 state: present
pip
: 用于管理 Python 软件包。例如:- name: Install a Python package using pip pip: name: requests state: present
4、系统管理模块:
- name: Create a new cron job
cron:
name: "Daily backup"
minute: "0"
hour: "1"
job: "/path/to/backup/script.sh"
- name: Modify an existing cron job
cron:
name: "Daily backup"
minute: "30"
hour: "2"
job: "/path/to/backup/script.sh"
- name: Remove a cron job
cron:
name: "Daily backup"
state: absent
- name: Comment out a cron job
cron:
name: "Daily backup"
disabled: yes
在上面的示例中:
cron
模块的其他一些常用参数包括:
user
: 用于管理用户账号。例如:- name: Create a user user: name: johndoe password: "{{ 'password' | password_hash('sha512') }}"
group
: 用于管理用户组。例如:- name: Create a group group: name: developers state: present
cron
模块用于管理目标主机上的 cron 任务。cron 是 Unix/Linux 系统中的一个定时任务调度器,允许用户在指定的时间间隔执行特定的命令或脚本。下面是一个使用cron
模块的示例:第一个任务使用
cron
模块创建一个名为 "Daily backup" 的新 cron 任务,该任务将在每天凌晨 1 点运行/path/to/backup/script.sh
脚本。第二个任务修改现有的 "Daily backup" cron 任务,将其执行时间更改为每天凌晨 2 点 30 分。
第三个任务通过将
state
参数设置为absent
,删除名为 "Daily backup" 的 cron 任务。第四个任务通过将
disabled
参数设置为yes
,注释掉名为 "Daily backup" 的 cron 任务,使其不再执行。user
: 指定要为其创建 cron 任务的用户。job
: 指定要执行的命令或脚本。minute
、hour
、day
、month
、weekday
: 指定 cron 任务的执行时间。special_time
: 指定特殊的时间表,如reboot
、annually
等。state
: 指定 cron 任务的状态,可以是present
(默认)或absent
。disabled
: 指定是否禁用 cron 任务,可以是yes
或no
(默认)。
5、网络管理模块:
get_url
: 用于从指定的 URL 下载文件到远程主机。例如:- name: Download a file from URL get_url: url: https://example.com/file.tar.gz dest: /path/to/save/file.tar.gz
uri
: 用于与 HTTP/HTTPS REST API 进行交互。例如:- name: Make an HTTP GET request uri: url: https://api.example.com/endpoint method: GET return_content: yes
6、调试模块:
debug
: 用于打印调试信息,可以输出变量的值或自定义消息。例如:- name: Print a debug message debug: msg: "The value of variable is {{ variable_name }}"
assert
: 用于检查条件是否满足,如果不满足则中止 playbook 的执行。例如:- name: Assert that a condition is true assert: that: - result.rc == 0 - "'success' in result.stdout"
7、条件和控制模块:
wait_for
: 用于等待特定条件满足后再继续执行。例如:- name: Wait for a port to become available wait_for: port: 80 timeout: 60
pause
: 用于暂停 playbook 的执行,等待用户输入后再继续。例如:- name: Pause for user input pause: prompt: "Press Enter to continue"
fail
: 用于故意使 playbook 执行失败并显示错误信息。例如:- name: Fail with a custom message fail: msg: "Something went wrong"
when
: 用于根据条件决定是否执行某个任务。例如:- name: Execute a task conditionally command: /path/to/script.sh when: ansible_os_family == 'Debian'
8、信息收集模块:
setup
: 用于收集目标主机的系统信息,如 IP 地址、操作系统、硬件信息等。例如:- name: Gather facts about the target hosts setup:
gather_facts
: 用于控制是否收集目标主机的系统信息。例如:- hosts: all gather_facts: no
9、变量管理模块:
set_fact
: 用于在 playbook 中设置自定义变量。例如:- name: Set a custom fact set_fact: custom_variable: "custom_value"
add_host
: 用于在 playbook 运行时动态添加主机到 inventory 中。例如:- name: Add a new host to inventory add_host: name: new_host groups: web_servers
10、任务控制模块:
import_tasks
: 用于在当前 playbook 中导入另一个 task 文件。例如:- name: Import tasks from another file import_tasks: tasks/common_tasks.yml
include_tasks
: 与import_tasks
类似,但是在运行时动态地包含 task 文件。例如:- name: Include tasks based on a condition include_tasks: tasks/conditional_tasks.yml when: some_condition
这些核心模块提供了 Ansible 的基础功能,可以满足大多数常见的自动化任务需求。除了这些核心模块,Ansible 还有许多其他模块,用于处理更具体的任务,如云平台管理、数据库管理、通知等。
你可以通过运行 ansible-doc -l
命令来查看 Ansible 中所有可用的模块列表。同时,你也可以使用 ansible-doc <module_name>
命令来查看特定模块的详细文档和用法。
import_playbook
:用于在当前 playbook 中导入另一个 playbook 文件。例如:
- name: Import another playbook import_playbook: playbooks/another_playbook.yml
include_playbook
:与import_playbook类似,但是在运行时动态地包含 playbook 文件。
例如:- name: Include a playbook based on a variable include_playbook: "playbooks/{{ playbook_name }}.yml"