【ansible】使用ansible安装nginx

时间:2021-12-01 04:35:38

一、主机准备

ServerIP:10.10.10.102

ClientIP:  10.10.10.103,10.10.10.104

 

二、安装ansible

yum -y install ansible

 

三、配置免密登录

1.在Server上制作公钥和私钥

[root@localhost roles]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fsarmF7FOVq6/s2ka/VjhEIjbnEcPoQvobEqL1dV63g root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|         .       |
|      . o +      |
|       + B o     |
|      o =.X.     |
|     . oSO*o .   |
|  . . ..+*E.o .  |
|   o . .=.+o.o   |
|  . o  + +.*  +  |
|   o .+.++=.o. . |
+----[SHA256]-----+
[root@localhost roles]# 

 

2.将Server上的公钥复制到Client上

ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@10.10.10.103
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@10.10.10.104
#写入成功后会在10.10.10.103目录~/.ssh/下生成authorized_keys

#验证免密登录是否成功,回车后能登录成功,说明设置成功
ssh root@10.10.10.103

 

四、编写ansible相关配置文件

1.ansible的目录结构如下

├── ansible.cfg
├── hosts
├── install_nginx.yml
└── roles
     └── install_nginx
          ├── files
          │   ├── install_nginx.sh
          │   └── nginx-1.14.0.tar.gz
          ├── tasks
          │   └── main.yml
          ├── templates
          └── vars
              └── main.yml

 

 2.相关配置文件内容如下:

 

/etc/ansible/hosts文件:

[Client]
10.10.10.103
10.10.10.104

 

/etc/ansible/install_nginx.yml

[root@CentOS7 ansible]# more install_nginx.yml
---
- hosts: Client
  roles:
  - install_nginx

 

/etc/ansible/roles/install_nginx/files/install_nginx.sh

[root@CentOS7 files]# more install_nginx.sh
#!/bin/bash

cd /usr/local/nginx-1.14.0
./configure --prefix=/usr/local/nginx
make && make install

/usr/local/nginx/sbin/nginx

 

/etc/ansible/roles/install_nginx/tasks/main.yml

[root@CentOS7 tasks]# more main.yml 
---
  - name: Install Software
    yum: name={{ item }} state=latest
    with_items:
      -  gcc 
      -  gcc-c++ 
      -  zlib-devel 
      -  pcre-devel 
      -  openssl 
      -  openssl-devel
  - name: Copy nginx.tar.gz
    copy: src=nginx-{{ nginx_version }}.tar.gz dest={{ nginx_dir }}/nginx-{{ nginx_version }}.tar.gz owner=root group=root
  - name: Copy install_nginx.sh
    copy: src=install_nginx.sh dest=/tmp/install_nginx.sh
  - name: uncompress nginx.tar.gz
    shell: tar -xf {{ nginx_dir }}/nginx-{{ nginx_version }}.tar.gz -C {{ nginx_dir }}/
  - name: install nginx
    shell: /bin/bash /tmp/install_nginx.sh

 

/etc/ansible/roles/install_nginx/vars/main.yml

[root@CentOS7 vars]# more main.yml 
nginx_dir: /usr/local
nginx_version: 1.14.0

 

五、使用ansible安装nginx并启动

[root@CentOS7 ansible]# cd /etc/ansible
[root@CentOS7 ansible]# ansible-playbook install_nginx.yml

PLAY [Client] ******************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************
ok: [10.10.10.103]
ok: [10.10.10.104]

TASK [install_nginx : Install Software] ****************************************************************************************************************************
ok: [10.10.10.103] => (item=[u'gcc', u'gcc-c++', u'zlib-devel', u'pcre-devel', u'openssl', u'openssl-devel'])
ok: [10.10.10.104] => (item=[u'gcc', u'gcc-c++', u'zlib-devel', u'pcre-devel', u'openssl', u'openssl-devel'])

TASK [install_nginx : Copy nginx.tar.gz] ***************************************************************************************************************************
changed: [10.10.10.103]
changed: [10.10.10.104]

TASK [install_nginx : Copy install_nginx.sh] ***********************************************************************************************************************
changed: [10.10.10.103]
changed: [10.10.10.104]

TASK [install_nginx : uncompress nginx.tar.gz] *********************************************************************************************************************
 [WARNING]: Consider using unarchive module rather than running tar

changed: [10.10.10.103]
changed: [10.10.10.104]

TASK [install_nginx : install nginx] *******************************************************************************************************************************
changed: [10.10.10.103]
changed: [10.10.10.104]

PLAY RECAP *********************************************************************************************************************************************************
10.10.10.103               : ok=6    changed=4    unreachable=0    failed=0   
10.10.10.104               : ok=6    changed=4    unreachable=0    failed=0  

 

六、验证

在安装完成后,可以到Client验证nginx是否安装完成!

如下是在10.10.10.104上的验证结果:

[root@CentOS7 local]# ps -ef|grep nginx
root     35688     1  0 04:24 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   35689 35688  0 04:24 ?        00:00:00 nginx: worker process
root     43485 19737  0 18:34 pts/2    00:00:00 grep --color=auto nginx
[root@CentOS7 local]#