14.使用Collection来管理Ansible资源
ansible-galaxy collection install ./gls-utils-0.0.1.tar.gz -p collections
ansible-galaxy collection list
vim bck.yml
---
- name: Backup the system configuration
hosts: servera.lab.example.com
become: true
gather_facts: false
tasks:
- name: Ensure the machine is up
gls.utils.newping:
data: pong
- name: Ensure configuration files are saved
include_role:
name: gls.utils.backup
vars:
backup_id: backup_etc
backup_files:
- /etc/sysconfig
- /etc/yum.repos.d
ansible-navigator run -m stdout bck.yml
15.使用system Role配置和管理特定的系统功能。
ansible-galaxy collection install redhat-rhel_system_roles-1.19.3.tar.gz -p collections
ansible-galaxy collection list
vim group_vars/all/timesync.yml
---
timesync_ntp_provider: chrony
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yes
vim group_vars/na_datacenter/timezone.yml
host_timezone: America/Chicago
vim group_vars/europe_datacenter/timezone.yml
host_timezone: Europe/Helsinki
vim configure_time.yml
---
- name: Time Synchronization
hosts: database_servers
roles:
- redhat.rhel_system_roles.timesync
post_tasks:
- name: Get time zone
ansible.builtin.command: timedatectl show
register: current_timezone
changed_when: false
- name: Set time zone
ansible.builtin.command: "timedatectl set-timezone {{ host_timezone }}"
when: host_timezone not in current_timezone.stdout
notify: reboot host
handlers:
- name: reboot host
ansible.builtin.reboot:
ansible-navigator run -m stdout configure_time.yml