当前位置: 首页 > news >正文

自动化运维,ansible综合测试练习题

自动化运维-ansible综合测试练习题

环境说明:

  • 预设 root 口令为 redhat

  • 提供 RHEL9 软件源 http://ansible.example.com/rhel9/BaseOS

  • 提供 RHEL9 软件源 http://ansible.example.com/rhel9/AppStream

  • 提供 RHEL9 软件源 http://ansible.example.com/ansible-automation-platform

  • 提供 DNS 服务,为区域 example.com 中相关站点提供解析

  • 提供 NTP 网络时间服务

虚拟机 master(RHEL9) —— 控制机:

  • master.example.com 192.168.122.100/24

  • 可从 ansible.example.com 免密 SSH 登录到 root 用户,已预设授权 sudo 用户 student

  • student 账户的密码为 redhat

  • 预设 IP 地址 192.168.122.100/24,已做好主机名映射

虚拟机 node1~node5(RHEL9) —— 受管机:

  • node[1-5].example.com 192.168.122.[10-50]/24

  • 预设授权 sudo 用户 student,可从控制机 master 免密 SSH 登录

  • student 账户的密码为 redhat

  • 预设 IP 地址 192.168.122.[10-50]/24,已做好主机名映射

其他信息:

  • 除非另有指定,否则所有工作都需要归属在控制机的/home/student/ansible/目录下

准备环境:

  • 使用root用户给student用户启用逗留

    [root@master ~]# loginctl enable-linger student

一、安装和配置 ansible 环境

  1. 安装所需软件包
  2. 在/home/student/ansible/inventory 文件中设置主机清单,要求:
    • node1 属于 test01 主机组
    • node2 属于 test02 主机组
    • node3 和 node4 属于 web 主机组
    • node5 属于 test05 主机组
    • web 组属于 webtest 主机组
  3. 在/home/student/ansible 目录中创建 ansible.cfg,满足以下需求:
    • 主机清单文件为/home/student/ansible/inventory
    • playbook 中角色位置为/home/student/ansible/roles
    • collection 位置为/home/student/ansible/collections

编辑主机清单

创建需要的目录

[student@master ~]$ mkdir ansible
[student@master ~]$ cd ansible/
[student@master ansible]$ vim inventory		# 编辑主机清单
[student@master ansible]$ ansible-config init --disabled > ansible.cfg
[student@master ansible]$ mkdir roles
[student@master ansible]$ mkdir collections

对配置文件进行编辑

[student@master ansible]$ vim ansible.cfg
# 编辑配置文件
inventory=/home/student/ansible/inventory
remote_user=student
roles_path=/home/student/ansible/roles
collections_path=/home/student/ansible/collections
host_key_checking=False
# privilege
become=True
become_ask_pass=False
become_method=sudo
become_user=root

测试

[student@master ansible]$ ansible all -m ping

在这里插入图片描述

二、创建和运行 Ansible 任务

编写脚本/home/student/ansible/yum.yml,为所有受管机配置 yum 仓库。

  1. 仓库 1:
    • 名称为 BASEOS,描述为 software base
    • URL 为 http://ansible.example.com/rhel9/BaseOS
    • GPG 签名启用
    • GPG 密钥 URL 为 http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
    • 仓库为启用状态
  2. 仓库 2:
    • 名称为 APPSTREAM,描述为 software stream
    • URL 为 http://ansible.example.com/rhel9/AppStream
    • GPG 签名启用
    • GPG 密钥 URL 为 http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
    • 仓库为启用状态

安装yum仓库

[student@master ansible]$ vim yum.yml
# playbook
---
- name: repohosts: alltasks:- name: repo1yum_repository:name: BASEOSdescription: software basebaseurl: http://ansible.example.com/rhel9/BaseOSenabled: yesgpgcheck: yesgpgkey: http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release- name: repo2yum_repository:name: APPSTREAMdescription: software stream baseurl: http://ansible.example.com/rhel9/AppStreamenabled: yesgpgcheck: yesgpgkey: http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release

在这里插入图片描述

三、编写剧本远程安装软件

