【Ansible】Ansible 自動(dòng)編譯部署 nginx詳解

一、ansible簡(jiǎn)介及測(cè)試環(huán)境

本次測(cè)試只是用一臺(tái)client做測(cè)試。但?。。?!

ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開發(fā),集合了眾多運(yùn)維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。

ansible是基于模塊工作的,本身沒(méi)有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。主要包括:

(1)、連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實(shí)現(xiàn)通信;

(2)、host inventory:指定操作的主機(jī),是一個(gè)配置文件里面定義監(jiān)控的主機(jī);

(3)、各種模塊核心模塊、command模塊、自定義模塊;

(4)、借助于插件完成記錄日志郵件等功能;

(5)、playbook:劇本執(zhí)行多個(gè)任務(wù)時(shí),非必需可以讓節(jié)點(diǎn)一次性運(yùn)行多個(gè)任務(wù)。

所以在批量部署時(shí),只需要在hosts文件加入client主機(jī)IP即可。

二、安裝ansible

因?yàn)槲业腖inux的是CentOS Linux release 7.3.1611 (Core),使用的ali倉(cāng)庫(kù)源。

我在yum安裝的ansible是

三、編寫nginx的roles模板

先看看所需要的文件

[root@node4 ansible]# pwd

/etc/ansible

[root@node4 ansible]# tree

.

├── ansible.cfg #核心配置文件

├── hosts? ?? ? #主機(jī)清單

├── nginx_install.yaml #playbook編譯安裝nginx文件

└── roles

└── nginx_install

├── default

├── files

│? ?└── nginx-1.12.0.tar.gz #編譯安裝nginx的安裝包,提前下載至此

├── handlers

│? ?└── main.yml #觸發(fā)配置文件

├── meta #元數(shù)據(jù)

├── tasks

│? ?└── main.yml #定義要調(diào)用的nginx的主邏輯文件

├── templates

│? ?├── nginx.conf #定義nginx的配置文件

│? ?└── nginx.service #定義nginx的啟動(dòng)腳本(systemctl)

└── vars

└── main.yml #定義變量配置文件

9 directories, 9 files

先創(chuàng)建所需要的目錄(必須一樣),而且上面配置文件必須以main.yml命名。

必須設(shè)置ansible到各主機(jī)之間的ssh免秘鑰互信。

命令如下

ssh-keygen?

ssh-copy-id node5 #主機(jī)名或IP

mkdir -p roles/nginx_instll/{handlers,files,meta,tasks,templates,vars,default}

1、配置主要邏輯文件:

[root@node4 ansible]# cat roles/nginx_install/tasks/main.yml?

- name: copy nginx package to remote host

copy: src=nginx-1.12.0.tar.gz dest=/usr/local/src/nginx-1.12.0.tar.gz#調(diào)用files模塊(復(fù)制nginx-1.12.0.tar.gz至遠(yuǎn)端)

tags: cppkg

- name: tar nginx

shell: cd /usr/local/src;tar -xf nginx-1.12.0.tar.gz

- name: install pakger

yum: name={{item}} state=latest#使用變量解決依賴包的問(wèn)題

with_items:?

- openssl-devel

- pcre-devel

- gcc

tags: dpdpkg

- name: useradd nginx

shell: useradd nginx

tags: add_nginx

- name: install nginx

shell: cd /usr/local/src/nginx-1.12.0;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre;make && make install

tags: inngx

- name: copy start config

template: src=nginx.service dest=//usr/lib/systemd/system/nginx.service#調(diào)用templates模塊

notify: start nginx

- name: mkdir vhosts

shell: mkdir /usr/local/nginx/vhosts

- name: copy config file nginx.conf

template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf

notify: reload nginx service

2、將nginx-1.12.0.tar.gz復(fù)制到/etc/ansible/roles/nginx_install/files目錄下

3、配置templates下的文件。

[root@node4 nginx_install]# ls templates/

nginx.conf??nginx.service

該目錄下的nginx.conf將推送到client主機(jī)。

?

[root@node4 templates]# cat nginx.conf?

#user??nobody;

worker_processes??{{ ansible_processor_vcpus }};#使用ansible的setup指令自動(dòng)傳入

#error_log??logs/error.log;

#error_log??logs/error.log??notice;

#error_log??logs/error.log??info;

#pid? ?? ???logs/nginx.pid;

events {

worker_connections??1024;

}

