溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ansible的roles角色詳解

發(fā)布時間:2020-07-04 10:41:38 來源:網(wǎng)絡(luò) 閱讀:490 作者:wx5c1cfd6e22842 欄目:系統(tǒng)運(yùn)維

1. Roles目錄結(jié)構(gòu)
2. Roles基本使用
3. 實例:使用Roles一鍵部署httpd,memcached(可根據(jù)自己需求增加task,以及roles的角色,比如再部署docker,可根據(jù)不同主機(jī)部署相關(guān)的任務(wù),指定tags和hosts。

這個roles主要是將已知的文件結(jié)構(gòu)預(yù)先是由roles定義好的,它會自動的加載這里面的文件內(nèi)容,例如任務(wù)handlers,都會自動去加載,也就是roles對已知的進(jìn)行分類分組,使我們很容易的去管理,roles提供一種層次化和結(jié)次化組織你的playbook,這也是我們?nèi)ナ褂盟哪康?,它不像playbook是一個具體的使用,而roles它是一個組織的方式,這種方式也是通過ansible的一個最佳實踐。

1. roles的目錄結(jié)構(gòu)

它在目錄下有個site,yaml,這個是一個統(tǒng)一的入口,因為它會關(guān)聯(lián)很多的文件,所以我們只需要對著這個統(tǒng)一的入口進(jìn)行操作
另外就是角色或者功能模塊進(jìn)行部署,像下面的webservers.yaml和fooservers.yml

site.yml 統(tǒng)一的入口,會關(guān)聯(lián)很多的文件
webservers.yml 角色或者功能模塊進(jìn)行部署
fooservers.yml hosts 角色或者功能模塊進(jìn)行部署
roles/ 角色目錄,下面又分為不同的模塊
common/ 像common 和webservers,相當(dāng)于兩個模塊,common用于存放一些公共資源的地方,例如可能會有很多的項目會用到共同的變量,或者有相同的依賴,那么都可以在common的這模塊下去定義,這個模塊也可以稱為角色,這個角色下面還有很多的目錄,像下面的文件都是屬于它的目錄,這些目錄就是區(qū)別不同的文件或者不同的任務(wù)
files/ 這個是角色部署時用到的文件,例如安裝一個包,安裝一個軟件可能會用到它的一個包,那么這個包可以放在這個位置,
templates/ 這個是它角色部署時用到的模版,例如jinja渲染一個配置文件,這里存放一些動態(tài)生成文件的,而其他文件的一個目錄,
tasks/ 任務(wù),具體要做哪些事
handlers/ 處理程序
vars/ 角色的變量
defaults/ 角色的默認(rèn)變量
meta/ 角色定義的一些元數(shù)據(jù)
webservers/ 跟上面的common一樣屬于模塊,下面是所屬的文件
files/。 角色部署時用到的文件
templates/ 角色部署時用到的模版
tasks/ 任務(wù),具體要做哪些事
handlers/ 處理程序
vars/ 定義的變量

2. Roles的基本使用


  • hosts: webservers 主機(jī)組
    roles:
    • common 模塊,下面可以定義很多的屬于它的目錄或者不同的任務(wù)
  • nginx. 這里就是引用下面的變量的,部署一個
  • php Nginx和php的網(wǎng)站平臺

定義角色來定義變量,


  • hosts: webservers
    roles:
    • common 使用這個roles后面也可以直接定義變量
  • role: nginx vars:
    dir: '/opt/a'
    app_port: 5000
    • role: php 在這個roles中直接去使用,
      vars:
      dir: '/opt/b'
      app_port: 5001

這里我們創(chuàng)建了hosts,這個我們的主機(jī)清單,roles需要放我們的模塊以及不同任務(wù)的目錄,要有一個site.yaml

[root@ansible roles-demo]# ls
hosts  roles  site.yaml
[root@ansible roles-demo]# mkdir roles/{common,nginx,php}/{file,templates,tasks,handlers}

在每個tasks下面都寫一個main的yaml,因為它會在你的task下找這個main,也就是這個tasks下的一個入口
查看目錄樹結(jié)構(gòu)

[root@ansible roles-demo]# tree .
.
|-- hosts
|-- roles
|   |-- common
|   |   |-- file
|   |   |-- handlers
|   |   |-- tasks
|   |   |   -- main.yaml
|   |   -- templates
|   |-- nginx
|   |   |-- file
|   |   |-- handlers
|   |   |-- tasks
|   |   |   -- main.yaml
|   |   -- templates
|   -- php
|       |-- file
|       |-- handlers
|       |-- main.yaml
|       |-- tasks
|       -- templates
- site.yaml

檢查語法,執(zhí)行
[root@ansible roles-demo]# ansible-playbook site.yaml -i hosts --syntax-check
[root@ansible roles-demo]# ansible-playbook site.yaml -i hosts
在site下去定義新的調(diào)用的變量這個會找目錄下對應(yīng)的任務(wù)比如tasks下的任務(wù)

