Ansible自动化安装nginx
1.安装ansible安装包
[root@m01 ~]
[root@m01 ~]
ansible-2.9.27-1.el7.noarch
2.配置免秘钥
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
3.配置ansible的webs组
[root@m01 ~]
[webs]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8
4.测试ping是否成功
[root@m01 ~]
web02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
web01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
5.创建m01服务器nginx的yum文件,需要在ansible目录中,必须与nginx.yml在同一个目录中
[root@m01 ~]
[root@m01 ansible]
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
6.需要把nginx的配置文件存放在ansible目录中(此配置是/etc/nginx里面的文件,勿错!)
[root@m01 ansible]
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
7.配置ansible服务器自动化配置
[root@m01 ansible]
- hosts: webs
tasks:
- name: Nginx Yum Update
copy:
src: nginx.repo
dest: /etc/yum.repos.d/
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
- name: Create www Group
group:
gid: 666
name: www
- name: Create www User
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
8.检查ansible语法
[root@m01 ansible]
playbook: nginx.yml
9.启动ansible配置
[root@m01 ansible]
PLAY [webs] ***********************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************
ok: [web01]
ok: [web02]
TASK [Nginx Yum Update] ***********************************************************************************************************
ok: [web01]
ok: [web02]
TASK [Install Nginx Server] *******************************************************************************************************
changed: [web02]
changed: [web01]
TASK [Configure Nginx Server] *****************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Create www Group] ***********************************************************************************************************
ok: [web01]
ok: [web02]
TASK [Create www User] ************************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Start Nginx Server] *********************************************************************************************************
changed: [web02]
changed: [web01]
PLAY RECAP ************************************************************************************************************************
web01 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.检查客户端是否正确
web01
[root@web01 ~]
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@web01 ~]
nginx-1.26.0-1.el7.ngx.x86_64
[root@web01 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]
nginx version: nginx/1.26.0
[root@web01 ~]
uid=666(www) gid=666(www) groups=666(www)
[root@web01 ~]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11605/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1273/sshd
web02:
[root@web02 ~]
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@web02 ~]
nginx-1.26.0-1.el7.ngx.x86_64
[root@web02 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]
uid=666(www) gid=666(www) groups=666(www)
[root@web02 ~]
nginx version: nginx/1.26.0
[root@web02 ~]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11406/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1300/sshd