2:ansible基础使用

一、ansible基础使用

1、执行命令

ansible tomcat -m shell -a "hostname" -o   # tomcat是主机组,-m指定执行shell模块,-a指定要执行的命令,-0一行显示
192.168.1.211 | SUCCESS | rc=0 | (stdout) web4-1.211
192.168.1.221 | SUCCESS | rc=0 | (stdout) web10-1.221
192.168.1.220 | SUCCESS | rc=0 | (stdout) web9-1.220

 

2、复制文件

-m指定copy模块,-a指定要执行的文件源目和权限,-a一行显示

ansible tomcat -m copy -a 'src=/etc/hosts dest=/tmp/hosts owner=root group=root mode=644' -o
192.168.1.219 | SUCCESS => {"changed": true, "checksum": "715ba330f4ab24ece9525846b47bdf211ef3a54d", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "3f99d46febd481dff515a7262b6c40a3", "mode": "0644", "owner": "root", "size": 172, "src": "/root/.ansible/tmp/ansible-tmp-1524194514.66-170889087587190/source", "state": "file", "uid": 0}
192.168.1.211 | SUCCESS => {"changed": true, "checksum": "715ba330f4ab24ece9525846b47bdf211ef3a54d", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "3f99d46febd481dff515a7262b6c40a3", "mode": "0644", "owner": "root", "size": 172, "src": "/root/.ansible/tmp/ansible-tmp-1524194514.57-228250949741398/source", "state": "file", "uid": 0}
192.168.1.210 | SUCCESS => {"changed": true, "checksum": "715ba330f4ab24ece9525846b47bdf211ef3a54d", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "3f99d46febd481dff515a7262b6c40a3", "mode": "0644", "owner": "root", "size": 172, "src": "/root/.ansible/tmp/ansible-tmp-1524194514.51-275749012714953/source", "state": "file", "uid": 0}

 

3、包和服务管理

 

ansible tomcat -m yum -a 'name=ntpdate state=latest' -o
192.168.1.211 | SUCCESS => {"changed": false, "msg": "", "rc": 0, "results": ["All packages providing ntpdate are up to date", ""]}
192.168.1.212 | SUCCESS => {"changed": false, "msg": "", "rc": 0, "results": ["All packages providing ntpdate are up to date", ""]}
192.168.1.214 | SUCCESS => {"changed": false, "msg": "", "rc": 0, "results": ["All packages providing ntpdate are up to date", ""]}

 

4、用户管理

首先通过openssl命令生成一个密码,因为ansible user的password参数需要接受加密后的值

echo log | openssl passwd -1 -stdin     
$1$CXn0YY7e$oTsoVWwIkPjR6Ac6MFSBj.

ansible tomcat -m user -a 'name=log password="$1$CXn0YY7e$oTsoVWwIkPjR6Ac6MFSBj."' -f 5 -o
posted @ 2018-04-19 22:40  Wesley·zk  阅读(159)  评论(0)    收藏  举报