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    #查看系统变量

4.1、Ansible模块的更多相关文章

  1. ansible模块

    ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...

  2. ansible笔记(3):ansible模块的基本使用

    ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...

  3. 第4天:Ansible模块

    Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...

  4. ansible模块command、shell、raw、script

    简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...

  5. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  6. Ansible 模块命令介绍

    copy模块: 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/' ...

  7. ansible 模块 分享

    A a10_server 管理A10 Networks AX / SoftAX / Thunder / vThunder设备 a10_service_group 管理A10网络设备的服务组 a10_v ...

  8. ansible 模块

    1. #vim /etc/ansible/yaml/back.yml - hosts: siyi tasks: - name: "yum rsync" yum: name=rsyn ...

  9. ansible模块学习

    ansible的功能: 模块化任务,调用特定的模块,完成特定的任务 基于python语言实现,由paramiko.pyyaml和jinja2三个模块构建 部署简单,agentless,ansible基 ...

随机推荐

  1. Python - 检測字符串之间的包括

    检測字符串之间的包括 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27048955 Python中, 能够检測字符串之间的包括问 ...

  2. 【待解决】An internal error occurred during: "Launching baiduTest1". java.lang.NullPointerException

    编写的一个Java类,以junit运行可以正常执行,以testNG执行就报如下错误 解决方法:

  3. java生成一张图片

    public class CreateImage { public static void main(String[] args) throws Exception{ int width = 100; ...

  4. Composer 很重要很重要 内核 原理

    话题先攒着,过来再来写 先来一张原理图 composer的原理和其他的包管理工具都是一样的,只是实现的细节有些不同,例如yum,例如brew,例如apt-get还有packets. 使用自己的comp ...

  5. [ASPX] 模版引擎XTemplate与代码生成器XCoder(源码)

    模版引擎XTemplate是一个仿T4设计的引擎,功能上基本与T4一致(模版语法上完全兼容T4,模版头指令部分兼容). 自己设计模版引擎,就是为了代码生成器.网站模版.邮件模版等多种场合,也就是要能拿 ...

  6. cas-client-core单点登录排除不需要拦截的URL

    同事提了一个要求,要求对外提供的接口不需要经过单点登录验证,我刚开始想,这简单,提供不需要拦截的url数组,在AuthenticationFilter里面对url进行检查,在此数组内,就不需要拦截. ...

  7. CharSequence源码分析

    CharSequence是一个接口,表示一个char值的可读序列,此接口为多种char序列提供统一的.只读的通道.既然是接口,就不能通过new来进行赋值,只能通过以下方式赋值: CharSequenc ...

  8. linux编译安装ccache3.2.4

    1.下载ccache3.2.4安装包 #cd /opt #wget http://samba.org/ftp/ccache/ccache-3.2.4.tar.gz 2.解压 #.tar.gz 3.创建 ...

  9. Git Learning Part II - Working locally

    file status life circle basic: modified:   Examples: untracked: unmodified: modified: Git branching ...

  10. Android媒体解码MediaCodec,MediaExtractor

    Android提供了MediaPlayer播放器播放媒体文件,其实MediaPlyer只是对Android Media包下的MediaCodec和MediaExtractor进行了包装,方便使用.但是 ...