系统角色的使用、角色结构创建方式

系统角色的使用、角色结构创建方式

1.角色的执行顺序

对于playbook中的每个play,任务将按照任务列表中的顺序执行。首先执行完所有任务后。再将任务通知的处理程序执行。

要在角色之前执行任务,可以给play配置pre_tasks部分
要在play的普通任务和它们通知的执行程序之后执行任务,可以用post_tasks部分

1.1系统角色 时间同步示例

[root@localhost ansible]# cat test.yml 
---
- hosts: node1
  vars:
    timesync_ntp_servers:
      - hostname: time1.aliyun.com
        iburst: yes
    power: true
  pre_tasks:
    - debug:
        msg: " pre-task "
      changed_when: power
      notify:
        - my handler
  roles:
    - timesync
  tasks: 
    - debug:
        msg: " first task "
      changed_when: power
      notify:
        - my handler
  post_tasks:
    - debug:
         msg: " post_task "
      changed_when: power
      notify:
        - my handler

  handlers:
    - name: my handler
      debug:
         msg: " running my handlers "

[root@localhost ansible]# ansible-playbook test.yml 

PLAY [node1] **************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [node1]

TASK [debug] **************************************************************************************************
changed: [node1] => {
    "msg": " pre-task "
}

RUNNING HANDLER [my handler] **********************************************************************************
ok: [node1] => {
    "msg": " running my handlers "
}

TASK [timesync : Check if only NTP is needed] *****************************************************************
ok: [node1]

TASK [timesync : Check if single PTP is needed] ***************************************************************
skipping: [node1]

TASK [timesync : Check if both NTP and PTP are needed] ********************************************************
skipping: [node1]

TASK [timesync : Determine current NTP provider] **************************************************************
ok: [node1]

TASK [timesync : Select NTP provider] *************************************************************************
ok: [node1]

TASK [timesync : Install chrony] ******************************************************************************
ok: [node1]

TASK [timesync : Install ntp] *********************************************************************************
skipping: [node1]

TASK [timesync : Install linuxptp] ****************************************************************************
skipping: [node1]

TASK [timesync : Run phc_ctl on PTP interface] ****************************************************************
skipping: [node1]

TASK [timesync : Check if PTP interface supports HW timestamping] *********************************************
skipping: [node1]

TASK [timesync : Get chrony version] **************************************************************************
ok: [node1]

TASK [timesync : Get ntp version] *****************************************************************************
skipping: [node1]

TASK [timesync : Generate chrony.conf file] *******************************************************************
ok: [node1]

TASK [timesync : Generate chronyd sysconfig file] *************************************************************
ok: [node1]

TASK [timesync : Generate ntp.conf file] **********************************************************************
skipping: [node1]

TASK [timesync : Generate ntpd sysconfig file] ****************************************************************
skipping: [node1]

TASK [timesync : Generate ptp4l.conf file] ********************************************************************
skipping: [node1]

TASK [timesync : Generate ptp4l sysconfig file] ***************************************************************
skipping: [node1]

TASK [timesync : Generate phc2sys sysconfig file] *************************************************************
skipping: [node1]

TASK [timesync : Generate timemaster.conf file] ***************************************************************
skipping: [node1]

TASK [timesync : Update network sysconfig file] ***************************************************************
ok: [node1]

TASK [timesync : Disable chronyd] *****************************************************************************
skipping: [node1]

TASK [timesync : Disable ntpd] ********************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service ntpd: host"}
...ignoring

TASK [timesync : Disable ntpdate] *****************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service ntpdate: host"}
...ignoring

TASK [timesync : Disable sntp] ********************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service sntp: host"}
...ignoring

TASK [timesync : Disable ptp4l] *******************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service ptp4l: host"}
...ignoring

TASK [timesync : Disable phc2sys] *****************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service phc2sys: host"}
...ignoring

TASK [timesync : Disable timemaster] **************************************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "Could not find the requested service timemaster: host"}
...ignoring

TASK [timesync : Enable chronyd] ******************************************************************************
ok: [node1]

TASK [timesync : Enable ntpd] *********************************************************************************
skipping: [node1]

