3.2、Ansible单命令测试

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>

 

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/7200471.html