ansible基礎-ansible角色的使用


              ansible基礎-ansible角色的使用

                                        作者:尹正傑 

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

  我們建議把多個節點都會用到的功能將其定義模塊,然后誰要用到該模塊就直接調用即可!而在ansible中它有一個特有的名稱,即角色。

 

 

一.角色相關概念

1>.每個角色都是以特定的層級目錄結構進行組織

    我們知道ansible可以自定義模塊,便於自己或他人調用,它也有一個特有的名稱叫做角色。每個角色對應的服務可能不太一樣,比如mysql,httpd,nginx,memcached。雖然每個角色(模塊)的功能不一樣,但是他們都以特定的目錄結構進行組織,相關說明如下所述:


files:
    存放由copy或script模塊等調用的文件。

templates:
    template模塊查找所需要模板文件的目錄。

tasks:
    用於定義任務,至少應該包含一個名為main.yml的文件(類似於java和go等編譯性語言,用於指定程序的入口),其他的文件需要在此文件中通過include進行包含。

handlers:
    定義處理器,至少應該包含一個名為main.yml的文件,其他的文件需要在此文件中通過include進行包含。

vars:
    定義變量,至少應該包含一個名為main.yml的文件,其他的文件需要在此文件中通過include進行包含。

meta:
    定義元數據,至少應該包含一個名為main.yml的文件,定義當前角色的特殊設定及其依賴關系,其他的文件需要在此文件中通過include進行包含。

default:
    設定模式變量時使用此目錄中的main.yml文件。

2>.playbook調用角色方法

在playbook調用角色方法1:
- hosts: web
  remote_user: root
  roles:
  - mysql
  - memcached
  - nginx


在playbook調用角色方法2:傳遞變量給角色
- hosts: web
  remote_user: root
  roles:
  - mysql
  - memcached
  - nginx

3>.ansible默認存放路徑

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# grep roles_path /etc/ansible/ansible.cfg 
roles_path    = /etc/ansible/roles:/usr/share/ansible/roles
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

 

二.角色實戰案例一

1>.創建初始化目錄(所有目錄不一定都必須存在,如果用不到對應的功能咱們也可以不創建)

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# mkdir -pv ./{nginx,tomcat,redis,memcached}/{files,templates,tasks,handlers,vars,meta,default}
mkdir: created directory ‘./nginx’
mkdir: created directory ‘./nginx/files’
mkdir: created directory ‘./nginx/templates’
mkdir: created directory ‘./nginx/tasks’
mkdir: created directory ‘./nginx/handlers’
mkdir: created directory ‘./nginx/vars’
mkdir: created directory ‘./nginx/meta’
mkdir: created directory ‘./nginx/default’
mkdir: created directory ‘./tomcat’
mkdir: created directory ‘./tomcat/files’
mkdir: created directory ‘./tomcat/templates’
mkdir: created directory ‘./tomcat/tasks’
mkdir: created directory ‘./tomcat/handlers’
mkdir: created directory ‘./tomcat/vars’
mkdir: created directory ‘./tomcat/meta’
mkdir: created directory ‘./tomcat/default’
mkdir: created directory ‘./redis’
mkdir: created directory ‘./redis/files’
mkdir: created directory ‘./redis/templates’
mkdir: created directory ‘./redis/tasks’
mkdir: created directory ‘./redis/handlers’
mkdir: created directory ‘./redis/vars’
mkdir: created directory ‘./redis/meta’
mkdir: created directory ‘./redis/default’
mkdir: created directory ‘./memcached’
mkdir: created directory ‘./memcached/files’
mkdir: created directory ‘./memcached/templates’
mkdir: created directory ‘./memcached/tasks’
mkdir: created directory ‘./memcached/handlers’
mkdir: created directory ‘./memcached/vars’
mkdir: created directory ‘./memcached/meta’
mkdir: created directory ‘./memcached/default’
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# tree .
.
├── memcached
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── nginx
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── redis
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
└── tomcat
    ├── default
    ├── files
    ├── handlers
    ├── meta
    ├── tasks
    ├── templates
    └── vars

32 directories, 0 files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# mkdir -pv ./{nginx,tomcat,redis,memcached}/{files,templates,tasks,handlers,vars,meta,default}

