Linux EX294-RHCE8考题『上篇』

简介: 在练习期间,除了您就坐位置的台式机之外,还将使用多个虚拟系统。您不具有台式机系统的 root 访问权,但具有对虚拟系统的完整 root 访问权。

重要配置信息


在练习期间,除了您就坐位置的台式机之外,还将使用多个虚拟系统。您不具有台式机系统的 root 访问权,但具有对虚拟系统的完整 root 访问权。


系统信息


在本练习期间,您将操作下列虚拟系统:

微信截图_20221013233139.png

这些系统的 IP 地址采用静态设置。请勿更改这些设置。


主机名称解析已配置为解析上方列出的完全限定主机名,同时也解析主机短名称。


帐户信息


所有系统的 root 密码是 flectrag。


请勿更改 root 密码。除非另有指定,否则这将是用于访问其他系统和服务的密码。此外,除非另有指定,否则此密码也应用于您创建的所有帐户,或者任何需要设置密码的服务。


为方便起见,所有系统上已预装了 SSH 密钥,允许在不输入密码的前提下通过 SSH 进行 root 访问。请勿对系统上的 root SSH 配置文件进行任何修改。


Ansible 控制节点上已创建了用户帐户 greg。此帐户预装了 SSH 密钥,允许在 Ansible 控制节点和各个 Ansible 受管节点之间进行 SSH 登录。请勿对系统上的 greg SSH 配置文件进行任何修改。您可以从 root 帐户使用 su 访问此用户帐户。


重要信息


除非另有指定,否则您的所有工作(包括 Ansible playbook、配置文件和主机清单等)应当保存在控制节点上的目录 /home/greg/ansible 中,并且应当归 greg 用户所有。所有 Ansible 相关的命令应当由 greg 用户从 Ansible 控制节点上的这个目录运行。


其他信息


一些练习项目可能需要修改 Ansible 主机清单。您要负责确保所有以前的清单组和项目保留下来,与任何其他更改共存。您还要有确保清单中所有默认的组和主机保留您进行的任何更改。


练习系统上的防火墙默认为不启用,SELinux 则处于强制模式。


如果需要安装其他软件,您的物理系统和 Ansible 控制节点可能已设置为指向 content 上的下述存储库:


  • http://content/rhel8.0/x86_64/dvd/BaseOS


  • http://content/rhel8.0/x86_64/dvd/AppStream


一些项目需要额外的文件,这些文件已在以下位置提供:


  • http://materials


产品文档可从以下位置找到:


  • http://materials/docs


其他资源也进行了配置,供您在练习期间使用。关于这些资源的具体信息将在需要这些资源的项目中提供。


重要信息


请注意,在评分之前,您的 Ansible 受管节点系统将重置为考试开始时的初始状态,您编写的 Ansible playbook 将通过以 greg 用户身份从控制节点上的目录 /home/greg/ansible 目录运行来应用。在 playbook 运行后,系统会对您的受管节点进行评估,以判断它们是否按照规定进行了配置。


考试要求


在您的系统上执行以下所有步骤。


1. 安装和配置 Ansible


安装和配置 Ansible

按照下方所述,在控制节点 control 上安装和配置 Ansible:

安装所需的软件包

创建名为 /home/greg/ansible/inventory 的静态清单文件,以满足以下要求:

node1 是 dev 主机组的成员

node2 是 test 主机组的成员

node3 和 node4 是 prod 主机组的成员

node5 是 balancers 主机组的成员

prod 组是 webservers 主机组的成员

创建名为 /home/greg/ansible/ansible.cfg 的配置文件,以满足以下要求:

主机清单文件为 /home/greg/ansible/inventory

playbook 中使用的角色的位置包括 /home/greg/ansible/roles


Hint - 提示


  • vars, children


  • Docs » User Guide » Working with Inventory


  • roles_path, 使用RHEL系统角色


  • host_key_checking, ssh ignore yes*5


$ ssh root@control
# su - greg
$ sudo yum -y install ansible
$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
$ mkdir -p /home/greg/ansible/roles
$ cd /home/greg/ansible
$ cp /etc/ansible/ansible.cfg .
$ vim ansible.cfg
[defaults]
#inventory     = /etc/ansible/hosts
inventory      = /home/greg/ansible/inventory
#host_key_checking = False
host_key_checking = False
#vault_password_file = /path/to/vault_password_file
#vault_password_file = /home/greg/ansible/secret.txt
#4.
#roles_path    = /etc/ansible/roles
#1.
#roles_path     = /home/greg/ansible/roles
roles_path    = /home/greg/ansible/roles:/usr/share/ansible/roles
...此处省略...
$ ansible --version
ansible 2.8.0
  config file = `/home/greg/ansible/ansible.cfg`
  ...
