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. 公众号和app和web都是客户端,都可以对接一个后台

    1.公众号和app和web都是客户端,都可以对接一个后台 2.域名中包含端口号吗?:不包括,不包括 3.目前在IIS服务器上搭建了一个网站,域名也申请了,可是80端口不能使用,可以使用8000,每次访 ...

  2. ReentrantLock公平锁与非公平锁lock()方法去竞争锁的不同点

  3. POJ 1654

    不知 是怎么看出的精度不够,吸经验吧. #include <iostream> #include <cstdio> #include <cstring> #incl ...

  4. Docker入门介绍

    Docker是一种虚拟化技术       刚開始看Docker,感觉非常抽象"An open platform for distributed applications for develo ...

  5. 具体解释linux文件处理的的经常使用命令

    原创Blog.转载请注明出处 附上之前訪问量比較高的几篇linux博客 本人使用shell的8个小技巧 grep的九个经典使用场景 sed命令具体解释 awk命令具体解释 linux中全部的东西都是文 ...

  6. Android多线程断点下载

    到华为后,信息管理特别严格,文件不能外发.所以好久都没写博客了,今天周日,老婆非要我学习.就闲来无事,写一篇博客,呵呵-- 前段时间,项目中提到了断点下载apk并静默安装的需求.本打算用应用市场成熟的 ...

  7. CSS学习(十六)-HSLA颜色模式

    一.理论: 1.HSLA颜色模式 a.HSLA在HSL基础上添加了不透明度,值越大透明度越低 b.HSLA颜色模式的浏览器兼容性和HSL一样,仅仅有较新版本号的主流浏览器才支持 2.RGBA和HSLA ...

  8. 【POJ 3696】 The Luckiest number

    [题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...

  9. java javax.annotation.Resource注解的详解

    转自:https://www.jb51.net/article/95456.htm java 注解:java javax.annotation.Resource  当我们在xml里面为类配置注入对象时 ...

  10. configparser (配置文件) 模块

    主要内容来自景女神博客 内涵:该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 常见文档格式: [DEFAULT] ...