4.1、Ansible模块

ansible-doc -l    列出所有模块

ansible-doc 模块名    查看模块的help说明

ansible-doc -s  module_name:获取指定模块的使用信息

***文件管理模块***

(1)copy 管理机复制到节点

ansible test -m copy -a "dest=/tmp src=/root/test.txt force=yes"    #dest目的地,src源文件,force=yes覆盖文件

(2)fetch 节点复制到管理机

ansible test -m copy -a "dest=/root src=/tmp/test.txt force=yes"

(3)file

ansible test -m file -a 'path=/tmp/test.txt state=touch'  # 创建文件

ansible test -m file -a "path=/tmp/test.txt state=absent"    #删除test.txt文件

path=文件路径,state=默认值 file创建文件、directory创建文件夹、link创建软连接、hard创建硬链接、absent删除文件,src=文件链接路径,mode=文件权限,owner=属主,group=属组,recurse=yes递归设置属性

(4)stat    获取远程文件状态信息

ansible test -m stat -a "path=/etc/sysctl.conf"

(5)setup 获取远程主机的facts

ansible test -m setup

 

***命令执行模块***

(1)command

ansible的默认模块,该模块获取不到环境变量,管道和重定向都不能使用

chdir=执行前先进入的某个目录,creates=文件,假如文件存在则不会执行命令,removes=文件,假如文件不存在则不会执行命令


[user@localhost ~]$ ansible clong -m command -a 'echo $TERM'   
192.168.0.148 | SUCCESS | rc=0 >>
linux

(2)shell

默认情况下,ansible使用的module 是 command,这个模块并不支持 shell 变量和管道等,若想使用shell 来执行模块,请使用-m 参数指定 shell 模块,但是值得注意的是普通的命令执行模块是通过python的ssh执行。

使用shell模块在远程主机上执行命令:

ansible web -m shell -a ‘echo $TERM’

[user@localhost ~]$ ansible clong -m shell -a 'echo $TERM'       
192.168.0.148 | SUCCESS | rc=0 >>
linux

(3)raw

Raw也是命令执行模块,而raw模块则是直接用ssh模块进行执行,通常用在客户机还没有python的环境的时候。

使用raw模块在远程主机上执行命令:

ansible web -m raw -a ‘echo $TERM’

[user@localhost ~]$ ansible clong -m raw -a 'echo $TERM'
192.168.0.148 | SUCCESS | rc=0 >>
linux
Shared connection to 192.168.0.148 closed.

(4)script

执行脚本模块,脚本只在管理主机上存在就可以,会自动下发到节点主机。

ansible test -m script -a "test.sh"

 

***网络相关***

(1)ping    #测试节点是否连通

(2)get_url    #用于下载网络上的文件

ansible test -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'    #把http://10.1.1.116/favicon.ico下载到/tmp

(3)uri    #用于发送HTTP协议,让节点主机向指定的网址发送Get、Post这样的HTTP请求,并且返回状态码

 

***包管理模块***

(1)yum

使用yum模块时,管理机设置yum源就好

例:

ansible test -m yum -a "name=httpd state=present"    #安装apache

ansible test -m yum -a "name=httpd state=absent"    #删除apache

ansible test -m yum -a "name=httpd state=latest"    #更新apache

ansible test -m yum -a "name=httpd enablerepo=testing state=absent"    #用testing这个repo安装apache

ansible test -m yum -a "name=* state=latest"    #upgrade all packages

ansible test -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"    #install the nginx rpm from a remote repo

ansible test -m yum -a "name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"    #install nginx rpm from a local file

ansible test -m yum -a "name="@Development tools" state=present"    #install the 'Development tools' package group

(2)pip和easy_install模块

(3)APT

 

***系统管理***

(1)service

ansible test -m service -a "name=httpd state=restarted"

state=started|stopped|restarted|reloaded

(2)group

ansible test -m group -a "name=test state=present"    #添加test组

gid=用户组的GID,system=yes/no是否是系统组,state=present/absent

(3)user

Linux用户管理模块

ansible-doc user    或者参考《Ansible权威指南》P58-59

例:

ansible test -m user -a "name=test shell=/bin/bash groups=admin,dba append=yes home=/home/test state=present"    #新建test用户,group指定属组,groups指定附加组,'groups='删除所有附加组,append=yes增量添加属组,state默认present,表示新建。

ansible test -m user -a "name=test groups=dba append=no"    #test用户附加组改为dba,append=no全量变更属组信息。

ansible test -m user -a "name=test1 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"    #generate_ssh_key=yes生成SSH key,ssh_key_bits指定加密位数,默认2048,ssh_key_file指定key文件位置,默认.ssh/id_rsa,相对路径表示家目录下

ansible test -m user -a "name=test state=absent remove=yes"    #删除test用户,state=absent表示删除用户,remove=yes结合state=absent相当于userdel --remove

ansible test -m user -a "name=test password=eRgUloPQMARVY"    #变更用户密码,使用加密密码。

加密方式:

(a)书中介绍的mkpasswd,在redhat下不行,好像ubuntu下可以,我用的openssl,测试redhat下可行。

openssl passwd  "redhat123"

eRgUloPQMARVY

(b)使用python的passlib、getpass库

(4)setup

ansible all -m setup    #查看系统变量

 

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/7200430.html