1、主機準備nginx
ServerIP:10.10.10.102c++
ClientIP: 10.10.10.103,10.10.10.104shell
2、安裝ansiblebash
yum -y install ansible
3、配置免密登陸dom
1.在Server上製做公鑰和私鑰ssh
[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上ide
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
4、編寫ansible相關配置文件spa
1.ansible的目錄結構以下code
├── 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.ymlblog
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
5、使用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
6、驗證
在安裝完成後,能夠到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]#