2>.編寫nginx角色

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# ll
total 0
drwxr-xr-x. 9 root root 97 Mar  9 07:36 memcached
drwxr-xr-x. 9 root root 97 Mar  9 07:36 nginx
drwxr-xr-x. 9 root root 97 Mar  9 07:36 redis
drwxr-xr-x. 9 root root 97 Mar  9 07:36 tomcat
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# tree nginx/
nginx/
├── default
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   └── server.conf.j2
└── vars
    └── main.yml

7 directories, 5 files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/files/index.html 
<h1>尹正傑到此一游</h1>
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/files/index.html
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/handlers/main.yml 
- name: reload nginx
  service: name=nginx state=reloaded
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/handlers/main.yml
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/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 }}/
- name: install configure 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
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/tasks/main.yml
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/templates/server.conf.j2 
server {
    listen 7000;
    server_name {{ ansible_fqdn }} {{ ansible_hostname }};
    
    location / {
        root {{ docroot }};
        index index.jsp index.html;
    }
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/templates/server.conf.j2
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/vars/main.yml 
docroot: /yinzhengjie/data/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/vars/main.yml

3>.編寫劇本調用nginx角色

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx.yaml 
- hosts: all
  remote_user: root
  roles:
  - nginx
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check nginx.yaml 

playbook: nginx.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx.yaml 

PLAY [all] **************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node102.yinzhengjie.org.cn]
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] *****************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]

TASK [nginx : install nginx package] ************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]

TASK [nginx : create doc root] ******************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]

TASK [nginx : install home page] ****************************************************************************************************************************
changed: [node103.yinzhengjie.org.cn]
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]

TASK [nginx : install configure file] ***********************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]

TASK [nginx : start nginx service] **************************************************************************************************************************
changed: [node103.yinzhengjie.org.cn]
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]

RUNNING HANDLER [nginx : reload nginx] **********************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]

PLAY RECAP **************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok=8    changed=6    unreachable=0    failed=0   
node102.yinzhengjie.org.cn : ok=8    changed=6    unreachable=0    failed=0   
node103.yinzhengjie.org.cn : ok=8    changed=6    unreachable=0    failed=0   

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook  nginx.yaml 

PLAY [all] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]

TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]

TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]

TASK [nginx : install configure file] *********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]

TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]

RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]

PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok=8    changed=2    unreachable=0    failed=0   
node102.yinzhengjie.org.cn : ok=8    changed=3    unreachable=0    failed=0   
node103.yinzhengjie.org.cn : ok=8    changed=2    unreachable=0    failed=0   

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx.yaml                       #運行劇本,注意,安裝nginx時請務必關閉掉httpd服務,否則可能會導致nginx服務啟動不了!
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -ntl | grep 7000'
node101.yinzhengjie.org.cn | SUCCESS | rc=0 >>
LISTEN     0      128          *:7000                     *:*                  

node103.yinzhengjie.org.cn | SUCCESS | rc=0 >>
LISTEN     0      128          *:7000                     *:*                  

node102.yinzhengjie.org.cn | SUCCESS | rc=0 >>
LISTEN     0      128          *:7000                     *:*                  

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'
node101.yinzhengjie.org.cn | SUCCESS | rc=0 >>
server {
    listen 7000;
    server_name node101.yinzhengjie.org.cn node101;
    
    location / {
        root /yinzhengjie/data/nginx;
        index index.jsp index.html;
    }
}

node103.yinzhengjie.org.cn | SUCCESS | rc=0 >>
server {
    listen 7000;
    server_name node103.yinzhengjie.org.cn node103;
    
    location / {
        root /yinzhengjie/data/nginx;
        index index.jsp index.html;
    }
}

node102.yinzhengjie.org.cn | SUCCESS | rc=0 >>
server {
    listen 7000;
    server_name node102.yinzhengjie.org.cn node102;
    
    location / {
        root /yinzhengjie/data/nginx;
        index index.jsp index.html;
    }
}

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -ntl | grep 7000'            #檢查nginx服務是否正常啟動
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn:7000/
<h1>尹正傑到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# curl http://node102.yinzhengjie.org.cn:7000/
<h1>尹正傑到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# curl http://node103.yinzhengjie.org.cn:7000/
<h1>尹正傑到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 

 

三.角色實戰案例二

1>.備份之前的配置

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# ll
total 0
drwxr-xr-x. 9 root root 97 Mar  9 07:36 memcached
drwxr-xr-x. 9 root root 97 Mar  9 07:36 nginx
drwxr-xr-x. 9 root root 97 Mar  9 07:36 redis
drwxr-xr-x. 9 root root 97 Mar  9 07:36 tomcat
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cp -a nginx/ /root/
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cp -a nginx/ /root/