TASK [timesync : Enable ptp4l] ********************************************************************************
skipping: [node1]

TASK [timesync : Enable phc2sys] ******************************************************************************
skipping: [node1]

TASK [timesync : Enable timemaster] ***************************************************************************
skipping: [node1]

TASK [debug] **************************************************************************************************
changed: [node1] => {
    "msg": " first task "
}

RUNNING HANDLER [my handler] **********************************************************************************
ok: [node1] => {
    "msg": " running my handlers "
}

TASK [debug] **************************************************************************************************
changed: [node1] => {
    "msg": " post_task "
}

RUNNING HANDLER [my handler] **********************************************************************************
ok: [node1] => {
    "msg": " running my handlers "
}

PLAY RECAP ****************************************************************************************************
node1                      : ok=22   changed=3    unreachable=0    failed=0    skipped=18   rescued=0    ignored=6   



1.2系统角色selinux示例

示例1

[root@localhost ansible]# cat  test.yml 
---
- hosts: node1
  tasks:
    - name: Apply Selinux role
      block:
        - name: role use
          include_role:
            name: selinux
      rescue:
        - name: Check for failure for other reasons than  required reboot
          fail:
          when: not selinux_reboot_required

        - name: Restart managed host
          reboot:

        - name: Reapply Selinux role to cpmplete changes
          include_role:
            name: selinux

[root@localhost ansible]# ansible-playbook test.yml 

PLAY [node1] **************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [node1]

TASK [role use] ***********************************************************************************************

TASK [selinux : Install SELinux python2 tools] ****************************************************************
skipping: [node1]

TASK [selinux : Install SELinux python3 tools] ****************************************************************
changed: [node1]

TASK [selinux : refresh facts] ********************************************************************************
ok: [node1]

TASK [selinux : Install SELinux tool semanage on Fedora] ******************************************************
changed: [node1]

TASK [selinux : Set permanent SELinux state if enabled] *******************************************************
skipping: [node1]

TASK [selinux : Set permanent SELinux state if disabled] ******************************************************
skipping: [node1]

TASK [selinux : Set ansible facts if needed] ******************************************************************
ok: [node1]

TASK [selinux : Fail if reboot is required] *******************************************************************
skipping: [node1]

TASK [selinux : debug] ****************************************************************************************
skipping: [node1]

TASK [selinux : Drop all local modifications] *****************************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux boolean local modifications] ************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux file context local modifications] *******************************************
skipping: [node1]

TASK [selinux : Purge all SELinux port local modifications] ***************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux login local modifications] **************************************************
skipping: [node1]

TASK [selinux : Reload SELinux policy] ************************************************************************
changed: [node1]

TASK [selinux : Set SELinux booleans] *************************************************************************

TASK [selinux : Set SELinux file contexts] ********************************************************************

TASK [selinux : Restore SELinux labels on filesystem tree] ****************************************************

TASK [selinux : Restore SELinux labels on filesystem tree in check mode] **************************************

TASK [selinux : Set an SELinux label on a port] ***************************************************************

TASK [selinux : Set linux user to SELinux user mapping] *******************************************************

PLAY RECAP ****************************************************************************************************
node1                      : ok=6    changed=3    unreachable=0    failed=0    skipped=16   rescued=0    ignored=0   


示例2

[root@localhost ansible]# cat test.yml 
---
- hosts: node1
  vars:
    PORT: 82
    selinux_state: enforcing
    seliux_ports:
      - ports: ' 82 '
        settype: 'httpd_port_t  '
        proto: ' tcp '
        state: ' present '
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: config hhtpd
      template:
        src: files/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf

    - name: seliunx rule for roles
      include_role:
        name: selinux

[root@localhost ansible]# ansible-playbook test.yml 

PLAY [node1] **************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [node1]

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

TASK [config hhtpd] *******************************************************************************************
ok: [node1]

TASK [seliunx rule for roles] *********************************************************************************

TASK [selinux : Install SELinux python2 tools] ****************************************************************
skipping: [node1]

TASK [selinux : Install SELinux python3 tools] ****************************************************************
ok: [node1]

TASK [selinux : refresh facts] ********************************************************************************
ok: [node1]

