系统角色的使用和角色构建创建方式
系統角色的使用和角色構建創建方式
1.控制執行順序
對于playbook中的每個play,任務按照任務列表中的順序來執行。執行完所有任務后,將執行任務通知的處理程序
在角色添加到play中后,角色任務將添加到任務列表的開頭。如果play中包含第二個角色,其任務列表添加到第一個角色之后。
角色處理程序添加到play中的方式與角色任務添加到play中相同。每個play定義一個處理程序列表。角色處理程序先添加到處理程序列表,后跟play的handlers部分中定義的任何處理程序。
在某些情形中,可能需要在角色之前執行一些play任務。若要支持這樣的情形,可以為play配置pre_tasks部分。列在此部分中的所有任務將在執行任何角色之前執行。如果這些任務中有任何一個通知了處理程序,則這些處理程序任務也在角色或普通任務之前執行。
此外,play也支持post_tasks關鍵字。這些任務在play的普通任務和它們通知的任何處理程序運行之后執行。
以下play演示了一個帶有pre_tasks、roles、tasks、post_tasks和handlers的示例。一個play中通常不會同時包含所有這些部分。
[root@master ansible]# cat httpd.tar/main.yml --- - hosts: 192.168.72.137vars: timesync_ntp_servers: - hostname: time1.aliyun.comiburst: yespre_tasks: - debug: msg: 'pre-tasks'notify: my handlerroles: - timesynctasks: - debug: msg: 'first task'notify: my handlerpost_tasks: - debug: msg: 'post-task'notify: my handlerhandlers: - name: my handlerdebug: msg: running my handler [root@master ansible]# [root@master ansible]# ansible-playbook httpd.tar/main.yml //執行成功PLAY [192.168.72.137] *********************************************************TASK [Gathering Facts] ********************************************************* ok: [192.168.72.137]TASK [debug] ******************************************************************* ok: [192.168.72.137] => {"msg": "pre-tasks" }TASK [timesync : Set version specific variables] ******************************* ok: [192.168.72.137]TASK [timesync : Populate service facts] *************************************** ok: [192.168.72.137]TASK [Set variable `timesync_services` with filtered uniq service names] ******* ok: [192.168.72.137]TASK [Check that variable 'timesync_services' is defined] ********************** ok: [192.168.72.137] => {"changed": false,"msg": "All assertions passed" }TASK [timesync : Check if only NTP is needed] ********************************** ok: [192.168.72.137]TASK [timesync : Check if single PTP is needed] ******************************** skipping: [192.168.72.137]TASK [timesync : Check if both NTP and PTP are needed] ************************* skipping: [192.168.72.137]TASK [timesync : Determine current NTP provider] ******************************* ok: [192.168.72.137]TASK [timesync : Select NTP provider] ****************************************** ok: [192.168.72.137]TASK [timesync : Install chrony] *********************************************** ok: [192.168.72.137]TASK [timesync : Install ntp] ************************************************** skipping: [192.168.72.137]TASK [timesync : Install linuxptp] ********************************************* skipping: [192.168.72.137]TASK [timesync : Gather package facts] ***************************************** ok: [192.168.72.137]TASK [timesync : Run phc_ctl on PTP interface] ********************************* skipping: [192.168.72.137]TASK [timesync : Check if PTP interface supports HW timestamping] ************** skipping: [192.168.72.137]TASK [timesync : Generate chrony.conf file] ************************************ ok: [192.168.72.137]TASK [timesync : Generate chronyd sysconfig file] ****************************** ok: [192.168.72.137]TASK [timesync : Generate ntp.conf file] *************************************** skipping: [192.168.72.137]TASK [timesync : Generate ntpd sysconfig file] ********************************* skipping: [192.168.72.137]TASK [timesync : Generate ptp4l.conf file] ************************************* skipping: [192.168.72.137]TASK [timesync : Generate ptp4l sysconfig file] ******************************** skipping: [192.168.72.137]TASK [timesync : Generate phc2sys sysconfig file] ****************************** skipping: [192.168.72.137]TASK [timesync : Generate timemaster.conf file] ******************************** skipping: [192.168.72.137]TASK [timesync : Update network sysconfig file] ******************************** ok: [192.168.72.137]TASK [timesync : Disable chronyd] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable ntpd] ************************************************* skipping: [192.168.72.137]TASK [timesync : Disable ntpdate] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable sntp] ************************************************* skipping: [192.168.72.137]TASK [timesync : Disable ptp4l] ************************************************ skipping: [192.168.72.137]TASK [timesync : Disable phc2sys] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable timemaster] ******************************************* skipping: [192.168.72.137]TASK [timesync : Enable chronyd] *********************************************** ok: [192.168.72.137]TASK [timesync : Enable ntpd] ************************************************** skipping: [192.168.72.137]TASK [timesync : Enable ptp4l] ************************************************* skipping: [192.168.72.137]TASK [timesync : Enable phc2sys] *********************************************** skipping: [192.168.72.137]TASK [timesync : Enable timemaster] ******************************************** skipping: [192.168.72.137]TASK [debug] ******************************************************************* ok: [192.168.72.137] => {"msg": "first task" }TASK [debug] ******************************************************************* ok: [192.168.72.137] => {"msg": "post-task" }PLAY RECAP ********************************************************************* 192.168.72.137 : ok=17 changed=0 unreachable=0 failed=0 skipped=23 rescued=0 ignored=0 [root@master ansible]#在上例中,每個部分中都執行debug任務來通知my handler處理程序。my handler任務執行了三次
- 在執行了所有pre_tasks任務后
- 在執行了所有角色任務和tasks部分中的任務后
- 在執行了所有post_tasks后
除了將角色包含在play的roles部分中外,也可以使用普通任務將角色添加到play中。使用include_role模塊可以動態包含角色,使用import_role模塊則可靜態導入角色。
以下playbook演示了如何通過include_role模塊來利用任務包含角色。
[root@master ansible]# cat httpd.tar/main.yml --- - hosts: 192.168.72.137vars: timesync_ntp_servers: - hostname: time1.aliyun.comiburst: yespower: truetasks: - name: debug: msg: 'first task'- name: timesyncinclude_role: name: timesynchandlers: - name: my handlerdebug: msg: running my handler [root@master ansible]#[root@master ansible]# ansible-playbook httpd.tar/main.yml PLAY [192.168.72.137] *********************************************************TASK [Gathering Facts] ********************************************************* ok: [192.168.72.137]TASK [debug] ******************************************************************* ok: [192.168.72.137] => {"msg": "first task" }TASK [timesync] ****************************************************************TASK [timesync : Set version specific variables] ******************************* ok: [192.168.72.137]TASK [timesync : Populate service facts] *************************************** ok: [192.168.72.137]TASK [Set variable `timesync_services` with filtered uniq service names] ******* ok: [192.168.72.137]TASK [Check that variable 'timesync_services' is defined] ********************** ok: [192.168.72.137] => {"changed": false,"msg": "All assertions passed" }TASK [timesync : Check if only NTP is needed] ********************************** ok: [192.168.72.137]TASK [timesync : Check if single PTP is needed] ******************************** skipping: [192.168.72.137]TASK [timesync : Check if both NTP and PTP are needed] ************************* skipping: [192.168.72.137]TASK [timesync : Determine current NTP provider] ******************************* ok: [192.168.72.137]TASK [timesync : Select NTP provider] ****************************************** ok: [192.168.72.137]TASK [timesync : Install chrony] *********************************************** ok: [192.168.72.137]TASK [timesync : Install ntp] ************************************************** skipping: [192.168.72.137]TASK [timesync : Install linuxptp] ********************************************* skipping: [192.168.72.137]TASK [timesync : Gather package facts] ***************************************** ok: [192.168.72.137]TASK [timesync : Run phc_ctl on PTP interface] ********************************* skipping: [192.168.72.137]TASK [timesync : Check if PTP interface supports HW timestamping] ************** skipping: [192.168.72.137]TASK [timesync : Generate chrony.conf file] ************************************ ok: [192.168.100.147]TASK [timesync : Generate chronyd sysconfig file] ****************************** ok: [192.168.100.147]TASK [timesync : Generate ntp.conf file] *************************************** skipping: [192.168.72.137]TASK [timesync : Generate ntpd sysconfig file] ********************************* skipping: [192.168.72.137]TASK [timesync : Generate ptp4l.conf file] ************************************* skipping: [192.168.72.137]TASK [timesync : Generate ptp4l sysconfig file] ******************************** skipping: [192.168.72.137]TASK [timesync : Generate phc2sys sysconfig file] ****************************** skipping: [192.168.72.137]TASK [timesync : Generate timemaster.conf file] ******************************** skipping: [192.168.72.137]TASK [timesync : Update network sysconfig file] ******************************** ok: [192.168.72.137]TASK [timesync : Disable chronyd] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable ntpd] ************************************************* skipping: [192.168.72.137]TASK [timesync : Disable ntpdate] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable sntp] ************************************************* skipping: [192.168.72.137]TASK [timesync : Disable ptp4l] ************************************************ skipping: [192.168.72.137]TASK [timesync : Disable phc2sys] ********************************************** skipping: [192.168.72.137]TASK [timesync : Disable timemaster] ******************************************* skipping: [192.168.72.137]TASK [timesync : Enable chronyd] *********************************************** ok: [192.168.72.137]TASK [timesync : Enable ntpd] ************************************************** skipping: [192.168.72.137]TASK [timesync : Enable ptp4l] ************************************************* skipping: [192.168.72.137]TASK [timesync : Enable phc2sys] *********************************************** skipping: [192.168.72.137]TASK [timesync : Enable timemaster] ******************************************** skipping: [192.168.72.137]PLAY RECAP ********************************************************************* 192.168.72.137 : ok=15 changed=0 unreachable=0 failed=0 skipped=23 rescued=0 ignored=0 [root@master ansible]#注意:
include_role模塊是在Ansible 2.3中新增的,而import_role模塊則是在Ansible 2.4中新增的
2.selinux角色實例
rhel-system-roles.selinux角色可以簡化SELinux配置設置的管理。它通過利用SELinux相關的Ansible模塊來實施。與自行編寫任務相比,使用此角色的優勢是它能讓用戶擺脫編寫這些任務的職責。取而代之,用戶將為角色提供變量以對其進行配置,且角色中維護的代碼將確保應用用戶需要的SELinux配置。
此角色可以執行的任務包括:
- 設置enforcing或permissive模式
- 對文件系統層次結構的各部分運行restorecon
- 設置SELinux布爾值
- 永久設置SELinux文件上下文
- 設置SELinux用戶映射
有時候,SELinux角色必須確保重新引導受管主機,以便能夠完整應用其更改。但是,它本身從不會重新引導主機。如此一來,用戶便可以控制重新引導的處理方式。
其工作方式為,該角色將一個布爾值變量selinux_reboot_required設為True,如果需要重新引導,則失敗。你可以使用block/rescure結構來從失敗中恢復,具體操作為:如果該變量未設為true,則讓play失敗,如果值是true,則重新引導受管主機并重新運行該角色。Play中的塊看起來應該類似于:
[root@master ansible]# cat httpd.tar/main.yml --- - hosts: 192.168.72.137tasks: - name: Apply SELinux roleblock:- name: role userinclude_role:name: selinuxrescue:- name: Check for failure for other reasons than required rebootfail:when: not selinux_reboot_required- name: Restart managed hostreboot:- name: Reapply SELinux role to complete changesinclude_role:name: selinux [root@master ansible]# [root@localhost ~]# getenforce Disabled [root@localhost ~]#3.配置selinux角色
用于配置rhel-system-roles.selinux角色的變量的詳細記錄位于其README.md文件中。以下示例演示了使用此角色的一些方法。
selinux_state變量設置SELinux的運行模式。它可以設為enforcing、permissive或disabled。如果未設置,則不更改模式
[root@master ansible]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@master ansible]#selinux_booleans變量取一個要調整的SELinux布爾值的列表作為值。列表中的每一項是變量的散列/字典:布爾值的name、state(它應是on還是off),以及該設置是否應在重新引導后persistent。
[root@master ansible]# cat httpd.tar/httpd1.yml --- - hosts: 192.168.72.137vars: PORT: 82tasks: - name: install httpdyum: name: httpdstate: present- name: config httpdtemplate: src: /etc/ansible/files/httpd.conf.j2dest: /etc/httpd/conf/httpd.conf- name: serivce httpdservice: name: httpdstate: started [root@master ansible]# [root@master ansible]# ansible-playbook httpd.tar/httpd1.yml PLAY [192.168.72.137] *********************************************************TASK [Gathering Facts] ********************************************************* ok: [192.168.72.137]TASK [install httpd] *********************************************************** ok: [192.168.72.137]TASK [config httpd] ************************************************************ changed: [192.168.72.137]TASK [serivce httpd] *********************************************************** changed: [192.168.72.137]PLAY RECAP ********************************************************************* 192.168.72.137 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0selinux_ports變量取應當具有特定SELinux類型的端口的列表作為值。
selinux_ports:- ports: '82'setype: 'http_port_t'proto: 'tcp'state: 'present'注意:想要設置上面這些內容,selinux狀態必須是enforcing,不然的話設置不了。
4.創建角色
角色創建流程
在Ansible中創建角色不需要特別的開發工具。創建和使用角色包含三個步驟:
默認情況下,Ansible在Ansible Playbook所在目錄的roles子目錄中查找角色。這樣,用戶可以利用playbook和其他支持文件存儲角色。
如果Ansible無法在該位置找到角色,它會按照順序在Ansible配置設置roles_path所指定的目錄中查找。此變量包含要搜索的目錄的冒號分隔列表。此變量的默認值為:
[root@master ~]# ls /usr/share/ansible/roles/ linux-system-roles.certificate linux-system-roles.crypto_policies linux-system-roles.ha_cluster linux-system-roles.kdump linux-system-roles.kernel_settings linux-system-roles.logging linux-system-roles.metrics linux-system-roles.nbde_client linux-system-roles.nbde_server linux-system-roles.network linux-system-roles.postfix linux-system-roles.selinux linux-system-roles.ssh linux-system-roles.sshd linux-system-roles.storage linux-system-roles.timesync linux-system-roles.tlog linux-system-roles.vpn rhel-system-roles.certificate rhel-system-roles.crypto_policies rhel-system-roles.ha_cluster rhel-system-roles.kdump rhel-system-roles.kernel_settings rhel-system-roles.logging rhel-system-roles.metrics rhel-system-roles.nbde_client rhel-system-roles.nbde_server rhel-system-roles.network rhel-system-roles.postfix rhel-system-roles.selinux rhel-system-roles.ssh rhel-system-roles.sshd rhel-system-roles.storage rhel-system-roles.timesync rhel-system-roles.tlog rhel-system-roles.vpn [root@master ~]#這允許用戶將角色安裝到由多個項目共享的系統上。例如,用戶可能將自己的角色安裝在自己的主目錄下的~/.ansible/roles子目錄中,而系統可能將所有用戶的角色安裝在/usr/share/ansible/roles目錄中。
每個角色具有自己的目錄,采用標準化的目錄結構。例如,以下目錄結構包含了定義motd角色的文件。
[root@master ansible]# tree roles/ roles/ ├── php.tar │ ├── php-7.2.8.tar.xz │ ├── php.sh │ └── php.yml └── timesync├── ansible_pytest_extra_requirements.txt├── COPYING├── custom_requirements.txt├── defaults│ └── main.yml├── handlers│ └── main.yml├── library│ └── timesync_provider.sh├── meta│ └── main.yml├── molecule_extra_requirements.txt├── pylint_extra_requirements.txt├── pylintrc├── pytest_extra_requirements.txt├── README.html├── README.md├── tasks│ └── main.yml├── templates│ ├── chrony.conf.j2│ ├── chronyd.sysconfig.j2│ ├── ntp.conf.j2│ ├── ntpd.sysconfig.j2│ ├── phc2sys.sysconfig.j2│ ├── ptp4l.conf.j2│ ├── ptp4l.sysconfig.j2│ └── timemaster.conf.j2├── tests│ ├── inventory.yaml.j2│ ├── provision.fmf│ ├── roles│ ├── tests_chrony.yml│ ├── tests_default_vars.yml│ ├── tests_default_wrapper.yml│ ├── tests_default.yml│ ├── tests_ntp_provider1.yml│ ├── tests_ntp_provider2.yml│ ├── tests_ntp_provider3.yml│ ├── tests_ntp_provider4.yml│ ├── tests_ntp_provider5.yml│ ├── tests_ntp_provider6.yml│ ├── tests_ntp_ptp.yml│ ├── tests_ntp.yml│ ├── tests_ptp_multi.yml│ └── tests_ptp_single.yml├── tox.ini└── vars├── CentOS_6.yml├── CentOS_9.yml├── default.yml├── Fedora_33.yml├── main.yml├── RedHat_6.yml└── RedHat_9.yml11 directories, 49 files [root@master ansible]#5.創建角色框架
可以使用標準Linux命令創建新角色所需的所有子目錄和文件。此外,也可以通過命令行實用程序來自動執行新角色創建過程。
ansible-galaxy命令行工具可用于管理Ansible角色,包括新角色的創建。用戶可以運行ansible-galaxy init來創建新角色的目錄結構。指定角色的名稱作為命令的參數,該命令在當前工作目錄中為新角色創建子目錄。
[root@master ansible]# ls ansible.cfg hosts httpd.tar roles files hosts_facts mysql.tar [root@master ansible]# cd roles/ [root@master roles]# ansible-galaxy init my_new_role - Role my_new_role was created successfully [root@master roles]# ls my_new_role/ defaults handlers README.md templates vars files meta tasks tests [root@master roles]#總結
以上是生活随笔為你收集整理的系统角色的使用和角色构建创建方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: soso地图api接口poi检索示例--
- 下一篇: 多言统计及R语言建模按组距为300编制频