本篇博客僅供學習,沒有實際項目意義,僅供學習之用html
實驗環境: VMware® Workstation 12 Pro 系統平臺: CentOS Linux release 7.4.1708 內核 3.10.0-693.el7.x86_64
Ansible中文權威指南:http://www.ansible.com.cn/nginx
配置密鑰鏈接:web
進入用戶的ssh密鑰路徑 cd /root/.ssh/ 生成公私鑰 ssh-keygen -t rsa -b 4096 將公鑰發送給遠程目標主機 ssh-copy-id -i id_rsa.pub root@10.10.11.2 測試鏈接 ssh root@10.10.11.2
定義角色的路徑:默認位置
[root@localhost ~]# less /etc/ansible/ansible.cfgvim
# additional paths to search for roles in, colon separated #roles_path = /etc/ansible/roles
建立相應的目錄:tomcat
~]# cd /etc/ansible/roles/ [root@localhost roles]# mkdir -pv ./nginx/{files,templates,tasks,handlers,vars,meta,dafault}
目錄結構服務器
[root@localhost roles]# tree ├── memcached ├── dafault ├── files ├── handlers ├── meta ├── tasks ├── templates └── vars
配置nginx思路:
安裝程序包,複製配置文件,啓動服務。less
編輯/nginx/tasks/main.yml文件(設置任務列表)dom
[root@localhost nginx]# vim tasks/main.yml - name: install epel-release package yum: name=epel-release state=installed - name: install nginx package yum: name=nginx state=installed #準備目錄路徑 - name: create doc root file: path={{ docroot }} state=directory - name: install home page copy: src=index.html dest={{ docroot }}/ #自動會去templates目錄下找j2文件 #複製帶變量的文件須要使用templates來執行 - name: install conf file template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf notify: reload nginx - name: start nginx service service: name=nginx enabled=true state=started #使用templates複製文件時的路勁設置方式: 相對於etc/ansible/roles/nginx的相對路徑 template: copy: src=templates/server.conf.j2 dest=/etc/nginx/conf.d/server.conf 徹底絕對路徑 template: copy: src=/etc/ansible/roles/nginx/templates/server.conf.j2 dest=/etc/nginx/conf.d/server.conf 須要自動去templates目錄下自動識別 template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
nginx的模版配置文件(.j2文件)ssh
[root@localhost nginx]# vim templates/server.conf.j2 server { listen 80; server_name {{ ansible_fqdn }} {{ ansible_hostname }}; location / { root {{ docroot }}; index index.jsp index.html; } }
觸發器文件配置jsp
[root@localhost nginx]# vim handlers/main.yml - name: reload nginx server: name=nginx state=reloaded
這個nginx wed服務,須要測試頁,編輯網站的主頁:
[root@localhost nginx]# vim files/index.html <h1>Welcome to nginx </h1>
定義變量:
[root@localhost nginx]# vim vars/main.yml #定義網站主頁的路徑 docroot: /data/nginx/
yaml文件play_books文件
[root@localhost ~]# cd ansible/ [root@localhost ansible]# vim nginx.yml - hosts: all remote_user: root roles: - nginx
進行語法檢測:
[root@localhost ansible]# ansible-playbook --syntax-check nginx.yml
進行測試執行:
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [10.10.11.2] ************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************** ok: [10.10.11.2] TASK [nginx : install epel-release package] *********************************************************************** ok: [10.10.11.2] TASK [nginx : install nginx package] ****************************************************************************** ok: [10.10.11.2] TASK [nginx : create doc root] ************************************************************************************ ok: [10.10.11.2] TASK [nginx : install home page] ********************************************************************************** ok: [10.10.11.2] TASK [nginx : install conf file] ********************************************************************************** ok: [10.10.11.2] TASK [nginx : start nginx service] ******************************************************************************** ok: [10.10.11.2] PLAY RECAP *********************************************************************************** 10.10.11.2 : ok=7 changed=0 unreachable=0 failed=0
確認執行文件:
[root@localhost ansible]# ansible-playbook nginx.yml PLAY [10.10.11.2] ******************************************************************************* TASK [Gathering Facts] ************************************************************************** ok: [10.10.11.2] TASK [nginx : install epel-release package] ***************************************************** ok: [10.10.11.2] TASK [nginx : install nginx package] ************************************************************ ok: [10.10.11.2] TASK [nginx : create doc root] ****************************************************************** ok: [10.10.11.2] TASK [nginx : install home page] **************************************************************** ok: [10.10.11.2] TASK [nginx : install conf file] **************************************************************** ok: [10.10.11.2] TASK [nginx : start nginx service] ************************************************************** ok: [10.10.11.2] PLAY RECAP ************************************************************************************** 10.10.11.2 : ok=7 changed=0 unreachable=0 failed=0
去執行命令的節點查看
查看端口
[root@localhost ~]# ss -ntlp |grep 80 LISTEN 0 128 *:80 *:* users:(("nginx",pid=11368,fd=6),("nginx",pid=11314,fd=6))
查看配置文件
[root@localhost ~]# cat /etc/nginx/conf.d/server.conf server { listen 80; server_name localhost.localdomain localhost; location / { root /data/nginx/; index index.jsp index.html; } }
修改前先進行備份
[root@localhost roles]# cp -a nginx/ /root/
修改自定義變量
[root@localhost nginx]# vim vars/main.yml #添加servertype變量值爲web servertype: web docroot: /data/nginx/
修改:/nginx/tasks/main.yml文件(設置任務列表)
[root@localhost nginx]# vim tasks/main.yml - name: install epel-release package yum: name=epel-release state=installed - name: install nginx package yum: name=nginx state=installed - name: create doc root file: path={{ docroot }} state=directory #新添加類型爲web when: servertype == 'web' - name: install home page copy: src=index.html dest={{ docroot }}/ #新添加類型爲web when: servertype == 'web' - name: install conf file template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf #新添加類型爲web when: servertype == 'web' notify: reload nginx - name: start nginx service service: name=nginx enabled=true state=started
測試執行nging=web (沒有問題)
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [10.10.11.2] ******************************************************************************* TASK [Gathering Facts] ************************************************************************** ok: [10.10.11.2] TASK [nginx : install epel-release package] ***************************************************** ok: [10.10.11.2] TASK [nginx : install nginx package] ************************************************************ ok: [10.10.11.2] TASK [nginx : create doc root] ****************************************************************** ok: [10.10.11.2] TASK [nginx : install home page] **************************************************************** ok: [10.10.11.2] TASK [nginx : install conf file] **************************************************************** ok: [10.10.11.2] TASK [nginx : start nginx service] ************************************************************** changed: [10.10.11.2] PLAY RECAP ************************************************************************************** 10.10.11.2 : ok=7 changed=1 unreachable=0 failed=0
設置變量
[root@localhost nginx]# vim vars/main.yml servertype: web #設置變量,代理服務器的代理目標主機變量 backendurl: 'http://127.0.0.1:8080' docroot: /data/nginx/
建立proxy的模版文件
[root@localhost nginx]# vim templates/proxy.conf.j2 server { listen 80; server_name {{ ansible_fqdn }} {{ ansible_hostname }}; location / { proxy_pass {{ backendurl }}; } }
yaml文件,play_books文件,
修改:/nginx/tasks/main.yml文件(設置任務列表)
[root@localhost nginx]# vim tasks/main.yml - name: install epel-release package yum: name=epel-release state=installed - name: install nginx package yum: name=nginx state=installed - name: create doc root file: path={{ docroot }} state=directory when: servertype == 'web' - name: install home page copy: src=index.html dest={{ docroot }}/ when: servertype == 'web' - name: install web conf file template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf when: servertype == 'web' notify: reload nginx #新加proxy部分配置段 - name: install proxy conf file template: src=proxy.conf.j2 dest=/etc/nginx/conf.d/server.conf when: servertype == 'proxy' notify: reload nginx - name: start nginx service service: name=nginx enabled=true state=started /nginx/tasks/main.yml文件(設置任務列表)測試
手動分別測試
默認執行web
使用:-e servertype=proxy 手動觸發proxy
配置/etc/ansible/hosts
[root@localhost nginx]# vim /etc/ansible/hosts [tomcat] 10.10.11.133 [web] 10.10.11.2
yaml文件play_books文件
[root@localhost ansible]# cat nginx.yml - hosts: tomcat remote_user: root roles: - { role: nginx, servertype: proxy } - hosts: web remote_user: root roles: - nginx
測試:
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [tomcat] *********************************************************************************** TASK [Gathering Facts] ************************************************************************** ok: [10.10.11.133] TASK [nginx : install epel-release package] ***************************************************** changed: [10.10.11.133] TASK [nginx : install nginx package] ************************************************************ changed: [10.10.11.133] TASK [nginx : create doc root] ****************************************************************** skipping: [10.10.11.133] TASK [nginx : install home page] **************************************************************** skipping: [10.10.11.133] TASK [nginx : install web conf file] ************************************************************ skipping: [10.10.11.133] TASK [nginx : install proxy conf file] ********************************************************** changed: [10.10.11.133] TASK [nginx : start nginx service] ************************************************************** changed: [10.10.11.133] RUNNING HANDLER [nginx : reload nginx] ********************************************************** changed: [10.10.11.133] PLAY [web] ************************************************************************************** TASK [Gathering Facts] ************************************************************************** ok: [10.10.11.2] TASK [nginx : install epel-release package] ***************************************************** ok: [10.10.11.2] TASK [nginx : install nginx package] ************************************************************ ok: [10.10.11.2] TASK [nginx : create doc root] ****************************************************************** ok: [10.10.11.2] TASK [nginx : install home page] **************************************************************** ok: [10.10.11.2] TASK [nginx : install web conf file] ************************************************************ ok: [10.10.11.2] TASK [nginx : install proxy conf file] ********************************************************** skipping: [10.10.11.2] TASK [nginx : start nginx service] ************************************************************** changed: [10.10.11.2] PLAY RECAP ************************************************************************************** 10.10.11.133 : ok=6 changed=5 unreachable=0 failed=0 10.10.11.2 : ok=7 changed=1 unreachable=0 failed=0
真正執行結果
最後的目錄結構以下:
[root@localhost nginx]# tree
.
[root@localhost nginx]# tree
. ├── dafault ├── files │ └── index.html ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ ├── proxy.conf.j2 │ └── server.conf.j2 └── vars └── main.yml