创建名为/home/student/ansible/tools.yml 的 playbook,能够实现以下目的:

  1. 将 php 和 tftp 软件包安装到 test01、test02 和 web 主机组中的主机上
  2. 将 RPM Development Tools 软件包组安装到 test01 主机组中的主机上
  3. 将 test01 主机组中的主机上所有软件包升级到最新版本

安装软件

[student@master ansible]$ vim tools.yml
# playbook
---
- name: install pkghosts: test01,test02,webtasks:- name: install pgksyum:name:- php- tftpstate: present- name: install pkg grouphosts: test01tasks:- name: isntall group pkgyum:name: "@RPM Development Tools"state: present- name: updatayum:name: '*'state: latest

在这里插入图片描述

四、配置计划任务

编写剧本/home/student/ansible/jihua.yml

  1. 在 test02 组中的被管理主机运行
  2. 为用户 student 创建计划任务: student 用户每隔 5 分钟执行 echo “hello tarena”

计划任务

[student@master ansible]$ vim jihua.yml
# playbook
---
- name: cronhosts: test02tasks:- name: cron1cron:name: jihua1user: studentminute: '*/5'job: echo "hello tarena"

在这里插入图片描述

五、安装并使用系统角色(timesync)

安装 RHEL 角色软件包,并创建剧本/home/student/ansible/timesync.yml,满足以下要求:

  1. 在 test01 组中的被管理主机运行
  2. 使用 timesync 角色
  3. 配置该角色,使用时间服务器 ansible.example.com,并启用 iburst 参数

安装系统角色

复制角色

[student@master ansible]$ sudo yum -y install rhel-system-roles
[student@master ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync -p roles/timesync

使用角色

[student@master ansible]$ vim timesync.yml
# playbook
---
- name: chronyhosts: test01vars:timesync_ntp_servers:- hostname: ansible.example.comiburst: yesroles:- timesync

在这里插入图片描述

六、通过 galaxy 安装角色与 collection

  1. 创建剧本/home/student/ansible/roles/down.yml,用来从以下 URL 下载角色,

    安装到/home/student/ansible/roles 目录下:

    http://ansible.example.com/roles/haproxy.tar 此角色名为 haproxy

    http://ansible.example.com/roles/myphp.tar 此角色名为 myphp

  2. 从 http://ansible.example.com/materials/下载如下 collection 并安装到

    /home/student/ansible/collections 目录下:
    ansible-posix-1.5.1.tar.gz
    community-general-6.3.0.tar.gz

编辑,安装haproxy、myphp

[student@master ansible]$ vim roles/down.yml
# tasks
---
- name: haproxysrc: http://ansible.example.com/roles/haproxy.tar- name: myphpsrc: http://ansible.example.com/roles/myphp.tar

在这里插入图片描述

安装集合

[student@master ansible]$ ansible-galaxy collection install http://ansible.example.com/materials/ansible-posix-1.5.1.tar.gz -p collections/
[student@master ansible]$ ansible-galaxy collection install http://ansible.example.com/materials/community-general-6.3.0.tar.gz -p collections/

七、创建及使用自定义角色

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

  1. 安装 httpd 软件,并能够开机自动运行
  2. 开启防火墙,并允许 httpd 通过
  3. 使用模板 index.html.j2,用来创建/var/www/html/index.html 网页,
    内容如下(HOSTNAME 是受管理节点的完全域名,IPADDRESS 是 IP 地址):
    Welcome to HOSTNAME on IPADDRESS
  4. 然后创建剧本 /home/student/ansible/myrole.yml,为 webtest 主机组启用 httpd 角色

创建角色

[student@master roles]$ ansible-galaxy init httpd

创建模板

[student@master httpd]$ vim templates/index.html.j2
# 模板
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}

编辑任务

[student@master httpd]$ vim tasks/main.yml
# tasks
---
# tasks file for httpd
- name: install pkgyum:name:- httpd- firewalldstate: present- name: cp htmltemplate:src: index.html.j2dest: /var/www/html/index.html- name: restarted serviceservice:name: "{{ item }}"state: restartedenabled: yesloop:- httpd- firewalld- name: set firewalldfirewalld:service: httpstate: enabledpermanent: yesimmediate: yes

编辑playbook

[student@master ansible]$ vim myrole.yml
# playbook
---
- name: user httpdhosts: webtestroles:- httpd

