0.Ansible的group支持all、通配符(*)、IP地址

1.查看Ansible的版本

$  ansbile  --version

[root@test ~]# ansible --version

ansible 1.7.2

[root@test ~]#

2.消除首次ssh登录时要求输入yes确认

在所有机器上修改/etc/ssh/ssh_config文件中设置StrictHostKeyChecking no即可(默认为 ask )

[root@master ~]# grep "StrictHostKeyChecking" /etc/ssh/ssh_config

StrictHostKeyChecking no

3.拷贝文件

ansible <groupname> -m copy -a "src=/etc/ssh/ssh_config dest=/etc/ssh/ssh_config owner=root group=root mode=644 force=yes"

示例:

[root@test ~]# ansible all -m copy -a "src=/etc/ssh/ssh_config dest=/etc/ssh/ssh_config owner=root group=root mode=644 force=yes"

192.168.91.135 | success >> {

"changed": true,

"dest": "/etc/ssh/ssh_config",

"gid": 0,

"group": "root",

"md5sum": "d27d7cc9767512b64c84d06544e01546",

"mode": "0644",

"owner": "root",

"size": 2072,

"src": "/root/.ansible/tmp/ansible-tmp-1476168613.68-180284084445387/source",

"state": "file",

"uid": 0

}

[root@test ~]#

4.查看所有机器的磁盘情况

ansible <groupname> -m shell -a "df -h" -k

5.测试机器的连通性

ansible <groupname> -m ping

6.Ansible所有的模块

      http://docs.ansible.com/ansible/list_of_all_modules.html

7.将本机上的配置文件组装发送到远程主机

ansible <groupname> -m assemble -a "src=/root/configure dest=/root/test/a.conf remote_src=False

8.将本机上的配置文件组装发送到远程主机,带分隔符

ansible <groupname> -m assemble -a "src=/root/configure dest=/root/test/a.conf remote_src=False delimiter='####'"

9.设定权限进行拷贝

ansible <groupname> -m copy -a "src=/root/configure/a.conf dest=/root/test owner=root group=root mode=0777"

10.拷贝的时候备份

ansible <groupname> -m copy -a "src=/root/kel/1 dest=/tmp/kel owner=root group=root backup=yes"

11.拷贝文件之后进行验证

ansible <groupname> -m copy -a "src=/etc/sudoers dest=/tmp/2 validate='visudo -cf %s'"

12. fetch一个文件进行保存

ansible <groupname> -m fetch -a "src=/root/123 dest=/root"

13.指定路径目录进行保存

ansible <groupname> -m fetch -a "src=/root/Ssh.py dest=/root/kel/ flat=yes"

14. 设置文件属性

ansible <groupname> -m file -a "path=/root/123 owner=kel group=kel mode=0644"

15.创建目录

ansible <groupname> -m file -a "path=/tmp/kel state=directory mode=0755"

16.修改权限

ansible <groupname> -m file -a "path=/tmp/kel mode=0444"

17.创建软连接

ansible <groupname> -m file -a "src=/tmp/1 dest=/tmp/2 owner=kel state=link"

18.添加其中的节的值

ansible <groupname> -m ini_file -a "dest=/tmp/kel section=kel option=kel value=kel mode=0600 backup=yes"

19.基础模块使用

19.1 并行和Shell命令

设置ssh-agent记住认证

$ ssh-agent bash

$ ssh-add ~/.ssh/id_rsa

19.2  10秒内重启

$
ansible <groupname> -a "/sbin/reboot" -f 10

19.3 在默认情况下,ansible使用的是当前用户,当你需要使用其他用户的时候,可以使用选项-u
username

$ ansible <groupname> -a
"/usr/bin/foo" -u username

19.4  要使用sudo的时候

$
ansible <groupname> -a "/usr/bin/foo" -u username --sudo[--ask-sudo-pass]

--ask-sudo-pass(-K)此选项是用来询问sudo的密码,如果设置了,如果未设置,那么无需使用

19.5  也可以在sudo到别的用户来进行执行

$
ansible <groupname> -a "/usr/bin/foo" -u
username -U otheruser[--ask-sudo-pass]

参数-f 10表示并发进行,也就是10个进程同时运行,在使用的时候,默认的情况下为5,选择合适的数据,从而使得系统能够进行处理

参数-m表示选择的模块,在默认情况下,command是默认的模块

Command不适用于有shell变量的情况,也不适用于有管道符的情况,如果要使用此种情况,那么可以使用shell模块

19.6  使用Shell模块

$
ansible <groupname> -m shell -a 'echo $TERM'

20.  文件传输

20.1  传输文件到很多主机

$ ansible <groupname> -m
copy -a "src=/etc/hosts dest=/tmp/hosts"

20.2  修改用户和用户组权限

$ ansible <groupname> -m
file -a "dest=/srv/foo/a.txt mode=600"

$ ansible <groupname> -m
file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

20.3   创建目录

$ ansible <groupname> -m
file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan
state=directory"

21. 管理包

21.1   确定一个包已经安装,但是不更新

$ ansible <groupname> -m
yum -a "name=acme state=present"

21.2   确定一个包安装的是指定版本

$ ansible <groupname>
-m yum -a "name=acme-1.5 state=present"

21.3  确定一个包是最新包

