ansible常用模块用法

本文介绍了Ansible中一些常用的模块,包括copy、file、cron、group、user、yum、service、script、ping、command、raw、get_url、synchronize等,详细说明了它们的用途和使用命令,帮助读者理解如何在自动化运维中应用这些模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ansible 默认提供了很多模块来供我们使用。在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用。

       下面介绍比较常用的几个模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
copy模块
file 模块
cron 模块
group模块
user模块
yum模块
service模块
script模块
ping 模块
command 模块
raw模块
get_url模块
synchronize模块




copy模块:

    目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上

    命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/'

    执行效果:

wKioL1TB_EWytdwHAALpQ_NZj_Y676.jpg




file模块:

    目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root

    命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"

    执行效果:

spacer.gifwKiom1TB-23S6GIaAAFkf3LCV0I648.jpg


cron模块:

    目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间

    命令:ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'

    执行效果:

wKioL1TB_EXAZ9npAAKlZcJk-2A313.jpg


group模块:

    目的:在所有节点上创建一个组名为nolinux,gid为2014的组

    命令:ansible all -m group -a 'gid=2014 name=nolinux'

    执行效果:

spacer.gifwKiom1TB-22w2EHsAAEyGxpaZ1Q696.jpg


user模块:

    目的:在指定节点上创建一个用户名为nolinux,组为nolinux的用户

    命令:ansible 10.1.1.113 -m user -a 'name=nolinux groups=nolinux state=present'

    执行命令:

wKioL1TB_EajzhgWAAKh8udIwfU616.jpg

补充:删除用户示例

spacer.gifwKiom1TB-27BpMDGAAJUaZVZKlQ882.jpg


yum模块:

    目的:在指定节点上安装 lrzsz 服务

    命令:ansible all -m yum -a "state=present name=httpd"

    执行效果:

wKioL1TB_EaTCw3xAAo06fOJT9o358.jpg


service模块:

    目的:启动指定节点上的 puppet 服务,并让其开机自启动

    命令:ansible 10.1.1.113 -m service -a 'name=puppet state=restarted enabled=yes'

    执行效果:

wKiom1TB-27BK1j6AAE7uhMosLQ824.jpgspacer.gif


script模块:

    目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)

    命令:ansible 10.1.1.113 -m script -a '/root/a.sh'

    执行效果:

wKioL1TB_EehJLUgAAFvF7DUzTA542.jpgspacer.gif


ping模块:

    目的:检查指定节点机器是否还能连通

    命令:ansible 10.1.1.113 -m ping

    执行效果:

wKioL1TB_EfDOcX3AAD56pSs8vw739.jpg


command模块:

    目的:在指定节点上运行hostname命令

    命令:ansible 10.1.1.113 -m command -a 'hostname'

    执行效果:

wKiom1TB-2_TVLsoAAH9514heiw532.jpgspacer.gif


raw模块:

    目的:在10.1.1.113节点上运行hostname命令

    命令:ansible 10.1.1.113 -m raw-a 'hostname|tee'

    执行效果:

wKioL1TB_EfiXSlvAAD6X2f-nZ4937.jpg


get_url模块:

    目的:将http://10.1.1.116/favicon.ico文件下载到指定节点的/tmp目录下

    命令:ansible 10.1.1.113 -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'

    执行效果:

wKiom1TB_ZvyFnXpAAQItOAiI4M141.jpg


synchronize模块:

    目的:将主控方/root/a目录推送到指定节点的/tmp目录下

    命令:ansible 10.1.1.113 -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'

    执行效果:

delete=yes   使两边的内容一样(即以推送方为主)

compress=yes  开启压缩,默认为开启

--exclude=.git  忽略同步.git结尾的文件

wKioL1TB_EjxkmYpAAYA5Ai3IPg889.jpg

由于模块,默认都是推送push。因此,如果你在使用拉取pull功能的时候,可以参考如下来实现

mode=pull   更改推送模式为拉取模式

    目的:将10.1.1.113节点的/tmp/a目录拉取到主控节点的/root目录下

    命令:ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'

    执行效果:

