ansible的安装
- 安装EPEL作为安装ansible的yum源
rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm
- 安装Ansible
yum install ansible -y
- 配置文件:
路径:/etc/ansible/hosts
配置说明:问别人v而是为组名,下面的ip或域名则是属于该组的主机
[webservers]
192.168.6.160
- 测试
[root@localhost python]# ansible webservers -m ping -k
SSH password:
192.168.6.160 | SUCCESS => {
"changed": false,
"ping": "pong"
}
常用模块
- 命令行调用模块格式:
ansible 操作目标 -m 模块名(默认模块为command) -a 模块参数
例:
ansible weservers -m ping
帮助命令:
ansible-doc 模块名
- 远程命令模块
command: 执行远程shell命令
script:在被控端执行主控端上存放的脚本,相当于scp+shell
shell:执行存放在被控端上的脚本
ansible webservers -m command -a 'df -h' #在被控端执行df -h命令
ansible webservers -m script -a '/root/test.sh' #在被控端执行test.sh脚本(test.sh脚本在主控端)
ansible webservers -m shell -a '/root/test.sh' #在被控端执行test.sh脚本(test.sh脚本在被控端)
- copy模块
copy:从主控端向被控端拷贝文件,类似于scp功能
例:
ansible webservers -m copy -a 'src=/root/test.py dest=/tmp owner=root group=root mode=0755'#将主控端的test.py文件拷贝到被控端的/tmp目录下,并且指定文件的属主和权限
- stat模块
stat:获取远程文件状态信息,包括atime,ctime,mtime,md5,uid,gid等
例:
ansible webservers -m stat -a 'path=/etc/sysctl.conf' #获取被控端/etc/sysctl.conf文件状态信息
- get_url模块
get_url模块:实现被控端下载指定url,
例:
`ansible 192.168.1.111 -m get_url -a ‘url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes’
被控端下载百度首页到/tmp/index.html中`
- 软件操作模块
yum/apt:linux平台软件包管理操作
例:
ansible 192.168.6.160 -m yum -a 'name=curl state=latest' #被控端使用yum安装最新的curl
ansible 192.168.6.160 -m apt -a 'pkg=curl state=latest' #被控端使用apt安装最新的curl
- cron模块
cron:被控端cron配置
例:
ansible 192.168.6.160 -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"
- service模块
service:被控端系统服务管理
例:
ansible webservers -m service -a 'name=httpd state=stopped' #关闭httpd服务
- user模块
user:被控端系统用户管理
ansible 192.168.6.160 -m user -a "name=johnd comment=Hohn Doe" #添加用户john