在这里插入图片描述

八、使用之前通过 galaxy 下载的角色

创建剧本/home/student/ansible/web.yml,满足下列需求:

  1. 该剧本中包含一个 play,可以在 test05 主机组运行 haproxy 角色
    (此角色已经配置好网站的负载均衡服务)
  2. 多次访问 http://node5.example.com 可以输出不同主机的欢迎页面
  3. 该剧本中包含另一个 play,可以在 webtest 主机组运行 myphp 角色
    (此角色已经配置好网站的 php 页面)
  4. 多次访问 http://node5.example.com/index.php 也输出不同主机的欢迎页面

创建playbook,使用角色

[student@master ansible]$ vim web.yml
# playbook
---
- name: get facthosts: webtest
- name: user haproxyhosts: test05roles:- haproxy- name: user myphphosts: webtestroles:- myphp

在这里插入图片描述

九、编写剧本远程管理逻辑卷

创建剧本 /home/student/ansible/lvm.yml,用来为所有受管机完成以下部署:

  1. 在卷组 search 中创建名为 mylv 的逻辑卷,大小为 1000MiB
  2. 使用 ext4 文件系统格式化该逻辑卷
  3. 如果无法创建要求的大小,应显示错误信息 insufficient free space,并改为 500MiB
  4. 如果卷组 search 不存在,应显示错误信息 VG not found
  5. 不需要挂载逻辑卷

创建playbook,使用block/rescue/always

[student@master ansible]$ vim lvm.yml
# playbook
---
- name: create lvhosts: alltasks:- name: create lv1block:- name: create lv1000lvol:vg: searchlv: mylvsize: 1000rescue:- name: output1debug:msg: insufficient free space- name: create lv500lvol:vg: searchlv: mylvsize: 500always:- name: fs mylvfilesystem:dev: /dev/search/mylvfstype: ext4when: "'search' in ansible_lvm.vgs"- name: output2debug:msg: VG not foundwhen: "'search' not in ansible_lvm.vgs"

在这里插入图片描述

十、根据模板部署主机文件

  1. 从 http://ansible.example.com/materials/newhosts.j2 下载模板文件
  2. 完成该模板文件,用来生成新主机清单(主机的显示顺序没有要求),结构如下:
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.122.10 node1.example.com node1
    192.168.122.20 node2.example.com node2
    192.168.122.30 node3.example.com node3
    192.168.122.40 node4.example.com node4
    192.168.122.50 node5.example.com node5
  3. 创建剧本/home/student/ansible/newhosts.yml,它将使用上述模板在 test01 主机组的主机上生成文件/etc/newhosts

下载模板

[student@master ansible]$ curl -O http://ansible.example.com/materials/newhosts.j2

编辑模板,使用 for 循环

[student@master ansible]$ vim newhosts.j2
# 模板内容
{% for i in groups.all %}
{{ hostvars[i].ansible_default_ipv4.address }} {{ hostvars[i].ansible_fqdn }} {{ hostvars[i].ansible_hostname }}
{% endfor %}

创建playbook

[student@master ansible]$ vim newhosts.yml
# playbook
---
- name: get facthosts: all
- name: cp filehosts: test01tasks:- name: cp newhoststemplate:src: /home/student/ansible/newhosts.j2dest: /etc/newhosts

在这里插入图片描述

十一、编写剧本修改远程文件内容

创建剧本 /home/student/ansible/newissue.yml,满足下列要求:

  1. 在所有清单主机上运行,替换/etc/issue 的内容
  2. 对于 test01 主机组中的主机,/etc/issue 文件内容为 test01
  3. 对于 test02 主机组中的主机,/etc/issue 文件内容为 test02
  4. 对于 web 主机组中的主机,/etc/issue 文件内容为 Webserver

创建playbook,使用 if 判断

[student@master ansible]$ vim newissue.yml
# playbook
---
- name: replace filehosts: alltasks:- name: replace file1copy:content: |{% if 'test01' in group_names %}test01{% elif 'test02' in group_names %}test02{% elif 'web' in group_names %}Webserver{% endif %}dest: /etc/issue

在这里插入图片描述

十二、编写剧本部署远程 Web 目录

创建剧本/home/student/ansible/webdev.yml,满足下列要求:

  1. 在 test01 主机组运行
  2. 创建目录/webdev,属于 webdev 组,权限为 rwxrwxr-x,具有 SetGID 特殊权限
  3. 使用符号链接/var/www/html/webdev 链接到/webdev 目录
  4. 创建文件/webdev/index.html,内容是 It’s works!
  5. 查看 test01 主机组的 web 页面 http://node1/webdev/将显示 It’s works!

创建playbook

下载服务,创建组,创建目录,创建文件,创建软连接,启用服务,设置防火墙

[student@master ansible]$ vim webdev.yml
# playbook
---
- name: webhosts: test01tasks:- name: install pkgyum:name:- httpd- firewalldstate: present- name: create groupgroup:name: webdevstate: present- name: create directoryfile:path: /webdevgroup: webdevmode: 2775state: directorysetype: httpd_sys_content_t- name: create filecopy:content: "It's works!\n"dest: /webdev/index.htmlsetype: httpd_sys_content_t- name: creste linkfile:src: /webdevdest: /var/www/html/webdevstate: link- name: restarted serviceservice:name: "{{ item }}"state: restartedenabled: yesloop:- httpd- firewalld- name: set firewalldfirewalld:service: httpstate: enabledpermanent: yesimmediate: yes

在这里插入图片描述

十三、编写剧本为受管机生成硬件报告

创建名为/home/student/ansible/hardware.yml 的 playbook,满足下列要求:

  1. 使所有受管理节点从以下 URL 下载文件:
    http://ansible.example.com/materials/hardware.empty
  2. 并用来生成以下硬件报告信息,存储在各自的/root/hardware.txt 文件中
    清单主机名称 inventory_hostname
    以 MB 表示的总内存大小 ansible_memtotal_mb
    BIOS 版本 ansible_bios_version
    硬盘 vda 的大小 ansible_devices.vda.size
    硬盘 vdb 的大小 ansible_devices.vdb.size
    如果这些硬件信息不存在的话,则改成NONE字符串

下载文件(可以不用下载,这里下载是为了方便查看文件内容,后续好进行替换)

[student@master ansible]$ curl -O http://ansible.example.com/materials/hardware.empty

创建playbook,下载文件,并对文件内容进行替换

[student@master ansible]$ vim hardware.yml
# playbook
---
- name: hardwarehosts: alltasks:- name: get fileget_url:url: http://ansible.example.com/materials/hardware.emptydest: /root/hardware.txt- name: hostnamereplace:path: /root/hardware.txtregexp: inventoryhostnamereplace: "{{ inventory_hostname }}"- name: memreplace:path: /root/hardware.txtregexp: memory_in_MBreplace: "{{ ansible_memtotal_mb }}"- name: biosreplace:path: /root/hardware.txtregexp: BIOS_versionreplace: "{{ ansible_bios_version }}"- name: vdareplace:path: /root/hardware.txtregexp: disk_vda_sizereplace: "{{ ansible_devices.vda.size if ansible_devices.vda is defined else 'NONE' }}"- name: vdbreplace:path: /root/hardware.txtregexp: disk_vdb_sizereplace: "{{ ansible_devices.vdb.size if ansible_devices.vdb is defined else 'NONE' }}"

在这里插入图片描述

十四、创建保险库文件

创建 ansible 保险库 /home/student/ansible/passdb.yml,其中有 2 个变量:

  1. pw_dev,值为 ab1234
  2. pw_man,值为 cd5678
  3. 加密和解密该库的密码是 pwd@1234,密码存在/home/student/ansible/secret.txt 中

创建 .yml 文件,设置变量

[student@master ansible]$ vim passdb.yml
# 变量
---
pw_dev: ab1234
pw_man: cd5678

创建密码本

[student@master ansible]$ vim secret.txt
# 密码
pwd@1234

使用密码本对存放变量的文件进行加密

[student@master ansible]$ ansible-vault encrypt passdb.yml --vault-id secret.txt

在这里插入图片描述

十五、编写剧本为受管机批量创建用户

从以下 URL 下载用户列表,保存到/home/student/ansible 目录下:
http://ansible.example.com/materials/name_list.yml
创建剧本/home/student/ansible/users.yml 的 playbook,满足下列要求:

  1. 使用之前题目中的 passdb.yml 保险库文件提供的密码做用户密码
  2. 职位描述为 dev 的用户应在 test01、test02 主机组的受管机上创建,
    使用 pw_dev 变量分配密码,是补充组 devops 的成员
  3. 职位描述为 man 的用户应在 web 主机组的受管机上创建,
    使用 pw_man 变量分配密码,是补充组 opsmgr 的成员
  4. 密码应采用 SHA512 哈希格式,这几个用户的密码最大有效时间为30天
  5. 该 playbook 可以使用之前题目创建的 secret.txt 密码文件运行

下载文件

[student@master ansible]$ curl -O http://ansible.example.com/materials/name_list.yml

创建playbook

[student@master ansible]$ vim users.yml
# playbook
---
- name: create user for devhosts: test01,test02vars_files:- /home/student/ansible/passdb.yml- /home/student/ansible/name_list.ymltasks:- name: create group1group:name: devopsstate: present- name: create user1user:name: "{{ item.name }}"groups: devopspassword: "{{ pw_dev | password_hash('sha512') }}"state: presentpassword_expire_max: 30loop: "{{ users }}"when: item.job == 'dev'- name: create user for manhosts: webvars_files:- /home/student/ansible/passdb.yml- /home/student/ansible/name_list.ymltasks:- name: create group2group:name: opsmgrstate: present- name: create user2user:name: "{{ item.name }}"groups: opsmgrpassword: "{{ pw_man | password_hash('sha512') }}"state: presentpassword_expire_max: 30loop: "{{ users }}"when: item.job == 'man'

在这里插入图片描述

十六、重设保险库密码

从以下 URL 下载保险库文件到/home/student/ansible 目录:
http://ansible.example.com/materials/topsec.yml
当前的库密码是 banana,新密码是 big_banana,请更新该库密码

下载文件

[student@master ansible]$ curl -O http://ansible.example.com/materials/topsec.yml

更改密码

[student@master ansible]$ ansible-vault rekey topsec.yml 
Vault password: 
New Vault password: 
Confirm New Vault password: 

在这里插入图片描述

http://www.xdnf.cn/news/1477261.html

相关文章:

  • Ceph分布式存储全解析:从了解到使用
  • 新能源研发,用新型实验记录本:ELN
  • 006-Dephi 表达式 选择语句 循环语句其他语句
  • k8s网络原理
  • Qt自定义列表项与QListWidget学习
  • PID控制技术深度剖析:从基础原理到高级应用(六)
  • LeetCode 刷题【66. 加一、67. 二进制求和】
  • Linux bzip2 命令使用说明
  • 大数据毕业设计选题推荐-基于大数据的宫颈癌风险因素分析与可视化系统-Spark-Hadoop-Bigdata
  • Day22_【机器学习—集成学习(2)—Bagging—随机森林算法】
  • 学习nginx location ~ .*.(js|css)?$语法规则
  • Error metrics for skewed datasets|倾斜数据集的误差指标
  • 区块链论坛社区
  • 在 ES6 中如何提取深度嵌套的对象中的指定属性
  • 【111】基于51单片机停车场车位管理系统【Proteus仿真+Keil程序+报告+原理图】
  • 从RAW到BMP:工业视觉系统图像格式的转换与优化
  • 数据结构之二叉树(1)
  • STM32-----SPI
  • JUC、JVM八股补充
  • YOLOv8 在 Intel Mac 上的 Anaconda 一键安装教程
  • JBoltAI:赋能AI数智化升级的Java级引擎——深入解析企业级AI开发框架的核心能力与行业价值
  • 待定系数法分解分式
  • 后端(JDBC)学习笔记(CLASS 1):基础篇(一)
  • VBA之Excel应用第四章第七节:单元格区域的整行或整列扩展
  • 进阶向:密码生成与管理工具
  • 【PCIe EP 设备入门学习专栏 -- 8.1.3 PCIe EP AXI Bridge Module】
  • 发布vue项目、nginx配置及问题场景(history)
  • 服务器内存和普通计算机内存在技术方面有什么区别?
  • 前端入门——案例一:登录界面设计(html+css+js)
  • 【xss基本介绍】