1. 文件操作:
复制文件到目标主机:
- name: Copy a file
ansible.builtin.copy:
src: /path/to/source/file
dest: /path/to/destination/file
创建目录:
- name: Create a directory
ansible.builtin.file:
path: /path/to/directory
state: directory
删除文件或目录:
- name: Remove a file or directory
ansible.builtin.file:
path: /path/to/file_or_directory
state: absent
2. 软件包管理:
安装软件包:
- name: Install a package
ansible.builtin.package:
name: package_name
state: present
卸载软件包:
- name: Remove a package
ansible.builtin.package:
name: package_name
state: absent
3. 服务管理:
启动服务:
- name: Start a service
ansible.builtin.service:
name: service_name
state: started
停止服务:
- name: Stop a service
ansible.builtin.service:
name: service_name
state: stopped
重新启动服务:
- name: Restart a service
ansible.builtin.service:
name: service_name
state: restarted
4. 用户管理:
创建用户:
- name: Create a user
ansible.builtin.user:
name: username
state: present
修改用户密码:
- name: Change user password
ansible.builtin.user:
name: username
password: "{{ 'new_password' | password_hash('sha512') }}"
删除用户:
- name: Remove a user
ansible.builtin.user:
name: username
state: absent
5. 远程命令执行:
执行 Shell 命令:
- name: Run a shell command
ansible.builtin.shell: ls -l /path/to/directory
执行远程脚本:
- name: Run a remote script
ansible.builtin.script:
src: /path/to/script.sh
6. 系统信息:
获取主机信息:
- name: Gather system facts
ansible.builtin.setup:
显示主机名:
- name: Display hostname
ansible.builtin.debug:
var: ansible_hostname
7. 网络配置:
配置网络接口:
- name: Configure network interface
ansible.builtin.interface:
name: eth0
state: up
配置静态路由:
- name: Configure static route
ansible.builtin.route:
dest: 10.0.0.0
netmask: 255.255.255.0
gateway: 192.168.1.1
8. 文件压缩和解压:
压缩文件或目录:
- name: Compress a file or directory
ansible.builtin.archive:
path: /path/to/source
dest: /path/to/destination/file.tar.gz
format: gz
解压文件或目录:
- name: Extract an archive
ansible.builtin.unarchive:
src: /path/to/source/file.tar.gz
dest: /path/to/destination
remote_src: yes
9. 数据库操作:
执行 SQL 查询:
- name: Execute SQL query
ansible.builtin.mysql_db:
login_user: db_user
login_password: db_password
database: db_name
state: query
query: "SELECT * FROM table_name;"
创建数据库:
- name: Create a database
ansible.builtin.mysql_db:
login_user: db_user
login_password: db_password
name: new_db_name
state: present
10. 日志记录和输出:
记录日志:
- name: Log a message
ansible.builtin.debug:
msg: "This is a log message."
打印变量值:
- name: Print variable value
ansible.builtin.debug:
var: my_variable
11. 添加主机
- name: 添加主机到 Ansible 清单
hosts: localhost
tasks:
- name: 添加新主机
ansible.builtin.add_host:
hostname: "{{ dynamic_hostname }}" # 新主机的主机名
ansible_ssh_host: "{{ dynamic_ip }}" # 新主机的 IP 地址
delegate_to: localhost # 在本地主机上执行此任务
- name: 在新主机上执行命令
hosts: "{{ dynamic_hostname }}" # 使用新添加的主机
tasks:
- name: 执行命令
command: echo "Hello from the new host"
12. ansible.builtin.archive:
说明:该模块用于创建和提取存档文件,支持常见的存档格式如 tar 和 zip。
示例用法:创建一个 tar 存档文件。
- name: 创建 tar 存档文件
ansible.builtin.archive:
src: /path/to/source
dest: /path/to/destination/archive.tar
format: tar
13.ansible.builtin.assert:
说明:此模块用于添加断言,以验证任务的状态或条件。如果断言失败,任务将失败。
示例用法:验证文件是否存在。
- name: 验证文件存在
ansible.builtin.assert:
that:
- "ansible_facts['file_exists'] == True"
vars:
ansible_facts:
file_exists: true
14.ansible.builtin.async_status:
说明:用于检查异步任务的状态。
示例用法:检查异步任务的状态。
- name: 检查异步任务状态
ansible.builtin.async_status:
jid: "{{ async_result.ansible_job_id }}"
register: async_check
- debug:
var: async_check
15.ansible.builtin.at:
说明:用于在特定时间执行任务的模块。
示例用法:使用 at 模块执行命令。
- name: 使用 at 模块执行命令
ansible.builtin.at:
command: "echo 'Hello, world' > /tmp/hello.txt"
count: 1
units: hours
value: 1
job_file: /tmp/at_job.txt
16.ansible.builtin.atd:
说明:用于启用或禁用远程主机上的 at 服务。
示例用法:启用 at 服务。
- name: 启用 at 服务
ansible.builtin.atd:
state: started