角色的應用
- 1.角色的基本操作
- 2.使用要求檔案安裝角色
- 3.管理下載下傳的角色
1.角色的基本操作
- 使用角色的幫助文檔
[[email protected] ~]# ansible-galaxy role --help
usage: ansible-galaxy role [-h] ROLE_ACTION ...
positional arguments:
ROLE_ACTION
init Initialize new role with the base structure of a role.
remove Delete roles from roles_path.
delete Removes the role from Galaxy. It does not remove or alter the
actual GitHub repository.
list Show the name and version of each role installed in the
roles_path.
search Search the Galaxy database by tags, platforms, author and
multiple keywords.
import Import a role
setup Manage the integration between Galaxy and the given source.
info View more details about a specific role.
install Install role(s) from file(s), URL(s) or Ansible Galaxy
optional arguments:
-h, --help show this help message and exit
[[email protected] ~]#
- 建立角色
- 删除已安裝的角色
- 列出所有角色的詳細資訊
- 列出目前目錄下的角色資訊
- 搜尋角色
- 搜尋角色的平台
- 檢視角色資訊
- 安裝角色
- 安裝在目前位置
2.使用要求檔案安裝角色
可以使用ansible-galaxy,根據某一文本檔案中的定義來安裝一個角色清單。例如,如果使用者的一個playbook需要安裝特定的角色,可以在項目目錄中建立一個roles/requirements.yml檔案來指定所需的角色。此檔案充當playbook項目的依賴項清單,使得playbook的開發和調試能與任何支援角色分開進行。
例如,一個用于安裝geerlingguy.redis的簡單requirements.yml可能類似于如下:
- src: geerlingguy.redis
version: "1.5.0"
src屬性指定角色的來源,本例中為來自Ansible Galaxy的geerlingguy.redis角色。version屬性是可選的,指定要安裝的角色版本,本例中為1.5.0。
重要:
應當在requirements.yml檔案中指定角色版本,特别是生産環境中的playbook。
如果不指定版本,将會擷取角色的最新版本。如果作者對角色做出了更改,并與使用者的playbook不相容,這可能會造成自動化失敗或其他問題。
若要使用角色檔案來安裝角色,可使用-r REQUIREMENTS-FILE選項:
ansible-galaxy install -r roles/requirements.yml -p roles
使用者可以使用ansible-galaxy來安裝不在Ansible Galaxy中的角色。可以在私有的Git存儲庫或Web伺服器上托管自有的專用或内部角色。下例示範了如何利用各種遠端來源配置要求檔案。
[[email protected] project]# cat roles/requirements.yml
# from Ansible Galaxy, using the latest version
- src: geerlingguy.redis
# from Ansible Galaxy, overriding the name and using a specific version
- src: geerlingguy.redis
version: "1.5.0"
name: redis_prod
# from any Git-based repository, using HTTPS
- src: https://gitlab.com/guardianproject-ops/ansible-nginx-acme.git
scm: git
version: 56e00a54
name: nginx-acme
# from any Git-based repository, using SSH
- src: [email protected]:guardianproject-ops/ansible-nginx-acme.git
scm: git
version: master
name: nginx-acme-ssh
# from a role tar ball, given a URL
# supports 'http', 'https', or 'file' protocols
- src: file:///opt/local/roles/myrole.tar
name: myrole
src關鍵字指定Ansible Galaxy角色名稱。如果角色沒有托管在Ansible Galaxy中,則src關鍵字将指明角色的URL。
如果角色托管在來源控制存儲庫中,則需要使用scm屬性。ansible-galaxy指令能夠從基于git或mercurial的軟體存儲庫下載下傳和安裝角色。基于Git的存儲庫要求scm值為git,而托管在Mercurial存儲庫中的角色則要求值為hg。如果角色托管在Ansible Galaxy中,或者以tar存檔形式托管在Web伺服器上,則省略scm關鍵字。
name關鍵字用于覆寫角色的本地名稱。version關鍵字用于指定角色的版本。version關鍵字可以是與嚴自角色的軟體存儲庫的分支、标記或送出哈希對應的任何值。
若要安裝與playbook項目關聯的角色,可執行ansible-galaxy install指令:
- 示例:
[[email protected] project]# vim requirements.yml
[[email protected] project]# cat requirements.yml
- src: https://github.com/geerlingguy/ansible-role-nginx.git
scm: git
version: master
name: geerlingguy.nginx
[[email protected] project]# ansible-galaxy role install -r requirements.yml -p /opt/project/playbook/roles/
- extracting geerlingguy.nginx to /opt/project/playbook/roles/geerlingguy.nginx
- geerlingguy.nginx (master) was installed successfully
[[email protected] project]# ls /opt/project/playbook/roles/
geerlingguy.nginx httpd nginx
[[email protected] project]#
3.管理下載下傳的角色
ansible-galaxy指令也可管理本地的角色,如位于playbook項目的roles目錄中的角色。ansible-galaxy list子指令列出本地找到的角色。
ansible-galaxy list
可以使用ansible-galaxy remove子指令本地删除角色。
ansible-galaxy remove nginx-acme-ssh
ansible-galaxy list
在playbook中使用下載下傳并安裝的角色的方式與任何其他角色都一樣。在roles部分中利用其下載下傳的角色名稱來加以引用。如果角色不在項目的roles目錄中,則将檢查roles_path來檢視角色是否安裝在了其中一個目錄中,将使用第一個比對項。以下use-role.ymlplaybook引用了redis_prod和geerlingguy.redis角色:
[[email protected] project]# cat use-role.yml
---
- name: use redis_prod for prod machines
hosts: redis_prod_servers
remote_user: devops
become: True
roles:
- redis_prod
- name: use geerlingguy.redis for Dev machines
hosts: redis_dev_servers
remote_user: devops
become: True
roles:
- geerlingguy.redis
此playbook使不同版本的geerlingguy.redis角色應用到生産和開發伺服器。借助這種方式可以對角色更改進行系統化測試和內建,然後再部署到生産伺服器上。如果角色的近期更改造成了問題,則借助版本控制來開發角色,就能復原到過去某一個穩定的角色版本。