Ansible更改配置文件


*vim /etc/ansible/hosts  //增加

*[testhost]

 127.0.0.1

 192.168.11.30或者client.wyp.com

*说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。



Ansible 远程执行命令


*ansible testhost -m command -a 'w' 

这样就可以批量执行命令了。这里的testhost 为主机组名,-m后边是模块名字,-a后面是命令。当*然我们也可以直接写一个ip,针对某一台机器来执行命令。

ansible 127.0.0.1 -m  command -a 'hostname'

错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

解决: yum install -y libselinux-python

*还有一个模块就是shell同样也可以实现

ansible testhost -m shell -a 'cat /etc/passwd|grep root'

说明:command模块不支持shell的一些功能,所以需要用到shell模块。command能实现的功能,shell一定能实现。shell能实现的功能,command不一定能实现。