---
- hosts: webservers
  gather_facts: no
  remote_user: root

  roles:
    - role: common

    - role: nginx
      vars:
        dir: /opt/a
        app_port: 80

    - role:  php
      vars:
        dir: /opt/b
        app_port: 81

在tasks下面去定義任務(wù)這個會找site.yaml下面的roles,然后執(zhí)行相關(guān)目錄下面的操作

[root@ansible nginx]# cat tasks/main.yaml 
- name: nginx task test
  debug: msg="nginx task test > {{dir}} {{app_port}}"

也可以寫一個組,在組里面去定義變量,這個all會幫你交給所有組,也可以定義webservers,就是定義單一這個組

[root@ansible roles-demo]# ls
group_vars  hosts  roles  site.yaml
[root@ansible roles-demo]# cd group_vars/
[root@ansible group_vars]# ls
all
[root@ansible group_vars]# cat all 
nginx_ver: 1.15
php_ver: 5.6

如果單一給這個site的單獨單個任務(wù)就可以定義標(biāo)簽在執(zhí)行的時候就可以執(zhí)行單個roles下的任務(wù)

[root@ansible roles-demo]# vim site.yaml 

---
- hosts: webservers
  gather_facts: no
  remote_user: root

  roles:
    - role: common

    - role: nginx
      vars:
        dir: /opt/a
        app_port: 80
      tags: nginx

    - role:  php
      vars:
        dir: /opt/b
        app_port: 81
      tags: php
[root@ansible roles-demo]# ansible-playbook site.yaml -i hosts --tags nginx

PLAY [webservers] *******************************************************************************************************************

TASK [nginx : nginx task test] ******************************************************************************************************
ok: [10.4.7.21] => {
    "msg": "nginx task test > /opt/a 80 1.15 5.6"
}
ok: [10.4.7.22] => {
    "msg": "nginx task test > /opt/a 80 1.15 5.6"
}

PLAY RECAP **************************************************************************************************************************
10.4.7.21                  : ok=1    changed=0    unreachable=0    failed=0   
10.4.7.22                  : ok=1    changed=0    unreachable=0    failed=0  

用register變量獲取k8s-master主機(jī)組上的狀態(tài)
這里也可以換成var: test,可以獲取全部的狀態(tài),使用msg進(jìn)行調(diào)式查看某個字段,獲取那個字段就可以使用msg
上述中使用”register“關(guān)鍵字將當(dāng)前shell任務(wù)的返回值寫入了名為test的變量中,第二個debug模塊輸出第一個任務(wù)中的注冊變量的值

- name: test
  shell: kubectl get node
  register: test
- name: shell
  debug:
    msg: "{{test.stdout_lines}}"

執(zhí)行命令

[root@hdss7-200 test]# ansible-playbook -i hosts test.yaml 

PLAY [test] *************************************************************************************************************************************************

TASK [nginx : test] *****************************************************************************************************************************************
changed: [10.4.7.11]

