ansible 角色定義及調用(nginx)
Roles的介紹
Roles是ansible自1.2版本引入的新特性,用於層次性,結構化地組織playbook,roles能夠根據層次型結構自動自動裝在變量文件、tasks以及handlers等。
創建roles的步驟
創建以roles命名的目錄:
在roles目錄中分別創建以各角色名稱命名的目錄,如webservers等:
在每個角色命名的目錄中分別創建files、handlers、meta、tasks、templates和vars目錄:用不到的目錄可以創建為空目錄,也可以不創建
在playbook文件中,調用各角色
roles內各目錄中可用的文件
tasks目錄:至少創建一個名為main.yml的文件,其定義了此角色的任務列表:此文件可以使用include包含其他的位於此目錄中的tasks文件
files目錄:存放由copy或者script等模塊調用的文件
templates目錄:templates模塊會自動在此目錄中尋找模板文件
handlers目錄:此目錄中應當包含一個main
yml文件:用於定義此角色用到的各handler:在handler中使用include包含的其他的handler文件也應該位於此目錄中
vars目錄:應當包含一個main.yml文件,用於定義此角色用到的變量
meta目錄:應當包含一個main.yml文件,用於定義此角色的特殊設定及其依賴關系:ansible 1.3及其以後的版本才支持
default目錄:為當前角色定義默認變量時使用此目錄,應該包含一個main.yml文件
實驗環境:
ansible:10.0.0.128
client :10.0.0.131
執行
1.在服務器生成免密鑰文件,推送到客戶端
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
2.安裝ansible
[root@ansible ~]# yum install -y ansible
3.到/etc/ansible 有個可以自定義roles的目錄
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg hosts nginx.yaml roles
4.定義要執行的角色路徑
[root@ansible ~]# cat /etc/ansible/nginx.yaml
- hosts: 10.0.0.131
remote_user: root
roles:
- nginx
5.定義掩碼安裝nginx,在roles目錄下的目錄及文件都要自己創建
[root@ansible roles]# ls
nginx
[root@ansible roles]# cd nginx
[root@ansible nginx]# ls
files handlers tasks templates vars
[root@ansible ansible]# cd roles/
[root@ansible roles]# tree
.
└── nginx
├── files
│ └── nginx-1.12.0.tar.gz
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ └── nginx.conf
└── vars
└── main.yml
6 directories, 5 files
6.進入tasks目錄創建任務
[root@ansible nginx]# cat tasks/main.yml
- name: copy nginx packup to remote host copy: src=nginx-1.12.0.tar.gz dest=/usr/local/src/nginx-1.12.0.tar.gz tags: cppkg - name: tar nginx shell: cd /usr/local/src/; tar -xf nginx-1.12.0.tar.gz - name: install packger yum: name={{ item }} state=latest with_items: - openssl-devel - pcre-devel - gcc- name: useradd shell: useradd nginx -s /sbin/nologin - 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 - name: copy conf file nginx.conf template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf notify: start nginx
7.存放nginx壓縮包目錄
[root@ansible nginx]# ls files/
nginx-1.12.0.tar.gz ##對應tasks第二行
8.template這一行對應的是template這個目錄和主服務端定義的變量
[root@ansible nginx]# cat templates/nginx.conf
#user nobody; worker_processes {{ ansible_processor_vcpus }}; #pid logs/nginx.pid; events { worker_connections 65532; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen {{ ngxport }}; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /web; 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; } #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; #} } include vhosts/*.conf; }
9.查看我們定義的變量,在vars目錄下
[root@ansible nginx]# cat vars/main.yml
ngxport: "8080"
10.編輯觸發器
[root@ansible nginx]# cat handlers/main.yml
- name: start nginx
shell: /usr/local/nginx/sbin/nginx
11.開始執行
[root@ansible nginx]# ansible-playbook /etc/ansible/nginx.yaml
PLAY [10.0.0.131] *************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.0.131]
TASK: [nginx | copy nginx packup to remote host] ******************************
changed: [10.0.0.131]
TASK: [nginx | tar nginx] *****************************************************
changed: [10.0.0.131]
TASK: [nginx | install packger] ***********************************************
ok: [10.0.0.131] => (item=openssl-devel,pcre-devel,gcc)
TASK: [nginx | useradd] *******************************************************
changed: [10.0.0.131]
TASK: [nginx | install nginx] *************************************************
changed: [10.0.0.131]
TASK: [nginx | copy conf file nginx.conf] *************************************
changed: [10.0.0.131]
NOTIFIED: [nginx | start nginx] ***********************************************
changed: [10.0.0.131]
PLAY RECAP ********************************************************************
10.0.0.131 : ok=8 changed=6 unreachable=0 failed=0
12.查看client客戶端nginx服務已經啟動
[root@zxb4 ~]# ps -ef |grep nginx
root 34655 1 0 02:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 34656 34655 1 02:13 ? 00:00:01 nginx: worker process
root 34660 28130 0 02:16 pts/1 00:00:00 grep --color=auto nginx
[root@zxb4 ~]# netstat -tulnp
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:8080 0.0.0.0:* LISTEN 34655/nginx: master
####附加
假如我們經常要增加nginx站點,直接寫好模板推送到vhos目錄:
[root@ansible templates]# cat temp_server.conf
server
{
listen80;
server_name{{ server_name }};
indexindex.php index.html;
root {{root_dir }};
}
##在vars定義變量:
[root@ansible vars]# cat main.yml
ngxport:"8080"
server_name:"www.xxx.com"
root_dir:"/web"
重寫tasks步驟:
[root@ansible tasks]# cat main.yml
- name:copy conf file nginx.conf # 調用templates模塊
template: src=temp_server.conf dest=/usr/local/nginx/conf/vhosts/{{server_name }}.conf
tags: ngxconf
notify: reload nginx service # 調用handlers模塊
本文出自 “XiaoBingZ” 博客,請務必保留此出處http://1767340368.blog.51cto.com/13407496/1975788
ansible 角色定義及調用(nginx)