- 红帽企业Linux系统角色
- 安装RHEL系统角色
- 访问RHEL系统角色的文档
- 时间同步角色示例
- SELinux角色实列
- SELinux角色
自RHEL7.4开始,操作系统随附了多个Ansible角色,作为rhel-system-roles软件包的一部分。在RHEL8中,该软件包可以从AppStream中获取。以下是每个角色的简要描述:
#查看是否有rhel-system-roles包 [root@control ~]# dnf list all | grep rhel-system-roles rhel-system-roles.noarch 1.7.3-2.el8 AppStream [root@control ~]#
RHEL系统角色
| 名称 | 状态 | 角色描述 |
|---|---|---|
| rhel-system-roles.kdump | 全面支持 | 配置kdump崩溃恢复服务 |
| rhel-system-roles.network | 全面支持 | 配置网络接口 |
| rhel-system-roles.selinux | 全面支持 | 配置和管理SELinux自定义, 包括SELinux模式、文件和端口上下文、 布尔值设置以及SELinux用户 |
| rhel-system-roles.timesync | 全面支持 | 使用网络时间协议或精确时间协议配置时间同步 |
| rhel-system-roles.postfix | 技术预览 | 使用Postfix服务将每个主机配置为邮件传输代理 |
| rhel-system-roles.firewall | 开发中 | 配置主机的防火墙 |
| rhel-system-roles.tuned | 开发中 | 配置tuned服务,以调优系统性能 |
系统角色的目的是在多个版本之间标准化红帽企业Linux子系统的配置。使用系统角色来配置版本6.10及以上的任何红帽企业Linux主机。
安装RHEL系统角色RHEL系统角色由rhel-system-roles软件包提供,该软件包可从AppStream流获取。在Ansible控制节点上安装该软件包。
安装RHEL系统角色
[root@control ~]# dnf -y install rhel-system-roles
安装后,RHEL系统角色位于/usr/share/ansible/roles目录中:
[root@control ~]# ls /usr/share/ansible/roles/
红帽企业Linux中的默认roles_path在路径中包含/usr/share/ansible/roles,因此在playbook引用这些角色时Ansible可以很轻松的找到它们。
如果在当前Ansible配置文件中覆盖了roles_path,设置了环境变量ANSIBLE_ROLES_PATH,或者roles_path中更早列出的目录下存在另一个同名的角色,则Ansible可能无法找到系统角色。
访问RHEL系统角色的文档安装后,RHEL系统角色的文档位于/usr/share/doc/rhel-system-roles-/目录中。文档按照子系统整理到子目录中:
[root@control ~]# ll /usr/share/doc/rhel-system-roles/ total 12 drwxr-xr-x. 2 root root 4096 Jun 14 20:38 certificate drwxr-xr-x. 3 root root 55 Jun 14 20:38 collection drwxr-xr-x. 2 root root 42 Jun 14 20:38 crypto_policies drwxr-xr-x. 2 root root 162 Jun 14 20:38 ha_cluster drwxr-xr-x. 2 root root 42 Jun 14 20:38 kdump drwxr-xr-x. 2 root root 42 Jun 14 20:38 kernel_settings drwxr-xr-x. 2 root root 42 Jun 14 20:38 logging drwxr-xr-x. 2 root root 4096 Jun 14 20:38 metrics drwxr-xr-x. 2 root root 131 Jun 14 20:38 nbde_client drwxr-xr-x. 2 root root 227 Jun 14 20:38 nbde_server drwxr-xr-x. 2 root root 4096 Jun 14 20:38 network drwxr-xr-x. 2 root root 42 Jun 14 20:38 postfix drwxr-xr-x. 2 root root 78 Jun 14 20:38 selinux drwxr-xr-x. 2 root root 77 Jun 14 20:38 ssh drwxr-xr-x. 2 root root 120 Jun 14 20:38 sshd drwxr-xr-x. 2 root root 42 Jun 14 20:38 storage drwxr-xr-x. 2 root root 131 Jun 14 20:38 timesync drwxr-xr-x. 2 root root 42 Jun 14 20:38 tlog drwxr-xr-x. 2 root root 123 Jun 14 20:38 vpn时间同步角色示例
#创建data目录
[root@control ~]# cd /opt/
[root@control opt]# mkdir data
[root@control opt]# ls
data
#把系统中的角色复制到data目录
[root@control data]# cp -r /usr/share/ansible/roles/rhel-system-roles.timesync timesyuc
[root@control data]# ls
timesyuc
[root@control data]# vim main.yml
---
- hosts: web.example.com
vars:
timesyuc_ntp_servers:
- hostname: time1.aliyun.com #阿里云时间同步服务器
pool: yes
iburst: yes
timezone: Asia/Shanghai
roles:
- timesync #此处为date下的timeync
tasks:
- name: set timezone
timezone:
name: "{{ timezone }}"
[root@control data]# mkdir roles
[root@control data]# mv timesyuc roles/
[root@control data]# ls
main.yml roles
#把清单文件复制到data目录下面
[root@control data]# cp /opt/playbook/httpd/ansible.cfg .
[root@control data]# cp /opt/playbook/httpd/inventory .
[root@control data]# ls
ansible.cfg inventory main.yml roles
[root@control data]#
# ping是否能通信
[root@control data]# ansible web.example.com -m ping
web.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@control data]#
data目录的最终效果
[root@control ~]# tree /opt/data
/opt/data
├── ansible.cfg #配置文件
├── inventory #清单文件
├── main.yml #有主playbook
└── roles
└── timesyuc
├── 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.yml
11 directories, 49 files
[root@control ~]#
受控主机操作
#在受控主机安装chrony [root@localhost ~]# dnf -y install chrony #设置开机自启 [root@localhost ~]# systemctl enable --now chronyd #查看时间同步 [root@localhost ~]# vim /etc/chrony.conf ....省略 # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift ....省略
#运行 [root@control data]# ansible-playbook main.yml #受控主机查看是否已更改时间同步 [root@localhost ~]# vim /etc/chrony.conf # Ansible managed pool 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 # Save NTS keys and cookies. ntsdumpdir /var/lib/chrony
例如一个playbook项目具有以下结构:
[root@localhost playbook-project]# tree . ├── ansible.cfg ├── group_vars │ └── servers │ └── timesync.yml ├── inventory └── timesync_playbook.yml
timesync.yml定义时间同步变量,覆盖清单中servers组内主机的角色默认值。此文件看起来类似于:
timesync_ntp_servers:
- hostname: 0.rhel.pool.ntp.org
iburst: yes
- hostname: 1.rhel.pool.ntp.org
iburst: yes
- hostname: 2.rhel.pool.ntp.org
iburst: yes
timezone: UTC
timesync_playbook.yml的内容简化为:
- name: Time Synchronization Play
hosts: servers
roles:
- rhel-system-roles.timesync
tasks:
- name: Set timezone
timezone:
name: "{{ timezone }}"
SELinux角色实列
#创建目录用于存放后续文件
[root@control ~]# cd /usr/src/
[root@control src]# mkdir selinux_play
[root@control src]# ls
debug kernels selinux_play
#复制selinux的角色目录到当前目录并改名为selinux
[root@control src]# cd selinux_play/
[root@control selinux_play]# cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/ selinux
[root@control selinux_play]# ls
selinux
#编写playbook
[root@localhost selinux_play]# cat main.yml
---
- hosts: web.example.com
vars:
selinux_policy: targeted
selinux_state: enforcing
roles:
- selinux
查看受控主机selinux状态
[root@localhost ~]# setenforce 0 [root@localhost ~]# getenforce 0 //修改为Permissive Permissive
把清单和ansible.cfg文件 复制到 selinux_play
[root@control selinux_play]# cp /opt/playbook/httpd/ansible.cfg .
[root@control selinux_play]# cp /opt/playbook/httpd/inventory .
[root@control selinux_play]# ls
ansible.cfg inventory main.yml selinux
[root@control selinux_play]# ansible all -m ping
web.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
执行palybook
[root@localhost selinux_play]# ansible-playbook main.yml
查看受控主机
[root@localhost ~]# getenforce //状态变为了enforcing EnforcingSELinux角色
selinux_play目录最终效果
[root@control ~]# tree /usr/src/selinux_play/
/usr/src/selinux_play/
├── ansible.cfg
├── inventory
├── main.yml
└── selinux
├── ansible_pytest_extra_requirements.txt
├── COPYING
├── custom_requirements.txt
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── library
│ └── selinux_modules_facts.py
├── meta
│ └── main.yml
├── molecule_extra_requirements.txt
├── pylint_extra_requirements.txt
├── pytest_extra_requirements.txt
├── README.html
├── README.md
├── tasks
│ ├── main.yml
│ └── selinux_load_module.yml
├── tests
│ ├── linux-system-roles-selinux-test-a.pp
│ ├── linux-system-roles-selinux-test-b.pp
│ ├── linux-system-roles-selinux-test-c.pp
│ ├── roles
│ ├── selinux_apply_reboot.yml
│ ├── selinux.config
│ ├── selinux_config_restore.yml
│ ├── selinux_config_save.yml
│ ├── selinux_test_transitions.yml
│ ├── set_selinux_variables.yml
│ ├── tests_all_purge.yml
│ ├── tests_all_transitions.yml
│ ├── tests_boolean.yml
│ ├── tests_default_vars.yml
│ ├── tests_default.yml
│ ├── tests_fcontext.yml
│ ├── tests_login.yml
│ ├── tests_port.yml
│ ├── tests_selinux_disabled.yml
│ └── tests_selinux_modules.yml
├── tox.ini
└── vars
└── main.yml
9 directories, 38 files
[root@control ~]#