TASK [nginx : shell] ****************************************************************************************************************************************
ok: [10.4.7.11] => {
    "msg": [
        "NAME          STATUS   ROLES    AGE     VERSION", 
        "k8s-master1   Ready    <none>   4h47m   v1.16.0", 
        "k8s-node1     Ready    <none>   4h47m   v1.16.0", 
        "k8s-node2     Ready    <none>   4h47m   v1.16.0"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
10.4.7.11                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ig

3.一鍵使用Roles角色部署memcached和httpd
查看我的目錄樹

[root@hdss7-200 test-roles]# tree .
.
├── hosts
├── roles
│?? ├── httpd
│?? │?? └── tasks
│?? │??     └── main.yml
│?? └── memcached
│??     └── tasks
│??         └── main.yml
└── site.yaml

5 directories, 4 files

兩個文件一個目錄


[root@hdss7-200 test-roles]# ls
hosts  roles  site.yaml
[root@hdss7-200 test-roles]# ll
總用量 8
-rw-r--r-- 1 root root  18 12月  5 10:43 hosts
drwxr-xr-x 4 root root  36 12月  5 11:10 roles
-rw-r--r-- 1 root root 146 12月  5 11:18 site.yaml

主機(jī)組,這里可以根據(jù)你想部署到哪臺服務(wù)器,寫好你的主機(jī)組,如果想部署某一個服務(wù),根據(jù)roles的角色的tags部署某個服務(wù)

[root@hdss7-200 test-roles]# cat hosts
[roles]
10.4.7.22

統(tǒng)一的入口,包含部署的角色任務(wù),可以定義多個角色

[root@hdss7-200 test-roles]# cat site.yaml 

---
- hosts: roles
  gather_facts: no
  remote_user: root

  roles:
   - role: httpd
     tags: httpd

   - role: memcached
     tags: memcached 

這就是我們的roles下的tasks的任務(wù),之前的是寫在一個yml里面,對于復(fù)雜的任務(wù)我們就可以使用roles來實現(xiàn)

[root@hdss7-200 test-roles]# cd roles/
httpd/     memcached/ 
[root@hdss7-200 test-roles]# cat roles/httpd/tasks/main.yml 
- name: install httpd
  yum: name=httpd state=present

- name: start httpd
  systemd: name=httpd state=started enabled=yes

- name: httpd status
  shell: ps -ef |grep httpd
  register: test
- name: shell
  debug:
    msg: "{{test.stdout_lines}}"
- name: 
  debug:
      msg: "{{inventory_hostname}}"

msg是一個調(diào)試工具,里面的stdout_lines是這個取全局變量的一個值,可以取var: test 就可以得到所有的輸入的內(nèi)容,因為ansible執(zhí)行完是不會將執(zhí)行成功的內(nèi)容幫我們?nèi)ポ敵龀鰜?,所以我們需要自己去調(diào)他的系統(tǒng)輸出的變量

[root@hdss7-200 test-roles]# cat roles/memcached/tasks/main.yml 
- name: install memcached
  yum: name=memcached state=present

- name: start memcached
  systemd: name=memcached state=started enabled=yes daemon_reload=yes

- name: memcached status
  shell: ps -ef |grep memcached
  register: test
- name: memcached ps
  debug:
    msg: "{{test.stdout}}"

查看輸出的整體內(nèi)容

[root@hdss7-200 test-roles]# ansible-playbook -i hosts site.yaml

PLAY [roles] ***************************************************************************************************

TASK [httpd : install httpd] ***********************************************************************************
changed: [10.4.7.22]

TASK [httpd : start httpd] *************************************************************************************
changed: [10.4.7.22]

TASK [httpd : httpd status] ************************************************************************************
changed: [10.4.7.22]

TASK [httpd : shell] *******************************************************************************************
ok: [10.4.7.22] => {
    "msg": [
        "root      10375      1  3 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10376  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10377  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10378  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10379  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10380  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "root      10417  10416  0 11:28 pts/1    00:00:00 /bin/sh -c ps -ef |grep httpd", 
        "root      10419  10417  0 11:28 pts/1    00:00:00 grep httpd"
    ]
}

TASK [memcached : install memcached] ***************************************************************************
ok: [10.4.7.22]

TASK [memcached : start memcached] *****************************************************************************
ok: [10.4.7.22]

TASK [memcached : memcached status] ****************************************************************************
changed: [10.4.7.22]

TASK [memcached : memcached ps] ********************************************************************************
ok: [10.4.7.22] => {
    "msg": "memcach+  10136      1  0 11:21 ?        00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024\nroot      10575  10574 18 11:28 pts/1    00:00:00 /bin/sh -c ps -ef |grep memcached\nroot      10577  10575  0 11:28 pts/1    00:00:00 grep memcached"
}

PLAY RECAP *****************************************************************************************************
10.4.7.22                  : ok=8    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

比如單獨執(zhí)行某個任務(wù),就部署一個httpd,這樣的話就很靈活,這里還有一個debug這個的話,就是打印主機(jī)名

[root@hdss7-200 test-roles]# ansible-playbook -i hosts site.yaml --tags httpd

PLAY [roles] ***************************************************************************************************

TASK [httpd : install httpd] ***********************************************************************************
ok: [10.4.7.22]

TASK [httpd : start httpd] *************************************************************************************
ok: [10.4.7.22]

TASK [httpd : httpd status] ************************************************************************************
changed: [10.4.7.22]

TASK [httpd : shell] *******************************************************************************************
ok: [10.4.7.22] => {
    "msg": [
        "root      10375      1  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10376  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10377  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10378  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10379  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    10380  10375  0 11:28 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "root      10744  10743  0 11:46 pts/1    00:00:00 /bin/sh -c ps -ef |grep httpd", 
        "root      10746  10744  0 11:46 pts/1    00:00:00 grep httpd"
    ]
}

TASK [httpd : debug] *******************************************************************************************
ok: [10.4.7.22] => {
    "msg": "10.4.7.22"
}

PLAY RECAP *****************************************************************************************************
10.4.7.22                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

還可以增加一些新的任務(wù)比如批量創(chuàng)建文件
這里使用的{{item}}循環(huán),就可以減少我們?nèi)懚鄠€name

- name: create file
  file: path=/tmp/{{item}} state=touch
  with_items:
      - "1.txt"
      - "2.txt"
      - "3.txt"
      - "4.txt"

執(zhí)行效果

[root@hdss7-200 test-roles]# ansible-playbook -i hosts site.yaml --tags nginx

PLAY [roles] ***************************************************************************************************

TASK [nginx : create file] *************************************************************************************
changed: [10.4.7.22] => (item=1.txt)
changed: [10.4.7.22] => (item=2.txt)
changed: [10.4.7.22] => (item=3.txt)
changed: [10.4.7.22] => (item=4.txt)

PLAY RECAP *****************************************************************************************************
10.4.7.22                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI