标签:
安装:
依赖的软件包:
Jinja2:Python 的一个对设计师友好的现代模板语言
PyYAML:Python 的一个 YAML 编码/反编码函数库
paramiko:纯 Python 编写的 SSHv2 协议函数库
httplib2:一个功能全面的 HTTP 客户端函数库
安装ansible
[root@server1-7373 ~]# yum list|grep -i ansible
Failed to set locale, defaulting to C
ansible.noarch 1.9.2-1.el6 epel
ansible-inventory-grapher.noarch 1.0.1-2.el6 epel
ansible-lint.noarch 2.0.1-1.el6 epel
[root@server1-7373 ~]# yum install ansible.noarch ansible-inventory-grapher.noarch ansible-lint.noarch -y
简单配置:
# vim /etc/ansible/ansible.cfg
remote_port = 22
private_key_file = /root/.ssh/id_rsa
# 定义一个group
[root@server1-7373 ~]# tail -n 3 /etc/ansible/hosts
[myservers]
192.168.4.98
192.168.2.98
Usage;
1. Execute simple command
[root@server1-7373 ~]# ansible myservers -m command -a ‘uptime‘
192.168.4.98 | success | rc=0 >>
08:23:14 up 61 days, 8:02, 0 users, load average: 0.00, 0.00, 0.00
192.168.2.98 | success | rc=0 >>
08:23:14 up 61 days, 10:01, 0 users, load average: 0.00, 0.00, 0.00
2. Cope file from local to remote server.
[root@server1-7373 ~]# ansible myservers -m copy -a "src=/root/test.sh dest=/tmp/test.sh owner=root group=root mode=0755"
192.168.2.98 | success >> {
"changed": true,
"checksum": "d51e9d0762e103dced4b6bc84e6af6d48ab44762",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "69eaa9aae1337871c084de2e62b45109",
"mode": "0755",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1437380034.46-279493020985021/source",
"state": "file",
"uid": 0
}
192.168.4.98 | success >> {
"changed": true,
"checksum": "d51e9d0762e103dced4b6bc84e6af6d48ab44762",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "69eaa9aae1337871c084de2e62b45109",
"mode": "0755",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1437380034.47-136677121174897/source",
"state": "file",
"uid": 0
}
3. Execute script on remote server.
[root@server1-7373 ~]# ansible myservers -m shell -a "sh /tmp/test.sh"
192.168.4.98 | success | rc=0 >>
08:14:28 up 61 days, 7:53, 0 users, load average: 0.00, 0.00, 0.00
192.168.2.98 | success | rc=0 >>
08:14:27 up 61 days, 9:52, 0 users, load average: 0.00, 0.00, 0.00
4. Install software with yum
[root@server1-7373 ~]# ansible myservers -m yum -a ‘name=httpd state=latest‘
192.168.4.98 | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date"
]
}
192.168.2.98 | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date"
]
}
5. Manage service
[root@server1-7373 ~]# ansible myservers -m service -a ‘name=httpd state=started‘
192.168.4.98 | success >> {
"changed": false,
"name": "httpd",
"state": "started"
}
192.168.2.98 | success >> {
"changed": false,
"name": "httpd",
"state": "started"
}
CentOS/REHL 6 安装配置Ansible
标签:
原文地址:http://www.cnblogs.com/torvalds0310/p/4711086.html