$ ansible-galaxy list
$ ansible-doc -l -t inventory
$ ansible-doc -t inventory ini
$ vim /home/greg/ansible/inventory
[dev]
node1 
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod 
$ ansible-inventory --graph
$ ansible all -a id
-sudo
$ ansible all -a id -b
-remote_user
$ ansible all -a id -u root -e ansible_password=flectrag

方法一:greg(ssh免密,sudo免密)

$ vim ansible.cfg
...
[privilege_escalation]
#become=True
become=True

方法二:root(不免密

$ vim /home/greg/ansible/inventory
...
[all:vars]
ansible_user=root
ansible_password=flectrag

方法三:root(不免密

$ vim ansible.cfg
...
[defaults]
#remote_user = root
remote_user = root
$ vim /home/greg/ansible/inventory
...
[all:vars]
ansible_password=flectrag
$ ansible all -a whoami
node2 | CHANGED | rc=0 >>
root
node4 | CHANGED | rc=0 >>
root
node5 | CHANGED | rc=0 >>
root
node3 | CHANGED | rc=0 >>
root
node1 | CHANGED | rc=0 >>
root

2. 创建和运行 Ansible 临时命令


创建和运行 Ansible 临时命令


作为系统管理员,您需要在受管节点上安装软件。


请按照正文所述,创建一个名为 /home/greg/ansible/adhoc.sh 的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库:


存储库1:

存储库的名称为 EX294_BASE

描述为 EX294 base software

基础 URL 为 http://content/rhel8.4/x86_64/dvd/BaseOS

GPG 签名检查为启用状态

GPG 密钥 URL 为 http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release

存储库为启用状态


存储库2:

存储库的名称为 EX294_STREAM

描述为 EX294 stream software

基础 URL 为 http://content/rhel8.4/x86_64/dvd/AppStream

GPG 签名检查为启用状态

GPG 密钥 URL 为 http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release

存储库为启用状态


Hint - 提示 Docs » User Guide » Introduction To Ad-Hoc Commands

$ ansible-inventory --graph
$ ansible-doc -l | grep yum
$ ansible-doc yum_repository
/EX
$ ansible-doc setup
$ vim /home/greg/ansible/adhoc.sh
#!/bin/bash
# 写法1
ansible all -m yum_repository -a 'name=EX294_BASE description="EX294 base software" baseurl=http://content/rhel8.4/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release'
# 写法2
ansible all -m yum_repository -a \
'name=EX294_STREAM \
description="EX294 stream software" \
baseurl=http://content/rhel8.4/x86_64/dvd/AppStream \
gpgcheck=yes \
gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release'
$ chmod +x /home/greg/ansible/adhoc.sh
$ ll /home/greg/ansible/adhoc.sh
$ /home/greg/ansible/adhoc.sh
$ ansible all -a 'yum -y remove dnf-utils'
$ ansible all -a 'yum -y install dnf-utils ftp'
$ ansible all -a 'rpm -q dnf-utils ftp'
...
node1 | CHANGED | rc=0 >>
dnf-utils-4.0.2.2-3.el8.noarch
ftp-0.17-78.el8.x86_64

3. 安装软件包


安装软件包


创建一个名为 /home/greg/ansible/packages.yml 的 playbook :


将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上

将 RPM Development Tools 软件包组安装到 dev 主机组中的主机上

将 dev 主机组中主机上的所有软件包更新为最新版本


Hint - 提示 Docs » User Guide » Working With Playbooks » Intro to Playbooks

$ ansible-doc -l | grep yum
$ ansible-doc yum
$ echo set nu ts=2 sw=2 et > ~/.vimrc
$ vim /home/greg/ansible/packages.yml
  • 方法一
---
- name: 安装软件包1
  hosts: dev,test,prod
  tasks:
  - name: ensure a list of packages installed
    yum:
      name: "{{ packages }}"
    vars:
      packages:
      - php
      - mariadb
- name: 安装软件包2
  hosts: dev
  tasks:
  - name: install the package group
    yum:
      name: "@RPM Development Tools"
      state: present
  - name: upgrade all packages
    yum:
      name: '*'
      state: latest
  • 方法二
---
- name: 安装软件包
  hosts: all 
  tasks:
  - name: install the latest version
    yum:
      name: "{{ item }}"
      state: latest
    loop:
    - php
    - mariadb
    when: inventory_hostname in groups.dev or inventory_hostname in groups.test or inventory_hostname in groups.prod
  - block:
    - name: install the 'Development tools' package group
      yum:
        name: "@RPM Development Tools"
        state: present
    - name: upgrade all packages
      yum:
        name: '*' 
        state: latest
    when: inventory_hostname in groups.dev
$ ansible-playbook packages.yml
$ ansible dev,test,prod -a 'rpm -q php mariadb'
$ ansible dev -a 'yum grouplist'
...
Installed Groups:
   RPM Development Tools
$ ansible dev -a 'yum update'
...
Nothing to do.

4.A 使用 RHEL 系统角色(NEW)


使用 RHEL 系统角色


安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/greg/ansible/selinux.yml :


在所有受管节点上运行

使用 selinux 角色

配置该角色,配置被管理节点的 selinux 为enforcing


Hint - 提示


  • 系统角色就是安装包


  • 注意系统角色路径


  • 系统角色要不看example,要不看README


$ yum search role
$ sudo yum -y install rhel-system-roles
$ rpm -ql rhel-system-roles
$ vim ansible.cfg
...
roles_path    = /home/greg/ansible/roles:/usr/share/ansible/roles
$ ansible-galaxy list
$ rpm -ql rhel-system-roles | grep example
$ cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml /home/greg/ansible/selinux.yml
$ vim /home/greg/ansible/selinux.yml
---
- hosts: all 
  vars:
    selinux_policy: targeted
    selinux_state: enforcing
  # prepare prerequisites which are used in this playbook
  tasks:
    - name: execute the role and catch errors
      block:
        - include_role:
            name: rhel-system-roles.selinux
      rescue:
        # Fail if failed for a different reason than selinux_reboot_required.
        - name: handle errors
          fail:
            msg: "role failed"
          when: not selinux_reboot_required
        - name: restart managed host
          shell: sleep 2 && shutdown -r now "Ansible updates triggered"
          async: 1
          poll: 0
          ignore_errors: true
        - name: wait for managed host to come back
          wait_for_connection:
            delay: 10
            timeout: 300 
        - name: reapply the role
          include_role:
            name: rhel-system-roles.selinux
$ ansible-playbook /home/greg/ansible/selinux.yml
$ ansible all -m shell -a 'grep ^SELINUX= /etc/selinux/config; getenforce'
node2 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node4 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node5 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node3 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node1 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing


4.B 使用 RHEL 系统角色(OLD)


使用 RHEL 系统角色


安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/greg/ansible/timesync.yml :


在所有受管节点上运行

使用 timesync 角色

配置该角色,以使用当前有效的 NTP 提供商

配置该角色,以使用时间服务器 172.25.254.254

配置该角色,以启用 iburst 参数


Hint - 提示


  • 系统角色就是安装包


  • 注意系统角色路径


  • 系统角色要不看example,要不看README


$ yum search role
$ sudo yum -y install rhel-system-roles
$ rpm -ql rhel-system-roles
$ vim ansible.cfg
...
roles_path    = /home/greg/ansible/roles:/usr/share/ansible/roles
$ ansible-galaxy list
$ rpm -ql rhel-system-roles | grep example
$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml /home/greg/ansible/timesync.yml
$ vim /home/greg/ansible/timesync.yml
---
- hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: 172.25.254.254
        iburst: yes
  roles:
    - rhel-system-roles.timesync
$ ansible-playbook /home/greg/ansible/timesync.yml
$ ansible all -m shell -a 'chronyc sources -v | grep classroom'
$ ansible all -m shell -a 'grep ^server /etc/chrony.conf'
$ ansible all -m shell -a 'timedatectl | grep -B 1 NTP'

5. 使用 Ansible Galaxy 安装角色


使用 Ansible Galaxy 安装角色


使用 Ansible Galaxy 和要求文件 /home/greg/ansible/roles/requirements.yml 。从以下 URL 下载角色并安装到 /home/greg/ansible/roles :


http://materials/haproxy.tar 此角色的名称应当为 balancer

http://materials/phpinfo.tar 此角色的名称应当为 phpinfo


Hint - 提示


  • requirements.yml


  • Docs » Galaxy User Guide /


  •  Installing roles from Galaxy /
       Installing multiple roles from a file
$ vim /home/greg/ansible/roles/requirements.yml
---
- src: http://materials/haproxy.tar
  name: balancer
- src: http://materials/phpinfo.tar
  name: phpinfo
$ ansible-galaxy install -r /home/greg/ansible/roles/requirements.yml
$ ansible-galaxy list
# /home/greg/ansible/roles
...
- balancer, (unknown version)
- phpinfo, (unknown version)
...

6. 创建和使用角色


创建和使用角色


根据下列要求,在 /home/greg/ansible/roles 中创建名为 apache 的角色:


httpd 软件包已安装,设为在系统启动时启用并启动


防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则


模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html :

Welcome to HOSTNAME on IPADDRESS


其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。


创建一个名为 /home/greg/ansible/apache.yml 的 playbook:


该 play 在 webservers 主机组中的主机上运行并将使用 apache 角色


$ cd roles/
$ ansible-galaxy init apache
$ cd ..
$ ansible-galaxy list
$ vim roles/apache/tasks/main.yml
---
- name: Start service httpd, if not started
  service:
    name: httpd
    state: started
    enabled: yes
- name: Start service firewalld, if not started
  service:
    name: firewalld
    state: started
    enabled: yes
- firewalld:
    service: http
    permanent: yes
    state: enabled
    immediate: yes
- name: Template a file
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
$ ansible node1 -m setup | grep node1
$ ansible node1 -m setup -a 'filter=*ip*'
$ vim roles/apache/templates/index.html.j2
Welcome to {{ ansible_nodename }} on {{ ansible_default_ipv4.address }}
$ vim /home/greg/ansible/apache.yml
---
- name: 创建和使用角色
  hosts: webservers
  roles:
  - apache
$ ansible-playbook /home/greg/ansible/apache.yml
$ ansible webservers --list-hosts
  hosts (2):
    node3
    node4
$ curl http://172.25.250.11
Welcome to node3.lab.example.com on 172.25.250.11
$ curl http://172.25.250.12
Welcome to node4.lab.example.com on 172.25.250.12

7. 从 Ansible Galaxy 使用角色


从 Ansible Galaxy 使用角色


根据下列要求,创建一个名为 /home/greg/ansible/roles.yml 的 playbook :


playbook 中包含一个 play, 该 play 在 balancers 主机组中的主机上运行并将使用 balancer 角色。


此角色配置一项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。


浏览到 balancers 主机组中的主机(例如 http://172.25.250.13 )将生成以下输出:

Welcome to node3.lab.example.com on 172.25.250.11


重新加载浏览器将从另一 Web 服务器生成输出:

Welcome to node4.lab.example.com on 172.25.250.12


playbook 中包含一个 play, 该 play 在 webservers 主机组中的主机上运行并将使用 phpinfo 角色。


请通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:

Hello PHP World from FQDN


其中,FQDN 是主机的完全限定名称。

Hello PHP World from node3.lab.example.com


另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。


同样,浏览到 http://172.25.250.12/hello.php 会生成以下输出:

Hello PHP World from node4.lab.example.com


另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。


Hint - 提示


  • roles 角色关键词直接查网页


Important - 重要


  • 第一个play, webservers;第二个play, balancers


$ vim /home/greg/ansible/roles.yml
---
- name: 从 Ansible Galaxy 使用角色 1
  hosts: webservers
  roles:
  - phpinfo
- name: 从 Ansible Galaxy 使用角色 2
  hosts: balancers
  roles:
  - balancer
$ ansible-playbook roles.yml
$ curl http://172.25.250.13
Welcome to node3.lab.example.com on 172.25.250.11
$ curl http://172.25.250.13
Welcome to node4.lab.example.com on 172.25.250.12

🦊firefox



目录
相关文章
|
存储 网络协议 应用服务中间件
Linux EX200-RHCSA考题『上篇』
在练习期间,除了您就坐位置的台式机之外,还将使用多个虚拟系统。您不具有台式机系统的根访问权,但具有对虚拟系统的完全根访问权。
1461 0
Linux EX200-RHCSA考题『上篇』
|
存储 Devops Linux
Linux EX294-RHCE8考题『下篇』
Linux EX294-RHCE8考题『下篇』
1323 0
|
存储 Linux 数据安全/隐私保护
Linux EX200-RHCSA考题『下篇』
在 node2.domain250.example.com 上执行以下任务(RH134)
568 0
|
Linux 开发工具
《Linux命令实践》-考题答案(Hands-on Labs T恤活动
体验Linux系列场景,通过实践测试,领取限量Hands-on Labs X Linux 联名T恤。(已于6月10日结束) T恤会在活动结束后15个工作日内陆续发出,领到奖品的同学会收到手机短信问卷。届时大家可以填写尺码,再次核对地址 附上体验考试地址:https://new-pre-developer.aliyun.com/adc/series/linux/
|
Linux 开发工具
《Linux命令实践》-考题答案(Hands-on Labs T恤活动)
体验Linux系列场景,通过实践测试,领取限量Hands-on Labs X Linux 联名T恤。(已于6月10日结束) T恤会在活动结束后15个工作日内陆续发出,领到奖品的同学会收到手机短信问卷。届时大家可以填写尺码,再次核对地址 附上体验考试地址:https://new-pre-developer.aliyun.com/adc/series/linux/
|
2天前
|
Linux
Linux常用命令包括
Linux常用命令包括
10 5
|
2天前
|
Linux
Linux命令
Linux命令
12 5
|
6天前
|
Linux Python Perl
Linux命令删除文件里的字符串
Linux命令删除文件里的字符串
18 7
|
6天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
8天前
|
Linux Perl
Linux之sed命令
Linux之sed命令
下一篇
无影云桌面