Skip to content

Install and configure Ansible on CentOS 6.7

1、update yum repo

yum update && yum upgrade

2、environment requiuement

Master:Python 2.6+
Slave: Python 2.4+,最好也是2.6+

3、config ssh

配置Master向每一台Slave的SSH无密码登录(在Master上使用ssh-keygen生成密钥对,并将id_rsa.pub加入到每一个Slave的authorized_keys中)

4、使用yum安装ansible

yum install ansible

如果提示没有此安装包,可以加入EPEL扩展包:

yum install epel-release
yum install ansible

5、config host/inventory file

默认在/etc/ansible/hosts,加入Slave的ip地址,例如

[Web]
192.168.1.101
192.168.1.102
192.168.1.103

6、测试

ansible all -m ping

成功ping同后显示

192.168.1.101 | success >> {
    "changed": false,
    "ping": "pong"
}

192.168.1.102 | success >> {
    "changed": false,
    "ping": "pong"
}

192.168.1.103 | success >> {
    "changed": false,
    "ping": "pong"
}

基本查看命令:

ansible all -a 'uptime'
ansible all -a 'free -m'

7、使用模块(-m)

command 模块不支持 shell 变量,也不支持管道等,如果需要使用这些功能,可以借助模块

ansible web -m shell -a 'echo $TERM'

拷贝文件模块(-m copy)

ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"

文件属性模块

ansible web -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

服务模块

ansible web -m service -a "name=httpd state=started"

具体用法可以参考

  • http://docs.ansible.com/ansible
  • http://www.ansible.com.cn/docs

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App