执行顺序、时间同步

控制执行顺序

  • 对于playbook中的每个play,任务按照任务列表中的顺序来执行
  • 在角色添加到play中后,角色任务将添加到任务列表的开头
  • 如果play中包含第二个角色,其任务列表添加到第一个角色之后
  • 在某些情形中,可能需要在角色之前执行一些play任务
  • 若要支持这样的情形,可以为play配置pre_tasks部分,列在此部分中的所有任务将在执行任何角色之前执行。如果这些任务中有任何一个通知了处理程序,则这些处理程序任务也在角色或普通任务之前执行。

以下通过一张图来解释执行顺序:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dVYHVFpa-1628081846648)(C:\Users\86155\Desktop\执行顺序.png)]

以下使用角色timesync来演示以下:

---
- hosts: 192.168.218.128
  pre_tasks:
    - debug:
        msg: 'pre-task'
      notify: my handler

  roles:
    - timesync

  tasks:
    - debug:
        msg: 'first task'
      notify: my handler
  post_tasks:
    - debug:
        msg: 'post-task'
      notify: my handler
  handlers:
    - name: my handler
      debug:
        msg: Running my handler

  • 在上例中,每个部分中都执行debug任务来通知my handler处理程序
  • my handler任务执行了三次
    • 在执行了所有pre_tasks任务后
    • 在执行了所有角色任务和tasks部分中的任务后
    • 在执行了所有post_tasks后

利用系统角色重用内容

  • 在RHEL8中,该软件包可以从AppStream中获取

RHEL系统角色

安装RHEL系统角色

  • RHEL系统角色由rhel-system-roles软件包提供,该软件包可从AppStream流获取。
  • 在Ansible控制节点上安装该软件包。
    安装RHEL系统角色
[root@192 ~]# yum -y install rhel-system-roles

安装后,RHEL系统角色位于/usr/share/ansible/roles目录中

[root@192 ~]# cd /usr/share/ansible/roles/
[root@192 roles]# ls 
linux-system-roles.certificate      rhel-system-roles.certificate
linux-system-roles.crypto_policies  rhel-system-roles.crypto_policies
linux-system-roles.ha_cluster       rhel-system-roles.ha_cluster
linux-system-roles.kdump            rhel-system-roles.kdump
linux-system-roles.kernel_settings  rhel-system-roles.kernel_settings
linux-system-roles.logging          rhel-system-roles.logging
linux-system-roles.metrics          rhel-system-roles.metrics
linux-system-roles.nbde_client      rhel-system-roles.nbde_client
linux-system-roles.nbde_server      rhel-system-roles.nbde_server
linux-system-roles.network          rhel-system-roles.network
linux-system-roles.postfix          rhel-system-roles.postfix
linux-system-roles.selinux          rhel-system-roles.selinux
linux-system-roles.ssh              rhel-system-roles.ssh
linux-system-roles.sshd             rhel-system-roles.sshd
linux-system-roles.storage          rhel-system-roles.storage
linux-system-roles.timesync         rhel-system-roles.timesync
linux-system-roles.tlog             rhel-system-roles.tlog

  • 每个角色的文档目录均包含一个README.md文件。
  • README.md文件含有角色的说明,以及角色用法信息。
  • README.md文件也会说明影响角色行为的角色变量。
  • 通常,README.md文件中含有一个playbook代码片段,用于演示常见配置场景的变量设置。
  • RHEL系统角色的角色文档与Linux系统角色的文档相匹配。使用Web浏览器来访问位于Ansible Galaxy网站https://galaxy.ansible.com/docs/上的角色文档。

时间同步角色示例

  • 该角色的详细记录位于/usr/share/doc/rhel-system-roles/timesync目录下的README.md中
  • 此文件说明了影响角色行为的所有变量,还包含演示了不同时间同步配置的三个playbook代码片段。
    timesync_ntp_servers属性
    |属性|用途|
    |—|---|
    |hostname|要与其同步的NTP服务器的主机名|
    |iburst|一个布尔值,用于启用或禁用快速初始同步。在角色中默认为no,但通常应该将属性设为yes|

以下示例,play使用rhel-system-roles.timesync角色将受管主机配置为利用快速初始同步NTP服务器获取时间:

//再playbook下创建了一个roles目录,把/usr/share/ansible/roles/rhel-system-roles.timesync复制到roles目录下,为了编辑时方便一点,改名为timesync 
[root@192 roles]# cp -a /usr/share/ansible/roles/rhel-system-roles.timesync   timesync
[root@192 roles]# ls 
timesync

//编辑playbook
[root@192 playbook]# vim time.yml 

--- 
- hosts: 192.168.218.128
  vars:
    timesync_ntp_servers:  
      - hostname: time1.aliyun.com
        iburst: yes

  roles:
    - timesync   //调用角色
                
//查看受管主机上的时间
[root@localhost ~]# date
Wed Aug  4 15:40:53 CST 2021

//随意修改一个时间,以便于后面测试容易看出效果
[root@localhost ~]# date -s "1990-6-2 12:00:00"
Sat Jun  2 12:00:00 CDT 1990
[root@localhost ~]# date 
Sat Jun  2 12:00:06 CDT 1990

//在执行时间同步角色之前关闭虚拟机自带的时间同步功能,不然再执行时间同步校色的时候,无法辨别是角色起到的效果还是虚拟机自身时间同步的效果

//执行playbook
[root@192 playbook]# ansible-playbook time.yml 
//受管主机查看
[root@localhost ~]# date 
Wed Aug  4 15:50:19 CST 2021  //已恢复正常时间

[root@localhost ~]# cat /etc/chrony.conf
# Ansible managed

server time1.aliyun.com iburst

# Allow the system clock to be stepped in the first three updates.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值