Ansible自动化安装nginx

Ansible自动化安装nginx

1.安装ansible安装包
[root@m01 ~]# yum -y install ansible 
[root@m01 ~]#rpm  -qa | grep ansible
ansible-2.9.27-1.el7.noarch

2.配置免秘钥
[root@m01 ~]#ssh-keygen 
[root@m01 ~]#ssh-copy-id -i .ssh/id_rsa.pub 172.16.1.7
[root@m01 ~]#ssh-copy-id -i .ssh/id_rsa.pub 172.16.1.8

3.配置ansible的webs组
[root@m01 ~]#cat /etc/ansible/hosts 
[webs]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8

4.测试ping是否成功
[root@m01 ~]#ansible webs -m ping 
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 ~]#mkdir ansible
[root@m01 ansible]#cat nginx.repo 
[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]#cat nginx.conf 
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]#vim nginx.yml 
- hosts: webs
  tasks:
    - name: Nginx Yum Update 		#创建nginx的yum
      copy:
        src: nginx.repo
        dest: /etc/yum.repos.d/
    - name: Install Nginx Server	#安装nginx服务
      yum:
        name: nginx
        state: present
    - name: Configure Nginx Server	#配置nginx服务
      copy:
        src: nginx.conf  
        dest: /etc/nginx/nginx.conf
    - name: Create www Group		#创建www组
      group:
        gid: 666
        name: www
    - name: Create www User			#创建www用户
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false
    - name: Start Nginx Server		#启动nginx服务并开机自启
      systemd:
        name: nginx
        state: started
        enabled: yes

8.检查ansible语法
[root@m01 ansible]#ansible-playbook --syntax-check nginx.yml 

playbook: nginx.yml

9.启动ansible配置
[root@m01 ansible]#ansible-playbook nginx.yml 
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 ~]#cat /etc/yum.repos.d/nginx.repo 
[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 ~]#rpm -qa nginx
nginx-1.26.0-1.el7.ngx.x86_64
[root@web01 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]#nginx -v
nginx version: nginx/1.26.0
[root@web01 ~]#id www
uid=666(www) gid=666(www) groups=666(www)
[root@web01 ~]#netstat -utnlp 
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 ~]#cat /etc/yum.repos.d/nginx.repo 
[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 ~]#rpm -qa nginx
nginx-1.26.0-1.el7.ngx.x86_64
[root@web02 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]#id www
uid=666(www) gid=666(www) groups=666(www)
[root@web02 ~]#nginx -v
nginx version: nginx/1.26.0
[root@web02 ~]#netstat -utnlp
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           


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值