$ ansible <groupname>
-m yum -a "name=acme state=latest"

21.4   确定一个包未安装

$ ansible <groupname>
-m yum -a "name=acme state=absent"

22. 用户和用户组

22.1   创建用户和管理已经存在的用户和组

$ ansible <groupname>
-m user -a "name=foo password=<crypted password here>"

$ ansible <groupname>
-m user -a "name=foo state=absent"

23. 服务管理

23.1 确定一个服务正在运行

$ ansible <groupname> -m
service -a "name=httpd state=started"

23.2  重启一个服务

$ ansible <groupname>
-m service -a "name=httpd state=restarted"

23.3   确定一个服务是停止的

$ ansible <groupname>
-m service -a "name=httpd state=stopped"

24.限制后台运行时间

24.1   后台运行总是耗费比较长的时间,从而其状态在随后总是能够查看的,如果踢掉主机,又不想轮训

$ ansible <groupname>
-B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"

24.2   如果要检查服务的状态,可以使用模块async_status,传递job id

$ ansible <groupname>
-m async_status -a "jid=488359678239.2844"

24.3   轮训是内建的

$ ansible <groupname> -B
1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

参数-B表示运行最多30分钟,30*60,-P 60 轮训其状态每60S,

当时间运行在-B参数后的时间之后,此服务会被停止运行。

可以使用参数—forksvalue,来确保服务尽快运行

25.收集信息

$ ansible <groupname> -m
setup

来自 <http://www.cnblogs.com/LuisYang/p/5961001.html>

3.2、Ansible单命令测试的更多相关文章

  1. Ansible学习记录四:单命令测试

    0.Ansible的group支持all.通配符(*).IP地址 1.查看Ansible的版本 $  ansbile  --version [root@test ~]# ansible --versi ...

  2. kafka单节点测试

    ======================命令====================== 启动zookeeper server bin/zookeeper-server-start.sh conf ...

  3. centos ansible常用命令

    ansible在日常运维中经常使用,特别是批量执行多台服务器的时候,有效减小重复的操作成本,以下从安装到使用仅讲解工作中常用的几种方式,模块很多功能很强大,但不做全面讨论. ansible安装 在ce ...

  4. 用命令测试安装好的OpenStack环境

    OpenStack三个节点icehouse-gre模式部署一文部署了一套OpenStack环境,接下来使用命令测试一遍. 首先要明确几个概念: 外网:可分配floating ip绑定到虚拟机,外部就可 ...

  5. USB系列之七:ASPI介绍及命令测试

    在以前的一篇博文<关于构建DOS下编程平台的总结>中曾经介绍了一种在DOS下驱动U盘的方法,我们大致回顾一下.在config.sys中加入两个驱动程序,就可以驱动U盘:device = a ...

  6. ansible常用命令

    一.ansible常用命令 一.ansible命令的常用参数 ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansib ...

  7. Linux下使用DD命令测试磁盘读写速度

    dd是Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换,所以可以用来测试硬盘的读写能力~ 几种常见的DD命令,先看一下区别~ dd bs=6 ...

  8. Python实现telnet命令测试防火墙

    Python实现telnet命令测试防火墙 telnet主要用于测试主机端口是否开通 ping主要是用来测试网络是否畅通和主机是否正在使用 使用Python实现Telnet测试主机端口是否开通的功能. ...

  9. phpSpider 单页测试_模拟登陆

    <?php require './vendor/autoload.php'; use phpspider\core\phpspider; use phpspider\core\requests; ...

随机推荐

  1. Spring深入理解(一)

    Spring 框架的设计理念与设计模式分析 Spring核心组件 Spring 框架中的核心组件只有三个:Core.Context 和 Beans Spring 的设计理念 前面介绍了 Spring ...

  2. F - Count the Colors

    F - Count the Colors ZOJ - 1610   思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...

  3. mysql 服务器监控系列-黄杉 mysqldba

    http://blog.csdn.net/mchdba/article/category/2220809

  4. Java经典线程同步问题------生产者与消费者

    先上代码 class Test { public static void main(String []args) { Queue q=new Queue(); Producer p=new Produ ...

  5. nyoj-20-吝啬的国度(深搜)

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 在一个吝啬的国度里有N个城市.这N个城市间仅仅有N-1条路把这个N个城市连接起来.如今,Tom在第S号城市 ...

  6. ant整合junit自己主动化測试

    一. 使用Junit进行測试 1. Java业务代码: public class HelloWorld { // 測试返回"world" public String hello() ...

  7. maven+springMVC+mybatis+easyUI管理用户增删改查

    1.项目演示图 2.项目简单介绍 项目分为两个projectdomain和manager.project结构例如以下图所看到的.当中domain是Maven javaproject主要完毕对数据库的操 ...

  8. 编程算法 - 数组中的逆序对 代码(C)

    数组中的逆序对 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 在数组中的两个数字假设前面一个数字大于后面的数字, 则这两个数字组成一个逆序对. ...

  9. graphviz.js的图形及属性简单用法

    digraph A { graph[bgcolor="cadetblue" label="图的标题" fontsize=48 fontcolor="g ...

  10. oc3--类方法1

    // // main.m // 第一个OC类-方法 #import <Foundation/Foundation.h> /* C语言中函数分为声明和实现,OC中定义类, 就是在写类的声明和 ...