0%

创建和运行 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

存储库为 启用状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[greg@control ansible]$ ansible-doc -l | grep yum
yum Manages packages with the `yum' package manager
yum_repository Add or remove YUM repositories
[greg@control ansible]$ ansible-doc yum_repository
[greg@control ansible]$ vim /home/greg/ansible/adhoc.sh
#!/bin/bash
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 enabled=yes'
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 enabled=yes'
[greg@control ansible]$ chmod +x /home/greg/ansible/adhoc.sh
[greg@control ansible]$ ./adhoc.sh
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "EX294_BASE",
"state": "present"
}
...
[greg@control ansible]$ ansible all -a 'ls -l /etc/yum.repos.d/'
node4 | CHANGED | rc=0 >>
total 8
-rw-r--r--. 1 root root 187 Jun 12 06:22 EX294_BASE.repo
-rw-r--r--. 1 root root 194 Jun 12 06:22 EX294_STREAM.repo
...
[greg@control ansible]$ ansible all -m yum -a 'list=repos'
node4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "",
"results": [
{
"repoid": "EX294_BASE",
"state": "enabled"
},
{
"repoid": "EX294_STREAM",
"state": "enabled"
}
]
}
...

安装和配置 Ansible

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

安装所需的软件包

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

node1dev 主机组的成员

node2test 主机组的成员

node3node4prod 主机组的成员

node5balancers 主机组的成员

prod 组是 webservers 主机组的成员

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[kiosk@foundation0 ~]$ ssh root@172.25.250.254
[root@control ~]# su - greg
[greg@control ~]$ yum search ansible
...
========================================================================================== Name Exactly Matched: ansible ==========================================================================================
ansible.noarch : SSH-based configuration management, deployment, and task execution system
...
[greg@control ~]$ sudo yum install -y ansible.noarch
...
Installed:
ansible-2.9.15-1.el8ae.noarch python3-jmespath-0.9.0-11.el8.noarch sshpass-1.06-3.el8ae.x86_64

Complete!
[greg@control ~]$ mkdir /home/greg/ansible/
[greg@control ~]$ vim /home/greg/ansible/inventory
[dev]
node1

[test]
node2

[prod]
node3
node4

[balancers]
node5

[webservers:children]
prod
[greg@control ~]$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
[greg@control ~]$ cp /etc/ansible/ansible.cfg /home/greg/ansible/ansible.cfg
[greg@control ~]$ vim /home/greg/ansible/ansible.cfg
...
inventory = /home/greg/ansible/inventory
...
# additional paths to search for roles in, colon separated
roles_path = /etc/ansible/roles:/home/greg/ansible/roles

# uncomment this to disable SSH key host checking
host_key_checking = False
...
[privilege_escalation]
become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
...
[greg@control ansible]$ ansible --version
ansible 2.9.15
config file = /home/greg/ansible/ansible.cfg
...
[greg@control ansible]$ ansible-inventory --graph
@all:
|--@balancers:
| |--node5
|--@dev:
| |--node1
|--@test:
| |--node2
|--@ungrouped:
|--@webservers:
| |--@prod:
| | |--node3
| | |--node4
[greg@control ansible]$ ansible all -m ping
node4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
...

配置系统调优

为您的系统选择建议的 tuned 配置集并将它设为默认设置。

1
2
3
4
5
6
7
8
[root@node2 ~]# man tuned-adm 
[root@node2 ~]# tuned-adm active
Current active profile: throughput-performance
[root@node2 ~]# tuned-adm recommend
virtual-guest
[root@node2 ~]# tuned-adm profile virtual-guest
[root@node2 ~]# tuned-adm active
Current active profile: virtual-guest

创建 VDO 卷

根据如下要求,创建新的 VDO 卷:

使用未分区的磁盘

该卷的名称为 vdough

该卷的逻辑大小为 50G

该卷使用 xfs 文件系统格式化

该卷(在系统启动时)挂载到 /vbread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@node2 ~]# yum install -y vdo
...
Installed:
kmod-kvdo-6.2.2.117-65.el8.x86_64 vdo-6.2.2.117-13.el8.x86_64

Complete!
[root@node2 ~]# systemctl enable --now vdo
[root@node2 ~]# vdo create --name=vdough --device=/dev/vdc --vdoLogicalSize=50G
Creating VDO vdough
The VDO volume can address 6 GB in 3 data slabs, each 2 GB.
It can grow to address at most 16 TB of physical storage in 8192 slabs.
If a larger maximum size might be needed, use bigger slabs.
Starting VDO vdough
Starting compression on VDO vdough
VDO instance 0 volume is ready at /dev/mapper/vdough
[root@node2 ~]# mkfs.xfs /dev/mapper/vdough
meta-data=/dev/mapper/vdough isize=512 agcount=4, agsize=3276800 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=6400, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@node2 ~]# mkdir /vbread
[root@node2 ~]# vim /etc/fstab
...
/dev/mapper/vdough /vbread xfs defaults,x-systemd.requires=vdo.service 0 0
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# mount /vbread/
[root@node2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
...
/dev/mapper/vdough xfs 50G 390M 50G 1% /vbread

创建逻辑卷

根据如下要求,创建新的逻辑卷:

逻辑卷取名为 qa,属于 qagroup 卷组,大小为 60 个扩展块

qagroup 卷组中逻辑卷的扩展块大小应当为 16 MiB

使用 ext3 文件系统格式化新逻辑卷。该逻辑卷应在系统启动时自动挂载到 /mnt/qa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
[root@node2 ~]# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): p

Selected partition 4
First sector (2095106-8388607, default 3645440):
Last sector, +sectors or +size{K,M,G,T,P} (3645440-8388607, default 8388607):

Created a new partition 4 of type 'Linux' and of size 2.3 GiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

[root@node2 ~]# pvcreate /dev/vdb4
Physical volume "/dev/vdb4" successfully created.
[root@node2 ~]# vgcreate qagroup /dev/vdb4 -s 16m
Volume group "qagroup" successfully created
[root@node2 ~]# vgdisplay qagroup
--- Volume group ---
VG Name qagroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 2.25 GiB
PE Size 16.00 MiB
Total PE 144
Alloc PE / Size 60 / 960.00 MiB
Free PE / Size 84 / 1.31 GiB
VG UUID HesSpg-CsMU-Dckq-Oydn-cUmK-cc3o-ddDJEW

[root@node2 ~]# lvcreate qagroup -l 60 -n qa
Logical volume "qa" created.
[root@node2 ~]# lvdisplay /dev/qagroup/qa
--- Logical volume ---
LV Path /dev/qagroup/qa
LV Name qa
VG Name qagroup
LV UUID WsfmKz-HKza-UrPO-CbBg-LkLH-GTwg-LOUkEk
LV Write Access read/write
LV Creation host, time node2.domain250.example.com, 2023-02-19 12:52:20 -0500
LV Status available
# open 0
LV Size 960.00 MiB
Current LE 60
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

[root@node2 ~]# mkfs.ext3 /dev/qagroup/qa
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 245760 4k blocks and 61440 inodes
Filesystem UUID: 2ea0bb6b-a006-4b79-a181-3a8236f1d920
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

[root@node2 ~]# lsblk --fs
NAME FSTYPE LABEL UUID MOUNTPOINT
...
vdb
...
└─vdb4 LVM2_member xiO2Ji-56zW-TaDP-nH6B-3mCW-4rmL-WumxrZ
└─qagroup-qa ext3 2ea0bb6b-a006-4b79-a181-3a8236f1d920
vdc
[root@node2 ~]# mkdir -p /mnt/qa
[root@node2 ~]# vim /etc/fstab
...
/dev/qagroup/qa /mnt/qa ext3 defaults 0 0
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# mount /mnt/qa
[root@node2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
...
/dev/mapper/qagroup-qa ext3 929M 1.2M 880M 1% /mnt/qa