ansible模块

ansible模块:

模块(Modules),类似于 “任务插件“(”task plugins”)或“库插件”(“library plugins”)的东西。

模块 可以单独的被ansible命令调用;也可以在palybook的每个任务中被执行。

每个模块都能接收参数(arguments),绝大部分arguments的格式是key=value,用空格隔开。

一些如 ansible servers -m command -a "/sbin/reboot -t now",不是key=value格式,而是直接传shell命令。

命令行调用:

ansible webservers -m service -a "name=httpd state=started"
ansible webservers -m ping
ansible webservers -m command -a "/sbin/reboot -t now"

命令行中的-m表示调用哪个模块,-a表示模块的参数(arguments)。

palybook中调用:

- name: reboot the servers
  action: command /sbin/reboot -t now

- name: reboot the servers
  command: /sbin/reboot -t now // 可省略action,等同于action: command 

- name: restart webserver
  service: name=httpd state=restarted

也可以用yaml的语法传入参数:

- name: restart webserver
  service:
    name: httpd
    state: restarted

所以的模块都有统一的json格式的返回值。

模块是等幂的

避免使用模块改变,模块必须是幂等的(idempotent,不改变的)???
模块会尽可能避免对系统作出改动,除非必须要改动的时候。

当使用playbooks时, 模块能够触发 ‘change events’,触发后会通知 ‘handlers’ 去运行附加任务。

了解模块的方法:

ansible-doc 模块名称ansible-doc -l列出所有模块。

常用模块:

setup 查看主机的一些基本信息
ping 测试主机的运行状态
file 文件操作
copy 复制文件到主机
command 在主机上执行命令
shell 切换到shell,执行指定的指令

其他模块:

service:系统服务管理
cron:计划任务管理
yum:yum软件包安装管理
synchronize:使用rsync同步文件
user:系统用户管理
group:系统用户组管理

统一返回值

  • backup_file
  • changed
  • failed
  • invocation
  • msg
  • rc
  • results
  • skipped
  • stderr
  • stderr_lines
  • stdout
  • stdout_lines
原文地址:https://www.cnblogs.com/iois/p/6216936.html