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. Windows 10不能正常打开开始菜单问题修复

    1.可以尝试通过命令重新注注册Windows Store app: powershell -ExecutionPolicy Unrestricted Add-AppxPackage -DisableD ...

  2. vbs 脚本2

    一些很恶作剧的vbs程序代码 作者: 字体:[增加 减小] 类型:转载 时间:2013-01-16我要评论 恶作剧的vbs代码,这里提供的都是一些死循环或导致系统死机的vbs对机器没坏处,最多关机重启 ...

  3. iOS 隐藏NavigationBar的方法

    使用下面方法: - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationCon ...

  4. 程序员(媛)Shell脚本必备技能之中的一个: 在Linux下怎样自己主动备份mysql数据

    作者:荀彧(花名) Background:鉴于QAS数据库曾遭到毁灭性损害.极其桑心L.为了避免这样的玉石俱焚的状况再度发生.于是我们找出了一种应对方案.这里给大家分享一下怎样实现定时备份mysql数 ...

  5. 微软继MVC5后,出现ASP.NET VNEXT

    vNext又称MVC 6.0,不再须要依赖System.Web.占用的内存大大降低(从前不管是多么简单的一个请求.System.Web本身就要占用31KB内存). 能够self-host模式执行.站点 ...

  6. 轻快的vim(一):移动

    断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 前几个星期把Windows换成了Linux Mint,基本上也稳定了下来 就今晚,我已经下定决心开始新的VIM之旅,顺便写一系列的笔记 ...

  7. Linux VGA驱动移植实验【转】

    本文转载自:http://m.blog.csdn.net/bzw73/article/details/46564275 有了前面的LCD驱动的框架,再移植VGA驱动就相当的容易了.默认在光盘中已经支持 ...

  8. spark rdd median 中位数求解

    lookup(key) Return the list of values in the RDD for key key. This operation is done efficiently if ...

  9. php获取uniqid

    md5(uniqid(microtime(true),true))

  10. [javaEE] Eclipse 默认设置的换行长度

    eclipse 默认设置的换行长度, 格式化代码后,经常换行,非常难看. 1.Java代码 打开Eclipse的Window菜单,然后 Preferences->Java->Code St ...