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
创建ansible.cfg并编辑
[student@master ansible]$ ansible-config init --disabled > ansible.cfg
[student@master ansible]$ vim ansible.cfg
inventory=/home/student/ansible/inventory #要修改的内容
host_key_checking=False
remote_user=student
roles_path=/home/student/ansible/roles
collections_path=/home/student/ansible/collections
become=true
become_ask_pass=False
become_method=sudo
become_user=root
设置主机清单
[student@master ansible]$ vim inventory
[test01]
node1
[test02]
node2
[web]
node3
node4
[test05]
node5
[webtest:children]
web
创建roles和collections目录
[student@master ansible]$ mkdir roles
[student@master ansible]$ mkdir collections
尝试ping所有主机查看是否成功
[student@master ansible]$ ansible all -m ping
二、创建和运行 Ansible 任务
编写脚本/home/student/ansible/yum.yml,为所有受管机配置 yum 仓库。
仓库 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:
名称为 APPSTREAM,描述为 software stream
URL 为 http://ansible.example.com/rhel9/AppStream
GPG 签名启用
GPG 密钥 URL 为 http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
仓库为启用状态
[student@master ansible]$ vim yum.yml
---
- name: yum
hosts: all
tasks:
- name: baseos
yum_repository:
name: BASEOS
description: software base
baseurl: http://ansible.example.com/rhel9/BaseOS
gpgcheck: yes
gpgkey: http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
- name: appstream
yum_repository:
name: APPSTREAM
description: software stream
baseurl: http://ansible.example.com/rhel9/AppStream
gpgcheck: yes
gpgkey: http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release
[student@master ansible]$ ansible-playbook yum.yml
三、编写剧本远程安装软件
创建名为/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
---
- name: tools
hosts: test01,test02,web
tasks:
- name: in p t
yum:
name:
- php
- tftp
state: present
- name: rpm
hosts: test01
tasks:
- name: rpm in
yum:
name: "@RPM Development Tools"
state: present
- name: up
yum:
name: "*"
state: latest
四、配置计划任务
编写剧本/home/student/ansible/jihua.yml
1)在 test02 组中的被管理主机运行
2)为用户 student 创建计划任务: student 用户每隔 5 分钟执行 echo “hello tarena”
[student@master ansible]$ vim jihua.yml
---
- name: jihua
hosts: test02
tasks:
- name: cron1
cron:
name: aa
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 install rhel-system-roles -y
将角色复制到/home/student/ansible/roles/下
[student@master roles]$ cp -r rhel-system-roles.timesync -p /home/student/ansible/roles/timesync
创建剧本
[student@master ansible]$ vim timesync.yml
---
- name: timesync
hosts: test01
vars:
timesync_ntp_servers:
- hostname: ansible.example.com
iburst: yes
roles:
- timesync
六、通过 galaxy 安装角色与 collection
创建剧本/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
创建剧本
[student@master ansible]$ vim roles/down.yml
---
- 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 install -r roles/down.yml -p roles/
从 http://ansible.example.com/materials/下载如下 collection 并安装到
/home/student/ansible/collections 目录下:
ansible-posix-1.5.1.tar.gz
community-general-6.3.0.tar.gz
安装集群
[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
然后创建剧本 /home/student/ansible/myrole.yml,为 webtest 主机组启用 httpd 角色
创建名为httpd的角色
[student@master roles]$ ansible-galaxy init httpd
修改模板
[student@master roles]$ cd 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 file for httpd
- name: httpd1
yum:
name:
- httpd
- firewalld
state: present
- name: cp1
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: restar
service:
name: "{{ item }}"
state: restarted
enabled: yes
loop:
- httpd
- firewalld
- name: fire
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
创建myrole.yml剧本
[student@master ansible]$ vim myrole.yml
---
- name: c1
hosts: webtest
roles:
- httpd
[student@master ansible]$ ansible-playbook myrole.yml
八、使用之前通过 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 也输出不同主机的欢迎页面
创建剧本
[student@master ansible]$ vim web.yml
---
- name: get
hosts: webtest
- name: t1
hosts: test05
roles:
- haproxp
- name: u1
hosts: webtest
roles:
- myphp
[student@master ansible]$ ansible-playbook web.yml
九、编写剧本远程管理逻辑卷
创建剧本 /home/student/ansible/lvm.yml,用来为所有受管机完成以下部署:
1.在卷组 search 中创建名为 mylv 的逻辑卷,大小为 1000MiB
2.使用 ext4 文件系统格式化该逻辑卷
3.如果无法创建要求的大小,应显示错误信息 insufficient free space,
并改为 500MiB
4.如果卷组 search 不存在,应显示错误信息 VG not found
5.不需要挂载逻辑卷
创建剧本
[student@master ansible]$ vim lvm.yml
---
- name: lv1
hosts: all
tasks:
- name: lv11
block:
- name: lv 1000
lvol:
vg: search
lv: mylv
size: 1000
rescue:
- name: o1
debug:
msg: insufficient free space
- name: lv 500
lvol:
vg: search
lv: mylv
size: 500
always:
- name: a1
filesystem:
dev: /dev/search/mylv
fstype: ext4
when: "'search' in ansible_lvm.vgs"
- name: o2
debug:
msg: VG not found
when: "'search' not in ansible_lvm.vgs"
十、根据模板部署主机文件
从 http://ansible.example.com/materials/newhosts.j2 下载模板文件
完成该模板文件,用来生成新主机清单(主机的显示顺序没有要求),结构如下:
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
创建剧本/home/student/ansible/newhosts.yml,它将使用上述模板在 test01 主机组的主机上
生成文件/etc/newhosts
1.下载模板文件
[student@master ansible]$ curl -O http://ansible.example.com/materials/newhosts.j2
2.修改模板文件
[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 %}
3.创建newhosts.yml剧本
[student@master ansible]$ vim newhosts.yml
- name: get
hosts: all #首先要获取全部主机的事实变量
- name: g1
hosts: test01
tasks:
- name: cp1
template:
src: /home/student/ansible/newhosts.j2
dest: /etc/newhosts
4.运行剧本
[student@master ansible]$ ansible-playbook newhosts.yml
5.查看
十一、编写剧本修改远程文件内容
创建剧本 /home/student/ansible/newissue.yml,满足下列要求:
1)在所有清单主机上运行,替换/etc/issue 的内容
2)对于 test01 主机组中的主机,/etc/issue 文件内容为 test01
3)对于 test02 主机组中的主机,/etc/issue 文件内容为 test02
4)对于 web 主机组中的主机,/etc/issue 文件内容为 Webserver
创建newissue.yml剧本
[student@master ansible]$ vim newissue.yml
---
- name: new1
hosts: all
tasks:
- name: aa
copy:
content: |
{% if 'test01' in group_names %}
test01
{% elif 'test02' in group_names %}
test02
{% elif 'web' in group_names %}
Webserver
{% endif %}
dest: /etc/issue
运行剧本
[student@master ansible]$ ansible-playbook newissue.yml
查看
十二、编写剧本部署远程 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!
创建剧本
[student@master ansible]$ vim webdev.yml
---
- name: webdev
hosts: test01
tasks:
- name: yum1
yum:
name:
- httpd
- firewalld
state: present
- name: group mk
group:
name: webdev
state: present
- name: d1
file:
path: /webdev
group: webdev
mode: 2775
state: directory
- name: c1
copy:
content: "It's works!\n"
dest: /webdev/index.html
setype: httpd_sys_content_t
- name: l1
file:
src: /webdev
dest: /var/www/html/webdev
state: link
setype: httpd_sys_content_t
- name: restart
service:
name: "{{ item }}"
state: restarted
enabled: yes
loop:
- httpd
- firewalld
- name: firew
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
运行剧本
[student@master ansible]$ ansible-playbook webdev.yml
查看test01组的web页面
十三、编写剧本为受管机生成硬件报告
创建名为/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]$ vim hardware.empty
hostname=inventoryhostname
mem=memory_in_MB
bios=BIOS_version
vdasize=disk_vda_size
vdbsize=disk_vdb_size
创建hardware.yml剧本
[student@master ansible]$ vim hardware.yml
---
- name: hard1
hosts: all
tasks:
- name: g1
get_url:
url: http://ansible.example.com/materials/hardware.empty
dest: /root/hardware.txt
- name: p1
replace:
path: /root/hardware.txt
regexp: inventoryhostname
replace: "{{ inventory_hostname }}"
- name: p2
replace:
path: /root/hardware.txt
regexp: memory_in_MB
replace: "{{ ansible_memtotal_mb }}"
- name: p3
replace:
path: /root/hardware.txt
regexp: BIOS_version
replace: "{{ ansible_bios_version }}"
- name: p4
replace:
path: /root/hardware.txt
regexp: BIOS_version
replace: "{{ ansible_bios_version }}"
- name: p5
replace:
path: /root/hardware.txt
regexp: disk_vda_size
replace: "{{ ansible_devices.vda.size if ansible_devices.vda.size is defind else 'NONE'}}"
- name: p6
replace:
path: /root/hardware.txt
regexp: disk_vdb_size
replace: "{{ ansible_devices.vdb.size if ansible_devices.vdb.size is defind else 'NONE'}}"
运行脚本
[student@master ansible]$ ansible-playbook hardware.yml
查看
十四、创建保险库文件
创建 ansible 保险库 /home/student/ansible/passdb.yml,其中有 2 个变量:
1)pw_dev,值为 ab1234
2)pw_man,值为 cd5678
加密和解密该库的密码是 pwd@1234,密码存在/home/student/ansible/secret.txt 中
创建剧本
[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
创建剧本
---
- name: users
hosts: test01,test02
vars_files:
- /home/student/ansible/passdb.yml
- /home/student/ansible/name_list.yml
tasks:
- name: cg
group:
name: devops
state: present
- name: c1
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: cm
hosts: web
vars_files:
- /home/student/ansible/passdb.yml
- /home/student/ansible/name_list.yml
tasks:
- name: cg2
group:
name: opsmgr
state: present
- name: us1
user:
name: "{{ item.name }}"
groups: opsmgr
password: "{{ pw_man | password_hash('sha512') }}"
state: present
password_expire_max: 30
loop: "{{ users }}"
when: item.job == 'man'
运行剧本
[student@master ansible]$ ansible-playbook users.yml --vault-id secret.txt
十六、重设保险库密码
从以下 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