http {

include? ?? ? mime.types;

default_type??application/octet-stream;

#log_format??main??'$remote_addr - $remote_user [$time_local] "$request" '

#? ?? ?? ?? ?? ?? ?'$status $body_bytes_sent "$http_referer" '

#? ?? ?? ?? ?? ?? ?'"$http_user_agent" "$http_x_forwarded_for"';

#access_log??logs/access.log??main;

sendfile? ?? ???on;

#tcp_nopush? ???on;

#keepalive_timeout??0;

keepalive_timeout??65;

#gzip??on;

server {

listen? ?? ?{{ ngxport }} ;#使用vars下自定義的變量

server_name??localhost;

#charset koi8-r;

#access_log??logs/host.access.log??main;

location / {

root? ?{{ root_dir }};#使用vars下自定義的變量

index??index.html index.htm;

}

#error_page??404? ?? ?? ?? ???/404.html;

# redirect server error pages to the static page /50x.html

#

error_page? ?500 502 503 504??/50x.html;

location = /50x.html {

root? ?html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#? ? proxy_passhttp://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

#? ? root? ?? ?? ???html;

#? ? fastcgi_pass? ?127.0.0.1:9000;

#? ? fastcgi_index??index.php;

#? ? fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;

#? ? include? ?? ???fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#? ? deny??all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#? ? listen? ?? ? 8000;

#? ? listen? ?? ? somename:8080;

#? ? server_name??somename??alias??another.alias;

#? ? location / {

#? ?? ???root? ?html;

#? ?? ???index??index.html index.htm;

#? ? }

#}

# HTTPS server

#

#server {

#? ? listen? ?? ? 443 ssl;

#? ? server_name??localhost;

#? ? ssl_certificate? ?? ?cert.pem;

#? ? ssl_certificate_key??cert.key;

#? ? ssl_session_cache? ? shared:SSL:1m;

#? ? ssl_session_timeout??5m;

#? ? ssl_ciphers??HIGH:!aNULL:!MD5;

#? ? ssl_prefer_server_ciphers??on;

#? ? location / {

#? ?? ???root? ?html;

#? ?? ???index??index.html index.htm;

#? ? }

#}

include vhosts/*.conf;#創(chuàng)建虛擬主機(jī)的包含文件,方便下次批量管理

}

該目錄下的nginx.service將推送到client主機(jī)。該啟動(dòng)腳本采用systemctl啟動(dòng)。

?

[root@node4 nginx_install]# cat templates/nginx.service?

[Unit]

Description=nginx

After=network.target

[Service]

Type=forking

ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx??-s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

4、配置vars下的文件

在nginx.conf的配置文件的變量部分可以選擇利用ansible調(diào)用系統(tǒng),例如cup核心數(shù):

?

[root@node4 nginx_install]# ansible 192.168.40.132 -m setup -a "filter=ansible_processor_vcpus"

192.168.40.132 | success >> {

"ansible_facts": {

"ansible_processor_vcpus": 1

},?

"changed": false

}

所以nginx.conf中定義的

{{ ansible_processor_vcpus }}

是可以自動(dòng)化轉(zhuǎn)入?yún)?shù)。

nginx.conf中其他剩下的變量屬于自定義,調(diào)用vars的配置文件:

[root@node4 nginx_install]# cat vars/main.yml?

ngxport: "80"#監(jiān)聽端口

server_name: "node5.aizhen.com"#訪問(wèn)域名

root_dir: "/web"#web主目錄

5、配置handlers下的文件

在主邏輯文件中:

- name: copy start config

template: src=nginx.service dest=//usr/lib/systemd/system/nginx.service

notify: start nginx #觸發(fā)到handlers的文件

handlers的配置文件

[root@node4 nginx_install]# cat handlers/main.yml?

- name: start nginx #這里的名字必須與主邏輯文件匹配,否則觸發(fā)不了

service: name=nginx state=started

- name: reload nginx service

service: name=nginx state=restarted


四、測(cè)試

1、檢測(cè)語(yǔ)法,看看是否正確。

[root@node4 ansible]# ansible-playbook --syntax-chec /etc/ansible/nginx_install.yaml?

playbook: /etc/ansible/nginx_install.yaml

確定沒(méi)有報(bào)錯(cuò)。

2、使用tags,copy nginx的安裝包到client主機(jī),防止出錯(cuò)。

[root@node4 ansible]# ansible-playbook -t cppkg /etc/ansible/nginx_install.yaml?

PLAY [192.168.40.132] *********************************************************?

GATHERING FACTS ***************************************************************?

ok: [192.168.40.132]

TASK: [nginx_install | copy nginx package to remote host] *********************?

changed: [192.168.40.132]

PLAY RECAP ********************************************************************?

192.168.40.132? ?? ?? ?? ? : ok=2? ? changed=1? ? unreachable=0? ? failed=0

查看client?

[root@node5 ~]# cd /usr/local/src/

[root@node5 src]# ls

nginx-1.12.0.tar.gz

測(cè)試安裝nginx

[root@node4 templates]# ansible-playbook /etc/ansible/nginx_install.yaml?

PLAY [192.168.40.132] *********************************************************?

GATHERING FACTS ***************************************************************?

ok: [192.168.40.132]

TASK: [nginx_install | copy nginx package to remote host] *********************?

ok: [192.168.40.132]

TASK: [nginx_install | tar nginx] *********************************************?

changed: [192.168.40.132]

TASK: [nginx_install | install pakger] ****************************************?

ok: [192.168.40.132] => (item=openssl-devel,pcre-devel,gcc)

TASK: [nginx_install | useradd nginx] *****************************************?

changed: [192.168.40.132]

TASK: [nginx_install | install nginx] *****************************************?

changed: [192.168.40.132]

TASK: [nginx_install | copy start config] *************************************?

changed: [192.168.40.132]

TASK: [nginx_install | mkdir vhosts] ******************************************?

changed: [192.168.40.132]

TASK: [nginx_install | copy config file nginx.conf] ***************************?

changed: [192.168.40.132]

NOTIFIED: [nginx_install | start nginx] ***************************************?

changed: [192.168.40.132]

NOTIFIED: [nginx_install | reload nginx service] ******************************?

changed: [192.168.40.132]

PLAY RECAP ********************************************************************?

192.168.40.132? ?? ?? ?? ? : ok=11? ?changed=8? ? unreachable=0? ? failed=0

查看client的80端口是否啟動(dòng)

root@node5 ~]# netstat -ntlp

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:111? ?? ?? ?? ? 0.0.0.0:*? ?? ?? ?? ?? ?LISTEN? ?? ?1/systemd

tcp? ?? ???0? ?? ?0 0.0.0.0:80? ?? ?? ?? ???0.0.0.0:*? ?? ?? ?? ?? ?LISTEN? ?? ?11503/nginx: master?

tcp? ?? ???0? ?? ?0 0.0.0.0:22? ?? ?? ?? ???0.0.0.0:*? ?? ?? ?? ?? ?LISTEN? ?? ?917/sshd

tcp? ?? ???0? ?? ?0 127.0.0.1:25? ?? ?? ?? ?0.0.0.0:*? ?? ?? ?? ?? ?LISTEN? ?? ?1009/master

tcp6? ?? ? 0? ?? ?0 :::111? ?? ?? ?? ?? ?? ?:::*? ?? ?? ?? ?? ?? ???LISTEN? ?? ?1/systemd

tcp6? ?? ? 0? ?? ?0 :::22? ?? ?? ?? ?? ?? ? :::*? ?? ?? ?? ?? ?? ???LISTEN? ?? ?917/sshd

tcp6? ?? ? 0? ?? ?0 ::1:25? ?? ?? ?? ?? ?? ?:::*? ?? ?? ?? ?? ?? ???LISTEN? ?? ?1009/master

這就完成部署啦。

五、測(cè)試出現(xiàn)報(bào)錯(cuò)

[root@node4 templates]# ansible-playbook -C /etc/ansible/nginx_install.yaml?

PLAY [192.168.40.132] *********************************************************?

GATHERING FACTS ***************************************************************?

ok: [192.168.40.132]

TASK: [nginx_install | copy nginx package to remote host] *********************?

ok: [192.168.40.132]

TASK: [nginx_install | tar nginx] *********************************************?

skipping: [192.168.40.132]####這里代表跳過(guò)shell的測(cè)試,因?yàn)榈谝淮问褂茫瑹o(wú)法檢測(cè)。

ok: [192.168.40.132]

如果使用-C測(cè)試的話,會(huì)出現(xiàn)報(bào)錯(cuò)

日志報(bào)錯(cuò)如下:

Oct 25 2017 08:35:05 - SKIPPED - ...

Oct 25 2017 08:35:05 - OK - {"module_name": "shell", "module_args": "cd /usr/local/src;tar -xf nginx-1.12.0.tar.gz"}

=> {"msg": "check mode not supported for shell", "skipped": true}

這個(gè)問(wèn)題也不說(shuō)是報(bào)錯(cuò),只是ansible識(shí)別不了而已。

因?yàn)橹鬟壿嬑募惺褂昧藄hell。只要直接推送即可。


轉(zhuǎn)自

ansible 自動(dòng)編譯部署 nginx詳解

(出處: 樂(lè)維論壇)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容