环境准备
服务器系统 | Centos7.3 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.41 |
服务器系统 | Centos7.3 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.43 |
服务器系统 | Centos7.3 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.44 |
在41Ansible管理主机上操作
vim when.yml
ansible-playbook when.yml
ansible all -m shell -a 'ls /opt/'
在43主机上操作,修改主机名
hostname node0-43
exit
ssh root@10.0.0.43
在44主机上操作,修改主机名
hostname node0-44
exit
ssh root@10.0.0.44
在41Ansible管理主机上操作
vim nginx.yml
---
- hosts: webservers
tasks:
- name: "推送nginx源码包"
unarchive: src=nginx-1.12.0.tar.gz dest=/usr/local/src/
- name: "安装编译环境"
yum: name=gcc,gcc-c++,pcre-devel,zlib-devel state=latest
- name: "安装nginx"
shell: cd /usr/local/src/nginx-1.12.0/ && ./configure && make && make install
- name: "推送配置文件"
copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
notify: reload-nginx
tags: config-nginx
- name: "启动nginx"
shell: ss -ntl | grep -qw 80 || /usr/local/nginx/sbin/nginx
tags: start-nginx
- name: "修改测试页面"
shell: echo "<h1>hello world!<h1>" > /usr/local/nginx/html/index.html
- name: "关闭防火墙"
service: name=firewalld state=stopped
- name: "关闭selinux"
shell: setenforce 0
handlers:
- name: "reload-nginx"
shell: ss -ntlp | grep -q nginx && /usr/local/nginx/sbin/nginx -s reload
ansible-playbook -t config-nginx nginx.yml