TASK [selinux : Install SELinux tool semanage on Fedora] ******************************************************
ok: [node1]

TASK [selinux : Set permanent SELinux state if enabled] *******************************************************
changed: [node1]

TASK [selinux : Set permanent SELinux state if disabled] ******************************************************
skipping: [node1]

TASK [selinux : Set ansible facts if needed] ******************************************************************
ok: [node1]

TASK [selinux : Fail if reboot is required] *******************************************************************
skipping: [node1]

TASK [selinux : debug] ****************************************************************************************
skipping: [node1]

TASK [selinux : Drop all local modifications] *****************************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux boolean local modifications] ************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux file context local modifications] *******************************************
skipping: [node1]

TASK [selinux : Purge all SELinux port local modifications] ***************************************************
skipping: [node1]

TASK [selinux : Purge all SELinux login local modifications] **************************************************
skipping: [node1]

TASK [selinux : Reload SELinux policy] ************************************************************************
changed: [node1]

TASK [selinux : Set SELinux booleans] *************************************************************************

TASK [selinux : Set SELinux file contexts] ********************************************************************

TASK [selinux : Restore SELinux labels on filesystem tree] ****************************************************

TASK [selinux : Restore SELinux labels on filesystem tree in check mode] **************************************

TASK [selinux : Set an SELinux label on a port] ***************************************************************

TASK [selinux : Set linux user to SELinux user mapping] *******************************************************

PLAY RECAP ****************************************************************************************************
node1                      : ok=9    changed=3    unreachable=0    failed=0    skipped=15   rescued=0    ignored=0   

2.角色目录结构创建方式

1.目录结构

默认情况下,Ansible在Ansible Playbook所在目录的roleq子目录中查找角色。这样,用户可以利用playbook和其他支持文件存储角色。

如果Ansible无法在该位置找到角色,它会按照顺序在Ansible配置设置roles_path所指定的目录中查找。此变量包含要搜索的目录的冒号分隔列表。此变量的默认值为:

~/ .ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles

这允许用户将角色安装到由多个项目共享的系统上。例如,用户可能将自己的角色安装在自己的主目录下的~l.ansible/roles子目录中,而系统可能将所有用户的角色安装在/usr/share/ansible/roles目录中。

每个角色具有自己的目录,采用标准化的目录结构。例如,以下目录结构包含了定义motd角色的文件。

[root@localhost roles]# tree motd/
motd/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

defaults 此目录中的main.yml文件包含角色变量的默认值,使用角色时可以覆盖这些默认值。 这些变量的优先级较低,应该在play中更改和自定义。

files 此目录包含由角色任务引用的静态文件。

handlers 此目录中的main.yml文件包含角色的处理程序定义。

meta 此目录中的main.yml文件包含与角色相关的信息,如作者、许可证、平台和可选的角色依赖项。

README.md 提供人类可读的基本描述、有关如何使用该角色的文档和示例,以及其发挥作用需要满足的任何非ansible要求

meta 此目录中的main.yml文件包含与角色相关的信息,如作者、许可证、平台和可选的角色依赖项。

tasks 此目录中的main.yml文件包含角色的任务定义。

templates 此目录包含由角色任务引用的Jinja2模板。

tests 此目录可以包含清单和名为test.ymlplaybook,可用于测试角色。

vars 此目录中的main.yml文件定义角色的变量值。这些变量通常用于角色内部用途。 这些变量的优先级较高,在playbook中使用时不应更改。

2.创建角色框架

可以使用标准Linux命令创建新角色所需的所有子目录和文件。此外,也可以通过命令行实用程序来自动执行新角色创建过程。
ansible-galaxy命令行工具可用于管理Ansible角色,包括新角色的创建。用户可以运行ansible-galaxy init来创建新角色的目录结构。指定角色的名称作为命令的参数,该命令在当前工作目录中为新角色创建子目录。

[root@localhost ansible]# cd roles/
[root@localhost roles]# ansible-galaxy init   motd
- Role motd was created successfully
[root@localhost roles]# ls
motd  selinux  timesync


[root@localhost roles]# tree motd/
motd/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值