wKiom1TB-3CwhN2dAASKTtTpOxI903.jpg

       由于模块默认启用了archive参数,该参数默认开启了recursive, links, perms, times, owner,group和-D参数。如果你将该参数设置为no,那么你将停止很多参数,比如会导致如下目的递归失败,导致无法拉取

wKiom1TB-3CwM88JAAKGdhDvVqI952.jpg

其它相关的参数解释:

1
2
3
dest_port=22     # 指定目的主机的ssh端口,ansible配置文件中的 ansible_ssh_port 变量优先级高于该 dest_port 变量
rsync_path       # 指定 rsync 命令来在远程服务器上运行。这个参考rsync命令的--rsync-path参数,--rsync-path=PATH     # 指定远程服务器上的rsync命令所在路径信息
rsync_timeout    # 指定 rsync 操作的 IP 超时时间,和rsync命令的 --timeout 参数效果一样


OK!

       以上暂且列举这些日常运维中经常会用到的一些模块,更多的模块信息后期会继续完善,大家也可以去官网查看更多的信息。

       官网地址:http://docs.ansible.com/synchronize_module.html

### Ansible 常用模块 Ansible 提供了大量的内置模块,用于执行各种任务。以下是部分常用模块及其功能: #### 1. `copy` 模块模块的主要作用是将本地文件复制到远程主机上[^4]。 ```yaml - name: Copy file to remote server copy: src: /path/to/local/file dest: /path/to/remote/file owner: user group: group mode: '0644' ``` #### 2. `file` 模块模块可以创建、修改或删除文件、目录和链接等资源。 ```yaml - name: Create a directory file: path: /path/to/directory state: directory owner: user group: group mode: '0755' ``` #### 3. `yum` 和 `apt` 模块 这两个模块分别用于管理基于 RedHat/CentOS (YUM) 和 Debian/Ubuntu (APT) 系统中的软件包安装状态。 ```yaml - name: Install httpd using YUM yum: name: httpd state: present ``` #### 4. `service` 模块 通过这个模块可以启动、停止或者重启服务。 ```yaml - name: Restart Apache service service: name: httpd state: restarted ``` #### 5. `template` 模块 允许用户利用 Jinja2 模板引擎生成配置文件并将其部署至目标机器。 ```yaml - name: Template a file to /etc/foo.conf template: src: /templates/etc/foo.conf.j2 dest: /etc/foo.conf owner: root group: root mode: '0644' ``` --- ### Playbook 使用教程 Playbooks 是 YAML 文件形式的任务清单集合,它们描述了一组要运行的操作序列。 #### 创建第一个 Playbook 下面是一个简单的例子,展示如何编写一个基本的 Playbook 来更新服务器上的软件包缓存,并确保 Nginx 已经被安装且正在运行: ```yaml --- - hosts: all become: yes tasks: - name: Update apt cache and install nginx apt: update_cache: yes name: nginx state: latest - name: Start the nginx service service: name: nginx state: started enabled: yes ``` 保存上述内容为 `nginx.yml` 后可以通过如下命令执行: ```bash ansible-playbook -i inventory_file nginx.yml ``` 其中 `-i` 参数指定了库存文件的位置;如果没有指定,默认会查找当前工作目录下的 `hosts` 或 `/etc/ansible/hosts`. #### Tags 功能的应用 为了更灵活地控制哪些 task 执行,在定义 Task 的时候还可以加上标签(tag),这样就能单独挑选某些特定标记的任务来运行了[^1]: ```yaml - name: Ensure apache is at the latest version yum: name: httpd state: latest tags: - webserver ``` 之后只需加参数即可只跑带某 tag 的 job : ```bash ansible-playbook site.yml --tags "webserver" ``` #### Handler 的触发机制 Handlers 类似于 Tasks ,只是当某个条件满足时才会被执行一次 。比如监听器通常用来响应服务变化后的重新加载动作[^3]: ```yaml handlers: - name: restart apache service: name: httpd state: restarted tasks: - name: Configure something that needs restarting later... notify: restart apache ``` 一旦任何带有 `notify` 属性的任务完成了更改,则对应的 handler 将会被激活。 --- ### 结论 以上介绍了几个常见的 Ansible 模块以及 Playbook 的基础概念与实际应用案例。希望这些信息可以帮助您更好地理解和运用 Ansible 自动化工具!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值