# 在test组中的主机上,配置yum
[root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=myApp description='My App' baseurl=ftp://192.168.88.240/rhel8/AppStream gpgcheck=no enabled=yes"
[root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
[myApp]
baseurl = ftp://192.168.88.240/rhel8/AppStream
enabled = 1
gpgcheck = 0
name = My App
[root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=BaseOS description='Base OS' baseurl=ftp://192.168.88.240/rhel8/BaseOS gpgcheck=no enabled=yes"
[root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
[myApp]
baseurl = ftp://192.168.88.240/rhel8/AppStream
enabled = 1
gpgcheck = 0
name = My App
[BaseOS]
baseurl = ftp://192.168.88.240/rhel8/BaseOS
enabled = 1
gpgcheck = 0
name = Base OS
# 在test组中的主机上安装tar
[root@control ansible]# ansible test -m yum -a "name=tar state=present"
# 在test组中的主机上安装wget、net-tools
[root@control ansible]# ansible test -m yum -a "name=wget,net-tools"
# 在test组中的主机上卸载wget
[root@control ansible]# ansible test -m yum -a "name=wget state=absent"
# 在test主机上安装httpd
[root@control ansible]# ansible test -m yum -a "name=httpd state=latest"
# 在test主机上启动httpd,并设置它开机自启
[root@control ansible]# ansible test -m service -a "name=httpd state=started enabled=yes"
/dev/vdb
和/dev/vdc
/dev/sdb
和/dev/sdc
/dev/nvme0n1
和/dev/nvme0n2
[root@node1 ~]# lsblk # 可以查看到新加的硬盘vdb和vdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 30G 0 disk
`-vda1 253:1 0 20G 0 part /
vdb 253:16 0 20G 0 disk
vdc 253:32 0 20G 0 disk
# 在test组中的主机上安装lvm2,state不写,默认是present
[root@control ansible]# ansible test -m yum -a "name=lvm2"
# 手工在node1上对vdb进行分区
[root@node1 ~]# fdisk /dev/vdb
Command (m for help): g # 创建GPT分区表
Command (m for help): n # 新建分区
Partition number (1-128, default 1): # 回车,使用1号分区
First sector (2048-41943006, default 2048): # 起始位置,回车
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006, default 41943006): +5G # 结束位置+5G
Command (m for help): n # 新建分区
Partition number (2-128, default 2): # 回车,使用2号分区
First sector (10487808-41943006, default 10487808): # 起始位置,回车
Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943006, default 41943006): # 结束位置,回车,分区到结尾
Command (m for help): w # 存盘
[root@node1 ~]# lsblk # vdb被分出来了两个分区
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 30G 0 disk
`-vda1 253:1 0 20G 0 part /
vdb 253:16 0 20G 0 disk
|-vdb1 253:17 0 5G 0 part
`-vdb2 253:18 0 15G 0 part
vdc 253:32 0 20G 0 disk
# 在test组中的主机上创建名为myvg的卷组,该卷组由/dev/vdb1组成
[root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/vdb1"
# 在node1上查看卷组
[root@node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- <5.00g <5.00g
# 扩容卷组。卷组由PV构成,只要向卷组中加入新的PV,即可实现扩容
[root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/vdb1,/dev/vdb2"
[root@node1 ~]# vgs # 在node1上查看卷组
VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 19.99g 19.99g
# 在test组中的主机上创建名为mylv的逻辑卷,大小为2GB
[root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=2G"
# 在node1上查看逻辑卷
[root@node1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 2.00g
# mylv扩容至4GB
[root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=4G"
[root@node1 ~]# lvs # 在node1上查看逻辑卷
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 4.00g
# 在test组中的主机上,把/dev/myvg/mylv格式化为xfs
[root@control ansible]# ansible test -m filesystem -a "fstype=xfs dev=/dev/myvg/mylv"
# 在node1上查看格式化结果
[root@node1 ~]# blkid /dev/myvg/mylv
/dev/myvg/mylv: UUID="46c0af72-e517-4b15-9e53-ec72fbe1d96e" TYPE="xfs"
# 在test组中的主机上,把/dev/myvg/mylv永久挂载到/data
[root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv state=mounted fstype=xfs"
# 在node1上查看
[root@node1 ~]# tail -1 /etc/fstab
/dev/myvg/mylv /data xfs defaults 0 0
[root@node1 ~]# df -h /data/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/myvg-mylv 4.0G 61M 4.0G 2% /data
# 在test组中的主机上,卸载/dev/myvg/mylv
[root@control ansible]# ansible test -m mount -a "path=/data state=absent"
# 在test组中的主机上,强制删除/dev/myvg/mylv
[root@control ansible]# ansible test -m lvol -a "lv=mylv state=absent vg=myvg force=yes" # force是强制
# 在test组中的主机上,删除myvg卷组
[root@control ansible]# ansible test -m lvg -a "vg=myvg state=absent"
用于配置防火墙的模块
常用选项:
防火墙一般默认拒绝,明确写入允许的服务。
有一些服务有名字,有些服务没有名字。但是最终都是基于TCP或UDP的某些端口。比如http服务基于TCP80端口。服务名和端口号对应关系的说明文件是:/etc/services
配置服务器的防火墙,一般来说只要配置开放哪些服务或端口即可。没有明确开放的,都默认拒绝。
应用
# 配置httpd服务
[root@control ansible]# vim firewall.yml
---
- name: configure test
hosts: test
tasks:
- name: install httpd pkg # 这里通过yum模块装httpd
yum:
name: httpd
state: present
- name: start httpd service # 这里通过service模块启httpd服务
service:
name: httpd
state: started
enabled: yes
[root@control ansible]# ansible-playbook firewall.yml
[root@control ansible]# curl http://192.168.88.11/ # 可访问
# 安装并启动firewalld
[root@control ansible]# vim firewall.yml
---
- name: configure test
hosts: test
tasks:
- name: install httpd pkg # 这里通过yum模块装httpd
yum:
name: httpd
state: present
- name: start httpd service # 这里通过service模块启httpd服务
service:
name: httpd
state: started
enabled: yes
- name: install firewalld pkg # 这里通过yum模块装firewalld
yum:
name: firewalld
state: present
- name: start firewalld service # 这里通过service模块启firewalld服务
service:
name: firewalld
state: started
enabled: yes
[root@control ansible]# ansible-playbook firewall.yml
[root@control ansible]# curl http://192.168.88.11/ # 被拒绝
curl: (7) Failed to connect to 192.168.88.11 port 80: 没有到主机的路由
# 配置防火墙规则,放行http协议
[root@control ansible]# vim firewall.yml
---
- name: configure test
hosts: test
tasks:
- name: install httpd pkg # 这里通过yum模块装httpd
yum:
name: httpd
state: present
- name: start httpd service # 这里通过service模块启httpd服务
service:
name: httpd
state: started
enabled: yes
- name: install firewalld pkg # 这里通过yum模块安装firewalld
yum:
name: firewalld
state: present
- name: start firewalld service # 这里通过service模块启service服务
service:
name: firewalld
state: started
enabled: yes
- name: set firewalld rules # 通过firewalld模块开放80端口
firewalld:
port: 80/tcp
permanent: yes
immediate: yes
state: enabled
[root@control ansible]# ansible-playbook firewall.yml
[root@control ansible]# curl http://192.168.88.11/ # 可访问
# 使用template模块将含有变量的文件上传到test组中的主机
[root@control ansible]# vim index.j2
Welcome to {{ansible_hostname}} on {{ansible_eth0.ipv4.address}}
[root@control ansible]# vim templ.yml
---
- name: upload index
hosts: test
tasks:
- name: create web index
template:
src: index.j2
dest: /var/www/html/index.html
[root@control ansible]# ansible-playbook templ.yml
[root@control ansible]# curl http://192.168.88.11/
Welcome to node1 on 192.168.88.11
[root@node1 ~]# cat /var/www/html/index.html
Welcome to node1 on 192.168.88.11