ansible相关

command模块: 支持基本的shell命令,但不支持管道操作.

ansible -i /etc/ansible/hosts -m command -a ‘ls /root’

所有服务器都下载redis安装包,存放路径为/opt

ansible -i /etc/ansible/hosts all -m command -a ‘wget -c http://download.redis.io/releases/redis-5.0.7.tar.gz -P /opt’

查看下载的redis安装包
ansible -i /etc/ansible/hosts all -m command -a ‘ls /opt’

raw和shell模块: 支持基本的shell命令,也支持管道操作.

ansible -i /etc/ansible/hosts name|group -m raw/shell -a ‘ps -ef | grep httpd’

file模块:

ansible -i /etc/ansible/hosts name|group -m file -a ‘path=/tmp/file state=touch’

ansible -i /etc/ansible/hosts name|group -m file -a ‘path=/tmp/file state=absent’

ansible -i /etc/ansible/hosts name|group -m file -a ‘path=/tmp/dic1 state=dictionary owner=root

gourp=root mode=755’

ansible -i /etc/ansible/hosts name|group -m file -a ‘path=/tmp/dic1 state=absent’

copy模块

ansible -i /etc/ansible/hosts name|group -m copy -a ‘src=/tmp/file dest=/opt/file owner=root

group=root mode=755 backup=yes|no’

若发现报错

“msg”: “Aborting, target uses selinux but python bindings (libselinux-python)

aren’t installed!”

则目标主机开启了selinux,需要安装ibselinux-python, 或者关闭selinux

Execute /usr/sbin/sestatus -v to view selinux status

yum模块

ansible -i /etc/ansible/hosts name|group -m yum -a ‘name=httpd state=present’

ansible -i /etc/ansible/hosts name|group -m yum -a ‘name=httpd state=absent’

一次性安装多个rpm包,例如:
ansible -i /etc/ansible/hosts all -m yum -a ‘name=gcc,gcc-c++ state=present’

service模块

service_module

ansible -i /etc/ansible/hosts cluster -m service -a ‘name=zabbix-agent state=restarted sleep=3’

ansible非常强大. 以上是些只是些简单的操作, 更多命令参考 ansible-doc -l

Leave a Reply