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. 收到微软寄来的MVP包裹

    经过一年多的艰苦努力,最终在7月份获得了微软SharePoint的MVP. 今天收到了微软从美国寄来的包裹. 内部图: 奖牌: 回忆过去一年.白天和客户撕逼,晚上回家写博客,或者翻译英文文章,去论坛回 ...

  2. POJ 2019 Cornfields 二维线段树的初始化与最值查询

    模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...

  3. QQ好友列表数据模型封装

    QQ好友中的信息较多.假设我们单独从plist 中直接取出数据 是能够解决这个问题 可是相当复杂.以为列表中分组 .每组中还有不同信息 大致模型是 数组套数组  数组套字典 所以我们要封装数据模型 / ...

  4. JavaFX本地应用自己主动更新功能的实现FXLauncher

    JavaFX本地应用自己主动更新功能的实现--FXLauncher 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 一. ...

  5. Qt 3D的研究(十):描边渲染(轮廓渲染)以及Silhouette Shader

    Qt 3D的研究(十):描边渲染(轮廓渲染)以及Silhouette Shader 之前写了两篇文章,介绍了我在边缘检測上面的研究.实际上.使用GPU对渲染图像进行边缘检測.前提是须要进行两遍渲染.前 ...

  6. 关于Mysql Enterprise Audit plugin的使用

    正如之前看到的一篇文章,假设想要知道是谁登陆了你的数据库server,干了什么东西,那么你须要使用Mysql Enterprise Audit plugin. 以下介绍一下Mysql Enterpri ...

  7. strcpy函数使用方法以及底层实现

    strcpy(s1, s2);   strcpy函数的意思是:把字符串s2中的内容copy到s1中.连字符串结束标志也一起copy. 这样s1在内存中的存放为:ch\0; 在cout<<s ...

  8. 黑马day07 登录注冊案例(一)

    简单介绍:依据三层架构的思想设计本案例. 1.搭建好开发环境 准备好须要的包和模拟数据库配置文件users.xml -->cn.itheima.dao -->cn.itheima.serv ...

  9. jquery非文本框复制

    function selectText(x) { if (document.selection) { var range = document.body.createTextRange();//ie ...

  10. Linux命令(七)——网络配置和网络通信

    在使用网络前,需要对linux主机进行基本的网络配置,配置后可以使该主机能够同其他主机进行正常的通信. 一.网络配置 1.ifcfg-ethn网络配置文件 所有的网络接口配置文件均存放在/etc/sy ...