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

Ansible学习记录四:单命令测试的更多相关文章

  1. Ansible学习记录二:命令

    0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...

  2. JavaScript学习记录四

    title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  3. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  4. 4.VUE前端框架学习记录四:Vue组件化编码2

    VUE前端框架学习记录四:Vue组件化编码2文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  5. ansible 学习记录

    Ansible 的重新学习记录 这里我的Ansible的宿主机是centos 7.2系统,这里我通过yum 安装Ansible 1.配置epel源 sudo yum -y install epel-r ...

  6. C++ primer学习记录(个人猜想未测试版本)

    学习版本:第五版. 本博文主要记录个人曾经并不知晓知识细节. 因为linux下的编译环境还未进行学习.所以实际代码测试将在今后完成. 红色:需确认. 蓝色:重点. 1)const对象设定为仅在文件内有 ...

  7. Ansible学习记录五:PlayBook学习

    0.介绍 Playbooks 是 Ansible 管理配置.部署应用和编排的语言,可以使用 Playbooks 来描述你想在远程主机执行的策略或者执行的一组步骤过程等 类似于一组任务集,定义好像项目, ...

  8. Ansible学习记录一:Linux下部署

    0.Ansible介绍 Ansible 是一个简单的自动化运维管理工具,可以用来自动化部署应用.配置.编排 task(持续交付.无宕机更新等),采用 paramiko 协议库(fabric 也使用这个 ...

  9. Oracle学习笔记四 SQL命令(二):SQL操作语言类别

    SQL分为下列语言类别 1.数据定义语言(DDL) Create.Alter.Drop 2.数据操纵语言(DML) Insert.Select.Delete.Update 3.事务控制语言(TCL) ...

随机推荐

  1. js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法

    表格有几行: var trCnt = table.rows.length;  (table为Id ) 每行有几列:for (var i=0; i<trCnt; i++)              ...

  2. Project Euler 28 Number spiral diagonals

    题意:给出一个 1001 × 1001 的矩阵,寻找每一圈四个顶点,并求出所有顶点的和 思路:只需要找到右上顶点数字的规律,然后每一圈四个顶点构成了一个等差数列,求个和即可 /************ ...

  3. [luogu2680] 运输计划 (lca+二分+树上差分)

    传送门 Description Input Output 一个整数,表示小 P 的物流公司完成阶段性工作所需要的最短时间. Sample Input 6 3 1 2 3 1 6 4 3 1 7 4 3 ...

  4. Jquery Math ceil()、floor()、round()比较与用法

    Math.ceil():向上取值 如:Math.ceil(2.1) --  结果为  3 Math.ceil(-2.1)  -- 结果为-2 结论:正入 负舍 Math.floor(): 先下取值 入 ...

  5. CodeForcesGym 100676G Training Camp

    G. Training Camp Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  6. HDU 2817 EASY题

    #include <iostream> #include <cstdio> using namespace std; const __int64 MOD=200907; __i ...

  7. UIButton上字体的对齐方式

    设置UIButton上字体的对齐方式,不是用: [Button.titleLabel setTextAlignment:UITextAlignmentCenter]; 而是用: [Button set ...

  8. WPF 基础到企业应用系列2——WPF前世今生

    1.开篇前言       非常多时候了解一项新技术的历史和趋势往往比这项技术的本身价值还要重要.WPF作为一项新技术(已经三年多了.或者应该叫老技术了).我们都有必要了解它的来龙去脉,尤其是公司的CT ...

  9. PHP别名引用错误:“The use statement with non-compound name … has no effect”

    别名概述 PHP5.3+支持命名空间:namespace,命名空间的一个重要功能是能够使用别名(alias)来引用一个符合规则的名字. 命名空间支持3中形式的别名引用(或称之为引入)方式:类(clas ...

  10. Javascript中的with用法

    1.看例子 <script language="javascript"> function Lakers() { this.name = "kobe brya ...