2>. 編寫nginx角色

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# pwd
/etc/ansible/roles/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# ll
total 0
drwxr-xr-x. 2 root root  6 Mar  9 07:36 default
drwxr-xr-x. 2 root root 23 Mar  9 07:55 files
drwxr-xr-x. 2 root root 21 Mar  9 07:54 handlers
drwxr-xr-x. 2 root root  6 Mar  9 07:36 meta
drwxr-xr-x. 2 root root 21 Mar  9 09:20 tasks
drwxr-xr-x. 2 root root 47 Mar  9 09:20 templates
drwxr-xr-x. 2 root root 21 Mar  9 08:59 vars
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# tree .
.
├── default
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   ├── proxy.conf.j2
│   └── server.conf.j2
└── vars
    └── main.yml

7 directories, 6 files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat files/index.html 
<h1>尹正傑到此一游</h1>
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat files/index.html
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat handlers/main.yml 
- name: reload nginx
  service: name=nginx state=reloaded
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat handlers/main.yml
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat  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 configure file
  template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
  when: servertype == 'web'
  notify: reload nginx

- name: install proxy configure 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
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat tasks/main.yml
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/proxy.conf.j2 
server {
    listen 7500;
    server_name {{ ansible_fqdn }} {{ ansible_hostname }};
    
    location / {
        proxy_pass {{ backendurl }}
    }
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/proxy.conf.j2
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/server.conf.j2 
server {
    listen 7000;
    server_name {{ ansible_fqdn }} {{ ansible_hostname }};
    
    location / {
        root {{ docroot }};
        index index.jsp index.html;
    }
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/server.conf.j2
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat vars/main.yml 
servertype: web
backendurl: 'http:node101.yinzhengjie.org.cn:8080/'
docroot: /yinzhengjie/data/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# 
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat vars/main.yml

3>.編寫劇本調用nginx角色

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts 
[web]
node[101:102].yinzhengjie.org.cn 


[tomcat]
node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx-v2.yaml
- hosts: tomcat
  remote_user: root
  roles:
  - { role: nginx, servertype=proxy }


- hosts: web
  remote_user: root
  roles:
  - nginx
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx-v2.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook  --check nginx-v2.yaml

PLAY [tomcat] *********************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]

TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

PLAY [web] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]
skipping: [node102.yinzhengjie.org.cn]

TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok=14   changed=0    unreachable=0    failed=0   
node102.yinzhengjie.org.cn : ok=7    changed=0    unreachable=0    failed=0   

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx-v2.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook   nginx-v2.yaml

PLAY [tomcat] *********************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

TASK [nginx : install web configure file] *****************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]

TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]

TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]

RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]

PLAY [web] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]

TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]
skipping: [node102.yinzhengjie.org.cn]

TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]

RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node102.yinzhengjie.org.cn]

PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok=15   changed=2    unreachable=0    failed=0   
node102.yinzhengjie.org.cn : ok=8    changed=2    unreachable=0    failed=0   

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx-v2.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'
node101.yinzhengjie.org.cn | SUCCESS | rc=0 >>
server {
    listen 7000;
    server_name node101.yinzhengjie.org.cn node101;
    
    location / {
        root /yinzhengjie/data/nginx;
        index index.jsp index.html;
    }
}

node103.yinzhengjie.org.cn | FAILED | rc=1 >>
cat: /etc/nginx/conf.d/server.conf: No such file or directorynon-zero return code

node102.yinzhengjie.org.cn | SUCCESS | rc=0 >>
server {
    listen 7000;
    server_name node102.yinzhengjie.org.cn node102;
    
    location / {
        root /yinzhengjie/data/nginx;
        index index.jsp index.html;
    }
}

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts 
node[101:103].yinzhengjie.org.cn 
[web]
node[101:102].yinzhengjie.org.cn 


[tomcat]
node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# 
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'

 

  角色的使用就介紹到這里了,其實我們可以用角色可以干很多我們想要干的事情,比如:設定基於msm的tomcat集群的角色,為tomcat部署應用,每個節點本地使用nginx發現代理,所有節點前端有一個nginx負載均衡器等等之類的!感興趣的小伙伴可以自行測試~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM