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

发布于:2025-09-10 ⋅ 阅读:(25) ⋅ 点赞:(0)

自动化运维-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: repo
  hosts: all
  tasks:
    - name: repo1
      yum_repository:
        name: BASEOS
        description: software base
        baseurl: http://ansible.example.com/rhel9/BaseOS
        enabled: yes
        gpgcheck: yes
        gpgkey: http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
        
    - name: repo2
      yum_repository:
        name: APPSTREAM
        description: software stream 
        baseurl: http://ansible.example.com/rhel9/AppStream
        enabled: yes
        gpgcheck: yes
        gpgkey: 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 pkg
  hosts: test01,test02,web
  tasks:
    - name: install pgks
      yum:
        name:
          - php
          - tftp
        state: present
          
- name: install pkg group
  hosts: test01
  tasks:
    - name: isntall group pkg
      yum:
        name: "@RPM Development Tools"
        state: present
        
    - name: updata
      yum:
        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: cron
  hosts: test02
  tasks:
    - name: cron1
      cron:
        name: jihua1
        user: student
        minute: '*/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: chrony
  hosts: test01
  vars:
    timesync_ntp_servers:
      - hostname: ansible.example.com
        iburst: yes
  roles:
    - 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: haproxy
  src: http://ansible.example.com/roles/haproxy.tar

- name: myphp
  src: 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 pkg
  yum:
    name:
      - httpd
      - firewalld
    state: present

- name: cp html
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

- name: restarted service
  service:
    name: "{{ item }}"
    state: restarted
    enabled: yes
  loop:
    - httpd
    - firewalld

- name: set firewalld
  firewalld:
    service: http
    state: enabled
    permanent: yes
    immediate: yes

编辑playbook

[student@master ansible]$ vim myrole.yml
# playbook
---
- name: user httpd
  hosts: webtest
  roles:
    - 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 fact
  hosts: webtest
- name: user haproxy
  hosts: test05
  roles:
    - haproxy

- name: user myphp
  hosts: webtest
  roles:
    - 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 lv
  hosts: all
  tasks:
    - name: create lv1
      block:
        - name: create lv1000
          lvol:
            vg: search
            lv: mylv
            size: 1000
      rescue:
        - name: output1
          debug:
            msg: insufficient free space
        - name: create lv500
          lvol:
            vg: search
            lv: mylv
            size: 500
      always:
        - name: fs mylv
          filesystem:
            dev: /dev/search/mylv
            fstype: ext4
      when: "'search' in ansible_lvm.vgs"

    - name: output2
      debug:
        msg: VG not found
      when: "'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 fact
  hosts: all
- name: cp file
  hosts: test01
  tasks:
    - name: cp newhosts
      template:
        src: /home/student/ansible/newhosts.j2
        dest: /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 file
  hosts: all
  tasks:
    - name: replace file1
      copy:
        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: web
  hosts: test01
  tasks:
    - name: install pkg
      yum:
        name:
          - httpd
          - firewalld
        state: present

    - name: create group
      group:
        name: webdev
        state: present

    - name: create directory
      file:
        path: /webdev
        group: webdev
        mode: 2775
        state: directory
        setype: httpd_sys_content_t

    - name: create file
      copy:
        content: "It's works!\n"
        dest: /webdev/index.html
        setype: httpd_sys_content_t

    - name: creste link
      file:
        src: /webdev
        dest: /var/www/html/webdev
        state: link

    - name: restarted service
      service:
        name: "{{ item }}"
        state: restarted
        enabled: yes
      loop:
        - httpd
        - firewalld

    - name: set firewalld
      firewalld:
        service: http
        state: enabled
        permanent: yes
        immediate: 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: hardware
  hosts: all
  tasks:
    - name: get file
      get_url:
        url: http://ansible.example.com/materials/hardware.empty
        dest: /root/hardware.txt

    - name: hostname
      replace:
        path: /root/hardware.txt
        regexp: inventoryhostname
        replace: "{{ inventory_hostname }}"

    - name: mem
      replace:
        path: /root/hardware.txt
        regexp: memory_in_MB
        replace: "{{ ansible_memtotal_mb }}"

    - name: bios
      replace:
        path: /root/hardware.txt
        regexp: BIOS_version
        replace: "{{ ansible_bios_version }}"

    - name: vda
      replace:
        path: /root/hardware.txt
        regexp: disk_vda_size
        replace: "{{ ansible_devices.vda.size if ansible_devices.vda is defined else 'NONE' }}"

    - name: vdb
      replace:
        path: /root/hardware.txt
        regexp: disk_vdb_size
        replace: "{{ 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 dev
  hosts: test01,test02
  vars_files:
    - /home/student/ansible/passdb.yml
    - /home/student/ansible/name_list.yml
  tasks:
    - name: create group1
      group:
        name: devops
        state: present

    - name: create user1
      user:
        name: "{{ item.name }}"
        groups: devops
        password: "{{ pw_dev | password_hash('sha512') }}"
        state: present
        password_expire_max: 30
      loop: "{{ users }}"
      when: item.job == 'dev'

- name: create user for man
  hosts: web
  vars_files:
    - /home/student/ansible/passdb.yml
    - /home/student/ansible/name_list.yml
  tasks:
    - name: create group2
      group:
        name: opsmgr
        state: present

    - name: create user2
      user:
        name: "{{ item.name }}"
        groups: opsmgr
        password: "{{ pw_man | password_hash('sha512') }}"
        state: present
        password_expire_max: 30
      loop: "{{ 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: 

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到