以上学习了ping、command、shell、script模块,但一般不建议使用以上三个,因为这三个模块没有幂等性。举例如下:
- [root@control ansible]# ansible test -a "mkdir /tmp/1234"
- [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
- If you need to use command because file is insufficient you can add 'warn: false' to this
- command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
-
- node1 | CHANGED | rc=0 >>
- [root@control ansible]# ansible test -a "mkdir /tmp/1234"
- [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
- If you need to use command because file is insufficient you can add 'warn: false' to this
- command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
-
- node1 | FAILED | rc=1 >>
- mkdir: 无法创建目录 “/tmp/1234”: 文件已存在non-zero return code
- 因为被控主机已经存在了要创建的目录,所以报错显示已存在
- 如果不想看到报错,可以使用专用的file模块
- [root@control ansible]# ansible test -m file -a "path=/tmp/1234 state=directory"
- node1 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": false,
- "gid": 0,
- "group": "root",
- "mode": "0755",
- "owner": "root",
- "path": "/tmp/1234",
- "size": 6,
- "state": "directory",
- "uid": 0
- }
- 查看使用帮助
- EXAMPLES:
- - name: Change file ownership, group and permissions
- file: 模块名。以下是它的各种参数
- path: /etc/foo.conf 要修改的文件的路径
- owner: foo 文件所有者
- group: foo 文件的所有组
- mode: '0644' 权限
- 根据上面的example,-m file -a的内容就是doc中把各参数的冒号换成=号
-
- 在test主机上创建/tmp/file.txt
- [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=touch" touch是指如果文件不存在,则创建
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dest": "/tmp/file.txt",
- "gid": 0,
- "group": "root",
- "mode": "0644",
- "owner": "root",
- "size": 0,
- "state": "file",
- "uid": 0
- }
- [root@node1 ~]# ls /tmp | grep file.txt
- file.txt
- 在test主机上创建/tmp/demo目录
- [root@control ansible]# ansible test -m file -a "path=/tmp/demo state=directory"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "gid": 0,
- "group": "root",
- "mode": "0755",
- "owner": "root",
- "path": "/tmp/demo",
- "size": 6,
- "state": "directory",
- "uid": 0
- }
- [root@node1 ~]# ls /tmp | grep demo
- demo
- 在test主机上创建一个/tmp/file.txt文件,属主为sshd,属组为adn 权限为777
- [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode="0777""
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "gid": 4,
- "group": "adm",
- "mode": "0777",
- "owner": "sshd",
- "path": "/tmp/file.txt",
- "size": 0,
- "state": "file",
- "uid": 74
- }
- [root@node1 ~]# ll /tmp | grep file
- -rwxrwxrwx 1 sshd adm 0 11月 8 18:00 file.txt
- 在test主机上删除/tmp/file.txt文件
- [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=absent"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "path": "/tmp/file.txt",
- "state": "absent"
- }
- 在test主机上删除/tmp/demo目录
- [root@control ansible]# ansible test -m file -a "path=/tmp/demo state=absent"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "path": "/tmp/demo",
- "state": "absent"
- }
- 在test主机上创建/etc/hosts的软连接,目标是/tmp/host.txt
- [root@control ansible]# ansible test -m file -a "src=/etc/hosts dest=/tmp/host.txt state=link"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dest": "/tmp/host.txt",
- "gid": 0,
- "group": "root",
- "mode": "0777",
- "owner": "root",
- "size": 10,
- "src": "/etc/hosts",
- "state": "link",
- "uid": 0
- }
-
用于将文件从控制端拷贝到被控端
常用选项:
src:源。控制端的文件路径
dest:目标。被控制端的文件路径
content: 内容。需要写到文件中的内容
- [root@control ansible]# ansible test -m copy -a "src=a.txt dest=/root"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "checksum": "54e87908396f730ae24754dc967d141bee7a293f",
- "dest": "/root/a.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "5ce11a5724b80ca946683b6c626bdb6c",
- "mode": "0644",
- "owner": "root",
- "size": 4,
- "src": "/root/.ansible/tmp/ansible-tmp-1699486007.0826297-54906651569985/source",
- "state": "file",
- "uid": 0
- }
- [root@node1 ~]# cat a.txt
- Aaa
- 绝对路径
- [root@control ansible]# ansible test -m copy -a "src=/root/root.txt dest=/root"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "checksum": "552c0ba71b1046a083583ebf943cc9aa09f39a32",
- "dest": "/root/root.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "74cc1c60799e0a786ac7094b532f01b1",
- "mode": "0644",
- "owner": "root",
- "size": 5,
- "src": "/root/.ansible/tmp/ansible-tmp-1699486076.8466651-150992524492280/source",
- "state": "file",
- "uid": 0
- }
- [root@node1 ~]# cat root.txt
- root
- 发送目录成功
- [root@control ansible]# ansible test -m copy -a "src=/etc/security dest=/root/"
- node1 | CHANGED => {
- "changed": true,
- "dest": "/root/",
- "src": "/etc/security"
- }
-
- [root@node1 ~]# ls | grep test
- [root@node1 ~]# ll | grep security
- drwxr-xr-x 7 root root 4096 11月 8 18:31 security
- 前提是目录不能为空
- 如果 getenforce状态不为Disabled,需要再各个主机安装python3-libselinux软件包
- 在远程主机直接创建文件,添加内容
- [root@control ansible]# ansible test -m copy -a "dest=/tmp/mytest.txt content='hello world'"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
- "dest": "/tmp/mytest.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "5eb63bbbe01eeed093cb22bb8f5acdc3",
- "mode": "0644",
- "owner": "root",
- "size": 11,
- "src": "/root/.ansible/tmp/ansible-tmp-1699486796.9039702-79725117871198/source",
- "state": "file",
- "uid": 0
- }
- [root@node1 ~]# cat /tmp/mytest.txt
- hello world
与copy模块相反,copy是上传,fetch是下载
常用选项:
src:源。被控制端的文件路径
dest:目标。控制端的文件路径
- 将test主机上的/etc/hostname下载到本地用户的家目录下
- [root@control ansible]# ansible webservers -m fetch -a "src=/etc/hostname dest=~/"
- node3 | CHANGED => {
- "changed": true,
- "checksum": "70e478f6fb7a1971d09496d109002c5809006a86",
- "dest": "/root/node3/etc/hostname",
- "md5sum": "3ce6701b5ee42becf085baf7368fe8ce",
- "remote_checksum": "70e478f6fb7a1971d09496d109002c5809006a86",
- "remote_md5sum": null
- }
- node4 | CHANGED => {
- "changed": true,
- "checksum": "5367c434083cf09560c19a3338c1d6caa791f36b",
- "dest": "/root/node4/etc/hostname",
- "md5sum": "a97ac14927fea0efc7a9733fe320cd99",
- "remote_checksum": "5367c434083cf09560c19a3338c1d6caa791f36b",
- "remote_md5sum": null
- }
- [root@control ansible]# ls ~/node3/etc/
- hostname
- [root@control ansible]# ls ~/node4/etc/
- hostname
- 不能下载目录
- [root@control ansible]# ansible webservers -m fetch -a "src=/root/aaa dest=~/"
- node4 | FAILED! => {
- "changed": false,
- "file": "/root/aaa",
- "msg": "remote file is a directory, fetch cannot work on directories"
- }
- node3 | FAILED! => {
- "changed": false,
- "file": "/root/aaa",
- "msg": "remote file is a directory, fetch cannot work on directories"
- }
用于确保存目标文件中有某一行内容
常用选项:
path:待修改的文件路径
line: 写入文件的一行内容
regexp:正则表达式,用于查找文件中的内容
- test组的主机,/etc/issue中一定要有一行hello world。如果该行不存在,则默认添加到文件结尾
- [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='hello world'"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "backup": "",
- "changed": true,
- "msg": "line added"
- }
- [root@node1 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
- hello world
- test组中的主机,把/etc/issue中有hello的行,替换成chi le ma
- [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='chi le ma' regexp=hello"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "backup": "",
- "changed": true,
- "msg": "line replaced"
- }
- [root@node1 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
- chi le ma
- [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='chi le ma' regexp=hello"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "backup": "",
- "changed": true,
- "msg": "line added"
- }
- [root@node1 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
- chi le ma
- chi le ma
lineinfile会替换一行,replace可以替换关键词
常用选项:
path:待修改的文件路径
replace:将正则表达式查到的内容,替换成replace的内容
regexp: 正则表达式,用于查找文件中的内容
- 将test组中的主机上/etc/issue文件中的chi,替换成he
- [root@node1 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
- chi le ma
- chi le ma
- [root@control ansible]# ansible test -m replace -a "path=/etc/issue regexp=chi replace="he""
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "msg": "2 replacements made"
- }
- [root@node1 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
- he le ma
- he le ma

- [root@control ansible]# cat exec.sh
- #!/bin/bash
- ansible test -m file -a "path=/tmp/mydemo state=directory owner=adm group=adm mode=0777"
- ansible test -m copy -a "src=/etc/hosts dest=/tmp/mydemo owner=adm group=adm mode=0600"
- ansible test -m replace -a "path=/tmp/mydemo/hosts regexp='node5' replace='server5'"
- ansible test -m fetch -a "src=/tmp/mydemo/hosts dest=/root/ansible/"
实现linux用户管理
常用选项:
name: 待创建的用户名
uid:用户ID
group:设置主组
groups:设置附加组
home:设置家目录
password:设置用户密码
state:状态。present表示创建,它是默认选项。absent表示删除
remove:删除家目录、邮箱等。值为yes或者true都可以
- 在test主机上创建个tom用户
- [root@control ansible]# ansible test -m user -a "user=tom"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "comment": "",
- "create_home": true,
- "group": 1000,
- "home": "/home/tom",
- "name": "tom",
- "shell": "/bin/bash",
- "state": "present",
- "system": false,
- "uid": 1000
- }
- [root@node1 ~]# cat /etc/passwd | grep ^tom
- tom:x:1000:1000::/home/tom:/bin/bash
- [root@control ansible]# ansible test -m user -a "name=jerry uid=1010 group=adm groups=daemon,root home=/home/jerry"
- 设置tom密码为123456,用sha512加密
- [root@control ansible]# ansible test -m user -a "name=tom password={{'123456'|password_hash('sha512')}}"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "append": false,
- "changed": true,
- "comment": "",
- "group": 1000,
- "home": "/home/tom",
- "move_home": false,
- "name": "tom",
- "password": "NOT_LOGGING_PASSWORD",
- "shell": "/bin/bash",
- "state": "present",
- "uid": 1000
- }
- [root@node1 ~]# cat /etc/shadow | grep tom
- tom:$6$Dfyqw6ty.pPwnyZm$SRpTqqORuCbPFGcdPuT8sNHHmIpHJAslaoDgk1RCA6gIAEEeg9tvz8MBxj7mGR1j4LV7GVN.1teZQ7OUaP51J1:19670:0:99999:7:::
- 删除tom用户,不删除家目录
- [root@control ansible]# ansible test -m user -a "name=tom state=absent"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "force": false,
- "name": "tom",
- "remove": false,
- "state": "absent"
- }
- 删除jerry用户,同时删除家目录
- [root@control ansible]# ansible test -m user -a "name=jerry state=absent remove=yes"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "force": false,
- "name": "jerry",
- "remove": true,
- "state": "absent"
- }
- [root@node1 ~]# ls /home
- tom
创建、删除组
常用选项:
name:待创建的组名
gid:组的ID号
state:prensent表示创建、它是默认选项。absent是删除
- 在test组的主机上创建名为devops的组
- [root@control ansible]# ansible test -m group -a "name=devops state=present"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "gid": 1000,
- "name": "devops",
- "state": "present",
- "system": false
- }
- 在test组的主机上删除名为devops的组
- [root@control ansible]# ansible test -m group -a "name=devops state=absent"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "name": "devops",
- "state": "absent"
- }
用于配置yum
常用选项:
file:指定文件名
其他选项,请于文件内容对照
- 在test组中的主机上,配置yum
- [root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=App baseurl=ftp://192.168.88.240/rhel8/AppStream gpgcheck=no enabled=yes description=app"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "repo": "App",
- "state": "present"
- }
- [root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
- [App]
- baseurl = ftp://192.168.88.240/rhel8/AppStream
- enabled = 1
- gpgcheck = 0
- name = app
- 再次执行
- [root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=BaseOs baseurl=ftp://192.168.88.240/rhel8/BaseOs gpgcheck=no enabled=yes description=BaseOs"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "repo": "BaseOs",
- "state": "present"
- }
- [root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
- [App]
- baseurl = ftp://192.168.88.240/rhel8/AppStream
- enabled = 1
- gpgcheck = 0
- name = app
-
-
- [BaseOs]
- baseurl = ftp://192.168.88.240/rhel8/BaseOs
- enabled = 1
- gpgcheck = 0
- name = BaseOs
用于rpm软件包管理,如安装、升级、卸载
常用选项:
name:包名
state:状态。present表示安装,如果已安装则忽略;latest表示安装或升级到最新版本;absent表示卸载
- 在test组中的主机上安装wget
- [root@control ansible]# ansible test -m yum -a "name=wget state=present"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "msg": "",
- "rc": 0,
- "results": [
- "Installed: wget",
- "Installed: wget-1.19.5-8.el8_1.1.x86_64"
- ]
- }
- [root@node1 ~]# yum -y install wget
- Updating Subscription Management repositories.
- Unable to read consumer identity
- This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
- 上次元数据过期检查:0:00:59 前,执行于 2023年11月09日 星期四 17时16分34秒。
- 软件包 wget-1.19.5-8.el8_1.1.x86_64 已安装。
- 依赖关系解决。
- 无需任何处理。
- 完毕!
- 在test组中的主机上卸载wget
- [root@control ansible]# ansible test -m yum -a "name=wget state=absent"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "msg": "",
- "rc": 0,
- "results": [
- "Removed: wget-1.19.5-8.el8_1.1.x86_64"
- ]
- }
用于控制服务。启动、关闭、重启、开机自启
常用选项:
name:控制的服务名
state: started表示启动 stopped表示关闭 restarted表示重启
enabled: yes表示设置开机自启;no表示设置开启不启动
- 在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"
- [root@node1 ~]# systemctl status httpd
- ● httpd.service - The Apache HTTP Server
- Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
- Active: active (running) since Thu 2023-11-09 17:29:59 EST; 2min 14s ago
-
逻辑卷可以动态管理存储空间。可以对逻辑卷进行扩展或缩减
可以把硬盘或分区转换成物理卷PV;再把1到多个PV组合成卷组VG;然后在VG上划分逻辑卷LV。LV可以像普通分区一样,进行格式化、挂载
关闭虚拟机node1,为其添加2块20Gb的硬盘
LINUX下KVM虚拟机新加的硬盘,名称是/dev/vdb和/dev/vdc
vmware虚拟机新加的硬盘,名称是/dev/sdb和/dev/sdc
如果选nvme硬盘,名称可能是/dev/nvme0n1和/dev/nvme0n2
创建、删除卷组,修改卷组大小
常用选项:
vg:定义卷组名。vg: volume group
pvs:由哪些物理卷构成。pvs:physical volumes
- 先手工进行gpt分区
- [root@node1 ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sr0 11:0 1 7.9G 0 rom
- nvme0n1 259:0 0 20G 0 disk
- ├─nvme0n1p1 259:1 0 1G 0 part /boot
- └─nvme0n1p2 259:2 0 19G 0 part
- ├─rhel-root 253:0 0 17G 0 lvm /
- └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
- nvme0n2 259:3 0 20G 0 disk
- [root@node1 ~]# fdisk /dev/nvme0n2
-
- 欢迎使用 fdisk (util-linux 2.32.1)。
- 更改将停留在内存中,直到您决定将更改写入磁盘。
- 使用写入命令前请三思。
-
- 设备不包含可识别的分区表。
- 创建了一个磁盘标识符为 0xdf955d41 的新 DOS 磁盘标签。
-
- 命令(输入 m 获取帮助):m
-
- 帮助:
-
- DOS (MBR)
- a 开关 可启动 标志
- b 编辑嵌套的 BSD 磁盘标签
- c 开关 dos 兼容性标志
-
- 常规
- d 删除分区
- F 列出未分区的空闲区
- l 列出已知分区类型
- n 添加新分区
- p 打印分区表
- t 更改分区类型
- v 检查分区表
- i 打印某个分区的相关信息
-
- 杂项
- m 打印此菜单
- u 更改 显示/记录 单位
- x 更多功能(仅限专业人员)
-
- 脚本
- I 从 sfdisk 脚本文件加载磁盘布局
- O 将磁盘布局转储为 sfdisk 脚本文件
-
- 保存并退出
- w 将分区表写入磁盘并退出
- q 退出而不保存更改
-
- 新建空磁盘标签
- g 新建一份 GPT 分区表
- G 新建一份空 GPT (IRIX) 分区表
- o 新建一份的空 DOS 分区表
- s 新建一份空 Sun 分区表
-
-
- 命令(输入 m 获取帮助):g 创建GPT分区表
- 已创建新的 GPT 磁盘标签(GUID: 66C691FD-9290-5A40-A7FE-7140003BB76B)。
-
- 命令(输入 m 获取帮助):n 新建分区
- 分区号 (1-128, 默认 1): 回车,使用1号分区
- 第一个扇区 (2048-41943006, 默认 2048): 起始位置,回车
- 上个扇区,+sectors 或 +size{K,M,G,T,P} (2048-41943006, 默认 41943006): +5G 结束位置+5G
-
- 创建了一个新分区 1,类型为“Linux filesystem”,大小为 5 GiB。
-
- 命令(输入 m 获取帮助):n 新建分区
- 分区号 (2-128, 默认 2): 回车 使用2号分区
- 第一个扇区 (10487808-41943006, 默认 10487808): 起始位置,回车
- 上个扇区,+sectors 或 +size{K,M,G,T,P} (10487808-41943006, 默认 41943006):
-
- 创建了一个新分区 2,类型为“Linux filesystem”,大小为 15 GiB。
-
- 命令(输入 m 获取帮助):p
- Disk /dev/nvme0n2:20 GiB,21474836480 字节,41943040 个扇区
- 单元:扇区 / 1 * 512 = 512 字节
- 扇区大小(逻辑/物理):512 字节 / 512 字节
- I/O 大小(最小/最佳):512 字节 / 512 字节
- 磁盘标签类型:gpt
- 磁盘标识符:66C691FD-9290-5A40-A7FE-7140003BB76B
-
- 设备 起点 末尾 扇区 大小 类型
- /dev/nvme0n2p1 2048 10487807 10485760 5G Linux 文件系统
- /dev/nvme0n2p2 10487808 41943006 31455199 15G Linux 文件系统
-
- 命令(输入 m 获取帮助):w
- 分区表已调整。
- 将调用 ioctl() 来重新读分区表。
- 正在同步磁盘。
-
- [root@node1 ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sr0 11:0 1 7.9G 0 rom
- nvme0n1 259:0 0 20G 0 disk
- ├─nvme0n1p1 259:1 0 1G 0 part /boot
- └─nvme0n1p2 259:2 0 19G 0 part
- ├─rhel-root 253:0 0 17G 0 lvm /
- └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
- nvme0n2 259:3 0 20G 0 disk
- ├─nvme0n2p1 259:4 0 5G 0 part
- └─nvme0n2p2 259:5 0 15G 0 part
-
- 在test组中的主机上创建名为myvg的卷组,该卷组由/dev/nvme0n2p1组成
- [root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/nvme0n2p1"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true
- }
- 在node1上查看卷组
- [root@node1 ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- myvg 1 0 0 wz--n- <5.00g <5.00g
- rhel 1 2 0 wz--n- <19.00g 0
- 扩容卷组。卷组由PV构成,只要向卷组中加入新的PV,即可实现扩容
- [root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/nvme0n2p1,/dev/nvme0n2p2"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true
- }
- [root@node1 ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- myvg 2 0 0 wz--n- 19.99g 19.99g
- rhel 1 2 0 wz--n- <19.00g 0
-
创建、删除逻辑卷,修改逻辑卷大小
常用选项
vg:指定在哪个卷组上创建逻辑卷
lv:创建的逻辑卷名。lv:logical volume
size:逻辑卷的大小,不写单位,以M为单位
- 在test组中的主机上创建名为mylv的逻辑卷,大小为2G
- [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=2G"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "msg": ""
- }
- 在node1查看逻辑卷
- [root@node1 ~]# lvs
- LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
- mylv myvg -wi-a----- 2.00g
- root rhel -wi-ao---- <17.00g
- swap rhel -wi-ao---- 2.00g
- mylv扩容至4GB
- [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=4G"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "lv": "mylv",
- "size": 2.0,
- "vg": "myvg"
- }
- [root@node1 ~]# lvs
- LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
- mylv myvg -wi-a----- 4.00g
格式化逻辑卷
常用选项:
fstype: 指定文件系统类型
dev:指定要格式化的设备,可以是分区,可以是逻辑卷
- 在test组中的主机上,把/dev/myvg/mylv格式化为xfs
- [root@control ansible]# ansible test -m filesystem -a "dev=/dev/myvg/mylv fstype=xfs"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true
- }
- 在node1上查看格式化结果
- [root@node1 ~]# blkid /dev/myvg/mylv
- /dev/myvg/mylv: UUID="7d0f9e53-bbd8-4da3-9bd5-85ae90fc290b" TYPE="xfs"
用于挂载文件系统
常用选项:
path:挂载点。如果挂载点不存在,自动创建。
src:待挂载的设备
fstype:文件系统类型
state: mounted,表示永久挂载
- 在test组中的主机上,把/dev/myvg/mylv永久挂在到/data
- [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv fstype=xfs state=mounted"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dump": "0",
- "fstab": "/etc/fstab",
- "fstype": "xfs",
- "name": "/data",
- "opts": "defaults",
- "passno": "0",
- "src": "/dev/myvg/mylv"
- }
- 在node1上查看
- [root@node1 ~]# cat /etc/fstab
- /dev/mapper/rhel-root / xfs defaults 0 0
- UUID=cb52237f-a2b2-423c-9d18-66892297474c /boot xfs defaults 0 0
- /dev/mapper/rhel-swap swap swap defaults 0 0
- /dev/myvg/mylv /data xfs defaults 0 0
-
- 对逻辑卷mylv进行扩容到5G
- ansible test -m lvol -a "vg=myvg lv=mylv size=5G"
- [root@node1 ~]# df -h
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/mapper/myvg-mylv 4.0G 61M 4.0G 2% /data
- 但挂载点不会更新为5G
- 扩容时应该加上resizefs参数
- [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=7G resizefs=yes"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "lv": "mylv",
- "size": 6.0,
- "vg": "myvg"
- }
- [root@node1 ~]# df -h |grep data
- /dev/mapper/myvg-mylv 7.0G 83M 7.0G 2% /data
- 卸载
- [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv fstype=xfs state=mounted"
- [root@node1 ~]# df -h |grep data
- 重新挂载
- [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv state=mounted fstype=xfs"
- node1 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dump": "0",
- "fstab": "/etc/fstab",
- "fstype": "xfs",
- "name": "/data",
- "opts": "defaults",
- "passno": "0",
- "src": "/dev/myvg/mylv"
- }
- [root@node1 ~]# df -h |grep data
- /dev/mapper/myvg-mylv 7.0G 83M 7.0G 2% /data
- 永久卸载
- [root@control ansible]# ansible test -m mount -a "path=/data state=absent"
- [root@node1 ~]# cat /etc/fstab |grep data
- [root@node1 ~]# df -h |grep data
删除逻辑卷
[root@control ansible]# ansible test -m lvol -a "vg=/dev/myvg lv=/dev/myvg/mylv state=absent force=yes"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[root@node1 ~]# lvs | grep mylv
删除卷组
[root@control ansible]# ansible test -m lvg -a "vg=myvg state=absent"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[root@node1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <19.00g 0