模块(也被称为 task plugins 或 library plugins)是在 Ansible 中实际在执行的。它们就是在每个 playbook 任务中被执行的。你也可以仅仅通过 ansible 命令来运行它们。
一、模块的使用方法
让我们回顾一下我们是如何通过命令行来执行三个不同的模块:
1 2 3 |
ansible webservers -m service -a "name=httpd state=started" ansible webservers -m ping ansible webservers -m command -a "/sbin/reboot -t now" |
每个模块都能接收参数。几乎所有的模块都接受键值对(key=value)参数,空格分隔。一些模块不接收参数,只需在命令行输入相关的命令就能调用!
1.1、在 playbook 中,Ansible 模块以类似的方式执行
1 2 |
- name: reboot the servers action: command /sbin/reboot -t now |
也可以简写成:
1 2 |
- name: reboot the servers command: /sbin/reboot -t now |
1.2、另一种给模块传递参数的方式是使用yaml语法,这也被称为complex args
1 2 3 4 |
- name: restart webserver service: name: httpd state: restarted |
无论你是使用命令行还是playbooks所有模块都会返回以JSON格式组织的数据。一般来说,对此你无需知道太多。但如果你在编写你自己的模块,那你需要在意,这意味着你不需要以特定的语言来编写你的模块 – 你可以自行选择。
模块努力使自身幂等,这意味着它们会尽可能避免对系统做出改动除非那是必须的。当使用 Ansible playbooks 时,这些模块能够触发 change events,以这种形式通知 handlers 去运行附加任务。
二、查找模块
ansible模块存放于以下位置(一般在第三个里面)
1 2 3 |
~/.ansible/plugins/modules /usr/share/ansible/plugins/modules /usr/lib/python2.7/site-packages/ansible/modules |
2.1、查看所有模块
1 |
[root@imzcy ~]# ansible-doc -l |
2.2、查看某个模块的参数
1 |
[root@imzcy ~]# ansible-doc -s module |
2.3、查看该模块更详细的信息
1 |
[root@imzcy ~]# ansible-doc help module |
本文学习自 Ansible中文权威指南:
1 |
http://www.ansible.com.cn/docs/modules.html |