Ansible实现LAMP的分离部署
1.配置主控机和受控主机
环境说明:
主机名 | 作用 | IP |
---|---|---|
ansible | Ansible控制主机 | 192.168.237.167 |
apache | 受控主机1(apache) | 192.168.237.168 |
mysql | 受控主机2(MySQL) | 192.168.237.169 |
php | 受控主机3(PHP) | 192.168.237.170 |
在主控机上将node1、node2、node3加入清单文件:
[root@master ansible]# cat inventory
[apache]
192.168.237.168
[mysql]
192.168.237.169
[php]
192.168.237.170
配置免密登录:
[root@localhost ansible]# ssh-keygen
[root@localhost ansible]# ssh-copy-id root@192.168.237.168
[root@localhost ansible]# ssh-copy-id root@192.168.237.169
[root@localhost ansible]# ssh-copy-id root@192.168.237.170
测试,用ping模块看主控机与受控主机能否ping通:
[root@master ansible]# ansible all -m ping
192.168.237.168 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.237.169 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.237.170 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
为所有受控主机关闭防火墙和selinux
[root@localhost ansible]# ansible all -m service -a 'name=firewalld state=stopped enabled=no'
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="SELINUX=enforcing" line="SELINUX=disabled"'
[root@localhost ansible]# ansible all -m shell -a 'setenforce 0'
2.安装httpd服务
在主控机上通过ansible在apache上安装apache服务:
[root@ansible ansible]# ansible apac