接下来将开始分享ansible相关知识点,先给大家分享一下李哥已通过的红帽RHCE的相关试题。
RHCE 9认证都是考核ansible相关知识。考试中除了少量变换题型,大部分都是原题,可能会改一些名称之类的。
由于篇幅较长,先分享1-8题,9-16题将在下一篇文章分享!
如果你是小白,请关注我,跟着李哥慢慢学!后续将对ansible的所有知识点进行详细讲解
1 安装和配置Ansible
按照下方所述,在控制节点workstation.lab.example.com 上安装和配置 Ansible:
1、安装所需的软件包
2、创建名为/home/student/ansible/inventory的静态清单文件, 以满足以下需求:
servera是dev主机组的成员
serverb是test主机组的成员
serverc和serverd是prod主机组的成员
bastion是balancers主机组的成员
prod组是webservers主机组的成员
3、创建名为/home/student/ansible/ansible.cfg的配置文件, 以满足以下要求:
主机清单文件为/home/student/ansible/inventory
playbook中使用的角色的位置包括/home/student/ansible/roles
参考答案:
# yum -y install ansible #若考试已经安装好了,则不需要安装了
# su - student #考试要求所有的配置都用一个普通用户进行配置
$ mkdir ansible
$ cd ansible/
$ vim inventory
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
bastion
[webservers:children]
prod
$ cp /etc/ansible/ansible.cfg .
$ vim ansible.cfg
[defaults]
inventory = /home/student/ansible/inventory
remote_user = student
ask_pass = Flase
roles_path = /home/student/ansible/roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
2 创建和运行Ansible临时命令
请按照下方所述, 创建一个名为/home/student/ansible/adhoc.sh的shell脚本, 该脚本将使用Ansible临时命令在各个受管节点上安装yum存储库:
存储库1:
存> 储库的名称为 rh294_BASE 描述为 rh294 base software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
GPG签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release 存储库为开启状态
存储库2:
存储库的名称为 rh294_STREAM 描述为 rh294 stream software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/AppStream
签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存储库为开启状态
参考答案:
# su - student
$ cd ansible/
$ vim adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a "name=rh294_BASE description='rh294 base software' file=rhel_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhatrelease enabled=yes"
ansible all -m yum_repository -a "name=rh294_STREAM description='rh294 stream software' file=rhel_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPGKEY-redhat-release enabled=yes"
$ chmod a+x adhoc.sh
$ ./adhoc.sh
$ ansible all -a “cat /etc/yum.repo.d/rhel_dvd.repo”
3 安装软件包
创建一个名为 /home/student/ansible/packages.yml的 playbook:
1、将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
2、将 Development Tools 软件包组安装到 dev 主机组中的主机上
3、将 dev 主机组中主机上的所有软件包更新为最新版本
参考答案:
$ vim packages.yml
- name: install pkgs
hosts: dev, test, prod
tasks:
- name: install mariadb php
yum:
name:
- php
- mariadb
state: present
- name: install group pkgs and update
hosts: dev
tasks:
- name: install Development Tools
yum:
name: "@Development Tools"
state: present
- name: update pkgs
yum:
name: '*'
state: latest
$ ansible-playbook packages.yml
4 使用RHEL系统角色
安装 RHEL 系统角色软件包,并创建符合以下条件的playbook
/home/student/ansible/timesync.yml:
1、在所有受管节点上运行
2、使用 timesync 角色
3、配置该角色,以使用当前有效的 NTP 提供
4、配置该角色,以使用时间服务器 classroom.example.com
参考答案:
# yum search roles
# yum -y install rhel-system-roles
# su - student
$ cd ansible/
$ mkdir roles
$ cp -r /usr/share/ansible/roles/rhel-system- roles.timesync/ roles/timesync
$ cat roles/timesync/README.md
$ vim timesync.yml
- hosts: all
vars:
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yes
timesync_ntp_provider: chrony
roles:
- timesync
post_tasks:
- name: set timezone
timezone:
name: Asia/Shanghai
notify: restart crond
handlers:
- name: restart crond
service:
name: crond
state: restarted
$ ansible-playbook timesync.yml
5 使用Ansible Galaxy安装角色
使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/student/ansible/roles:
http://classroom.example.com/content/haproxy.tar.gz 此角色的名称应当为 balancer http://classroom.example.com/content/phpinfo.tar.gz 此角色的名称应当为 phpinfo
参考答案:
$ vim roles/requirements.yml
- name: balancer
src: http://classroom.example.com/content/haproxy.tar.gz
- name: phpinfo
src: http://classroom.example.com/content/phpinfo.tar.gz
$ ansible-galaxy install -r roles/requirements.yml -p roles/
6 创建和使用角色
根据下列要求,在 /home/student/ansible/roles中创建名为 apache 的角色:
1、httpd软件包已安装,设为在系统启动时启用
2、防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则
3、模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html:
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。
4、按照下方所述,创建一个使用此角色的 playbook /home/student/ansible/newrole.yml:
该 playbook 在 webservers 主机组中的主机上运行
参考答案:
$ cd roles/
$ ansible-galaxy init apache
$ vim apache/tasks/main.yml
---
# tasks file for apache
- name: install http
yum:
name: httpd
state: present
- name: config httpd
service:
name: httpd
state: started
enabled: yes
- name: config firewalld
service:
name: firewalld
state: started
enabled: yes
- name: firewalld service
firewalld:
zone: public
service: http
permanent: yes
immediate: yes
state: enabled
- name: user templates
template:
src: index.html.j2
dest: /var/www/html/index.html
$ vim apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
$ cd ..
$ vim newrole.yml
- name: use apache role
hosts: webservers
roles:
- apache
$ ansible-playbook newrole.yml
$ curl serverc
Welcome to serverc.lab.example.com on 172.25.250.12
$ curl serverd
Welcome to serverd.lab.example.com on 172.25.250.13
7 从Ansible Galaxy使用角色
根据下列要求,创建一个名为 /home/student/ansible/roles.yml 的 playbook:
1、playbook 中包含一个 play,该 play 在 balancers 主机组中的主机上运行并将使用 balancer 角 色。
此角色配置一项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。
浏览到 balancers 主机组中的主机(例如http://bastion.lab.example.com/ )将生成以下输出:
Welcome to serverc.example.com on 172.25.250.12 重新加载浏览器将从另一 Web 服务器生成输出:
Welcome to serverd.example.com on 172.25.250.13
2、playbook 中包含一个 play,该 play 在 webservers主机组中的主机上运行并将使用 phpinfo 角色。
通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
Hello PHP World from FQDN
其中,FQDN是主机的完全限定名称。
例如,浏览到 http://serverc.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverc.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
同样,浏览到 http://serverd.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverd.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
参考答案:
$ vim roles.yml
- name: config apache
hosts: webservers
roles:
- apache
- name: config php webserver
hosts: webservers
roles:
- phpinfo
- name: config balancer
hosts: balancers
roles:
- balancer
$ ansible-playbook roles.yml # 验证
$ curl http://bastion.lab.example.com/
Welcome to serverc.lab.example.com on 172.25.250.12
$ curl http://bastion.lab.example.com/
Welcome to serverd.lab.example.com on 172.25.250.13
$ curl http://serverc.lab.example.com/hello.php
Hello PHP World form serverc.lab.example.com
$ curl http://serverd.lab.example.com/hello.php
Hello PHP World form serverd.lab.example.com
8 创建和使用逻辑卷
创建一个名为/home/student/ansible/lv.yml 的playbook,它将在所有受管节点上运行以执行下列任务
1、创建符合以下要求的逻辑卷:
逻辑卷创建在 research 卷组中
逻辑卷名称为 data 逻辑卷大小为 1500MiB
2、使用 ext4 文件系统格式化逻辑卷
3、如果无法创建请求的逻辑卷大小,应显示错误消息
Could not create logical volume of that size,并且应改为使用大小 800MiB。4、 如果卷组research 不存在 ,应显示错误消息
Volume group does not exist。5. 不要以任何方式挂载逻辑卷。
参考答案:
$ vim lv.yml
- name: create lvm
hosts: all
tasks:
- name: create logical volume
block:
- name: create lvm 1500m
lvol:
vg: research
lv: data
size: 1500m
rescue:
- name: output fail msg
debug:
msg: Could not create logical volume of that size
- name: create lvm 800m
lvol:
vg: research
lv: data
size: 800m
always:
- name: format lvm
filesystem:
fstype: ext4
dev: /dev/research/data
when: "'research' in ansible_lvm.vgs"
- name: search not exists
debug:
msg: Volume group does not exist
when: "'research' not in ansible_lvm.vgs"
$ ansible-playbook lv.yml # 验证
$ ansible all -a “lvs”
如有帮助,请点个赞和“在看”!如有不足,敬请指出!感谢你的关注与支持。
路虽远,行则将至!
事虽难,做则必成!共勉!