• ansible-第二天


    ansible

    第二天

    以上学习了ping、command、shell、script模块,但一般不建议使用以上三个,因为这三个模块没有幂等性。举例如下:

    1. [root@control ansible]# ansible test -a "mkdir /tmp/1234"
    2. [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
    3. If you need to use command because file is insufficient you can add 'warn: false' to this
    4. command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    5. node1 | CHANGED | rc=0 >>
    6. [root@control ansible]# ansible test -a "mkdir /tmp/1234"
    7. [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
    8. If you need to use command because file is insufficient you can add 'warn: false' to this
    9. command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    10. node1 | FAILED | rc=1 >>
    11. mkdir: 无法创建目录 “/tmp/1234”: 文件已存在non-zero return code
    12. 因为被控主机已经存在了要创建的目录,所以报错显示已存在
    13. 如果不想看到报错,可以使用专用的file模块
    14. [root@control ansible]# ansible test -m file -a "path=/tmp/1234 state=directory"
    15. node1 | SUCCESS => {
    16.    "ansible_facts": {
    17.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    18.   },
    19.    "changed": false,
    20.    "gid": 0,
    21.    "group": "root",
    22.    "mode": "0755",
    23.    "owner": "root",
    24.    "path": "/tmp/1234",
    25.    "size": 6,
    26.    "state": "directory",
    27.    "uid": 0
    28. }
    file模块

    1. 查看使用帮助
    2. EXAMPLES:
    3. - name: Change file ownership, group and permissions
    4. file:   模块名。以下是它的各种参数
    5.   path: /etc/foo.conf 要修改的文件的路径
    6.   owner: foo   文件所有者
    7.   group: foo   文件的所有组
    8.   mode: '0644' 权限
    9. 根据上面的example,-m file -a的内容就是doc中把各参数的冒号换成=号
    10. test主机上创建/tmp/file.txt
    11. [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=touch" touch是指如果文件不存在,则创建
    12. node1 | CHANGED => {
    13.    "ansible_facts": {
    14.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    15.   },
    16.    "changed": true,
    17.    "dest": "/tmp/file.txt",
    18.    "gid": 0,
    19.    "group": "root",
    20.    "mode": "0644",
    21.    "owner": "root",
    22.    "size": 0,
    23.    "state": "file",
    24.    "uid": 0
    25. }
    26. [root@node1 ~]# ls /tmp | grep file.txt
    27. file.txt
    28. test主机上创建/tmp/demo目录
    29. [root@control ansible]# ansible test -m file -a "path=/tmp/demo state=directory"
    30. node1 | CHANGED => {
    31.    "ansible_facts": {
    32.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    33.   },
    34.    "changed": true,
    35.    "gid": 0,
    36.    "group": "root",
    37.    "mode": "0755",
    38.    "owner": "root",
    39.    "path": "/tmp/demo",
    40.    "size": 6,
    41.    "state": "directory",
    42.    "uid": 0
    43. }
    44. [root@node1 ~]# ls /tmp | grep demo
    45. demo
    46. test主机上创建一个/tmp/file.txt文件,属主为sshd,属组为adn 权限为777
    47. [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode="0777""
    48. node1 | CHANGED => {
    49.    "ansible_facts": {
    50.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    51.   },
    52.    "changed": true,
    53.    "gid": 4,
    54.    "group": "adm",
    55.    "mode": "0777",
    56.    "owner": "sshd",
    57.    "path": "/tmp/file.txt",
    58.    "size": 0,
    59.    "state": "file",
    60.    "uid": 74
    61. }
    62. [root@node1 ~]# ll /tmp | grep file
    63. -rwxrwxrwx  1 sshd adm     0 11月  8 18:00 file.txt
    64. test主机上删除/tmp/file.txt文件
    65. [root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=absent"
    66. node1 | CHANGED => {
    67.    "ansible_facts": {
    68.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    69.   },
    70.    "changed": true,
    71.    "path": "/tmp/file.txt",
    72.    "state": "absent"
    73. }
    74. test主机上删除/tmp/demo目录
    75. [root@control ansible]# ansible test -m file -a "path=/tmp/demo state=absent"
    76. node1 | CHANGED => {
    77.    "ansible_facts": {
    78.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    79.   },
    80.    "changed": true,
    81.    "path": "/tmp/demo",
    82.    "state": "absent"
    83. }
    84. test主机上创建/etc/hosts的软连接,目标是/tmp/host.txt
    85. [root@control ansible]# ansible test -m file -a "src=/etc/hosts dest=/tmp/host.txt state=link"
    86. node1 | CHANGED => {
    87.    "ansible_facts": {
    88.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    89.   },
    90.    "changed": true,
    91.    "dest": "/tmp/host.txt",
    92.    "gid": 0,
    93.    "group": "root",
    94.    "mode": "0777",
    95.    "owner": "root",
    96.    "size": 10,
    97.    "src": "/etc/hosts",
    98.    "state": "link",
    99.    "uid": 0
    100. }

    copy模块

    用于将文件从控制端拷贝到被控端

    常用选项:

    src:源。控制端的文件路径

    dest:目标。被控制端的文件路径

    content: 内容。需要写到文件中的内容

    1. [root@control ansible]# ansible test -m copy -a "src=a.txt dest=/root"
    2. node1 | CHANGED => {
    3.    "ansible_facts": {
    4.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    5.   },
    6.    "changed": true,
    7.    "checksum": "54e87908396f730ae24754dc967d141bee7a293f",
    8.    "dest": "/root/a.txt",
    9.    "gid": 0,
    10.    "group": "root",
    11.    "md5sum": "5ce11a5724b80ca946683b6c626bdb6c",
    12.    "mode": "0644",
    13.    "owner": "root",
    14.    "size": 4,
    15.    "src": "/root/.ansible/tmp/ansible-tmp-1699486007.0826297-54906651569985/source",
    16.    "state": "file",
    17.    "uid": 0
    18. }
    19. [root@node1 ~]# cat a.txt
    20. Aaa
    21. 绝对路径
    22. [root@control ansible]# ansible test -m copy -a "src=/root/root.txt dest=/root"
    23. node1 | CHANGED => {
    24.    "ansible_facts": {
    25.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    26.   },
    27.    "changed": true,
    28.    "checksum": "552c0ba71b1046a083583ebf943cc9aa09f39a32",
    29.    "dest": "/root/root.txt",
    30.    "gid": 0,
    31.    "group": "root",
    32.    "md5sum": "74cc1c60799e0a786ac7094b532f01b1",
    33.    "mode": "0644",
    34.    "owner": "root",
    35.    "size": 5,
    36.    "src": "/root/.ansible/tmp/ansible-tmp-1699486076.8466651-150992524492280/source",
    37.    "state": "file",
    38.    "uid": 0
    39. }
    40. [root@node1 ~]# cat root.txt
    41. root
    42. 发送目录成功
    43. [root@control ansible]# ansible test -m copy -a "src=/etc/security dest=/root/"
    44. node1 | CHANGED => {
    45.    "changed": true,
    46.    "dest": "/root/",
    47.    "src": "/etc/security"
    48. }
    49. [root@node1 ~]# ls | grep test
    50. [root@node1 ~]# ll | grep security
    51. drwxr-xr-x  7 root root 4096 11月  8 18:31 security
    52. 前提是目录不能为空
    53. 如果 getenforce状态不为Disabled,需要再各个主机安装python3-libselinux软件包
    54. 在远程主机直接创建文件,添加内容
    55. [root@control ansible]# ansible test -m copy -a "dest=/tmp/mytest.txt content='hello world'"
    56. node1 | CHANGED => {
    57.    "ansible_facts": {
    58.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    59.   },
    60.    "changed": true,
    61.    "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
    62.    "dest": "/tmp/mytest.txt",
    63.    "gid": 0,
    64.    "group": "root",
    65.    "md5sum": "5eb63bbbe01eeed093cb22bb8f5acdc3",
    66.    "mode": "0644",
    67.    "owner": "root",
    68.    "size": 11,
    69.    "src": "/root/.ansible/tmp/ansible-tmp-1699486796.9039702-79725117871198/source",
    70.    "state": "file",
    71.    "uid": 0
    72. }
    73. [root@node1 ~]# cat /tmp/mytest.txt
    74. hello world
    fetch模块

    与copy模块相反,copy是上传,fetch是下载

    常用选项:

    src:源。被控制端的文件路径

    dest:目标。控制端的文件路径

    1. test主机上的/etc/hostname下载到本地用户的家目录下
    2. [root@control ansible]# ansible webservers -m fetch -a "src=/etc/hostname dest=~/"
    3. node3 | CHANGED => {
    4.    "changed": true,
    5.    "checksum": "70e478f6fb7a1971d09496d109002c5809006a86",
    6.    "dest": "/root/node3/etc/hostname",
    7.    "md5sum": "3ce6701b5ee42becf085baf7368fe8ce",
    8.    "remote_checksum": "70e478f6fb7a1971d09496d109002c5809006a86",
    9.    "remote_md5sum": null
    10. }
    11. node4 | CHANGED => {
    12.    "changed": true,
    13.    "checksum": "5367c434083cf09560c19a3338c1d6caa791f36b",
    14.    "dest": "/root/node4/etc/hostname",
    15.    "md5sum": "a97ac14927fea0efc7a9733fe320cd99",
    16.    "remote_checksum": "5367c434083cf09560c19a3338c1d6caa791f36b",
    17.    "remote_md5sum": null
    18. }
    19. [root@control ansible]# ls ~/node3/etc/
    20. hostname  
    21. [root@control ansible]# ls ~/node4/etc/
    22. hostname
    23. 不能下载目录
    24. [root@control ansible]# ansible webservers -m fetch -a "src=/root/aaa dest=~/"
    25. node4 | FAILED! => {
    26.    "changed": false,
    27.    "file": "/root/aaa",
    28.    "msg": "remote file is a directory, fetch cannot work on directories"
    29. }
    30. node3 | FAILED! => {
    31.    "changed": false,
    32.    "file": "/root/aaa",
    33.    "msg": "remote file is a directory, fetch cannot work on directories"
    34. }
    lineinfile模块

    用于确保存目标文件中有某一行内容

    常用选项:

    path:待修改的文件路径

    line: 写入文件的一行内容

    regexp:正则表达式,用于查找文件中的内容

    1. test组的主机,/etc/issue中一定要有一行hello world。如果该行不存在,则默认添加到文件结尾
    2. [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='hello world'"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "backup": "",
    8.    "changed": true,
    9.    "msg": "line added"
    10. }
    11. [root@node1 ~]# cat /etc/issue
    12. \S
    13. Kernel \r on an \m
    14. hello world
    15. test组中的主机,把/etc/issue中有hello的行,替换成chi le ma
    16. [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='chi le ma' regexp=hello"
    17. node1 | CHANGED => {
    18.    "ansible_facts": {
    19.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    20.   },
    21.    "backup": "",
    22.    "changed": true,
    23.    "msg": "line replaced"
    24. }
    25. [root@node1 ~]# cat /etc/issue
    26. \S
    27. Kernel \r on an \m
    28. chi le ma
    29. [root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='chi le ma' regexp=hello"
    30. node1 | CHANGED => {
    31.    "ansible_facts": {
    32.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    33.   },
    34.    "backup": "",
    35.    "changed": true,
    36.    "msg": "line added"
    37. }
    38. [root@node1 ~]# cat /etc/issue
    39. \S
    40. Kernel \r on an \m
    41. chi le ma
    42. chi le ma
    replace模块

    lineinfile会替换一行,replace可以替换关键词

    常用选项:

    path:待修改的文件路径

    replace:将正则表达式查到的内容,替换成replace的内容

    regexp: 正则表达式,用于查找文件中的内容

    1. test组中的主机上/etc/issue文件中的chi,替换成he
    2. [root@node1 ~]# cat /etc/issue
    3. \S
    4. Kernel \r on an \m
    5. chi le ma
    6. chi le ma
    7. [root@control ansible]# ansible test -m replace -a "path=/etc/issue regexp=chi replace="he""
    8. node1 | CHANGED => {
    9.    "ansible_facts": {
    10.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    11.   },
    12.    "changed": true,
    13.    "msg": "2 replacements made"
    14. }
    15. [root@node1 ~]# cat /etc/issue
    16. \S
    17. Kernel \r on an \m
    18. he le ma
    19. he le ma
    文件操作综合练习

    1. [root@control ansible]# cat exec.sh
    2. #!/bin/bash
    3. ansible test -m file -a "path=/tmp/mydemo state=directory owner=adm group=adm mode=0777"
    4. ansible test -m copy -a "src=/etc/hosts dest=/tmp/mydemo owner=adm group=adm mode=0600"
    5. ansible test -m replace -a "path=/tmp/mydemo/hosts regexp='node5' replace='server5'"
    6. ansible test -m fetch -a "src=/tmp/mydemo/hosts dest=/root/ansible/"
    user模块

    实现linux用户管理

    常用选项:

    name: 待创建的用户名

    uid:用户ID

    group:设置主组

    groups:设置附加组

    home:设置家目录

    password:设置用户密码

    state:状态。present表示创建,它是默认选项。absent表示删除

    remove:删除家目录、邮箱等。值为yes或者true都可以

    1. test主机上创建个tom用户
    2. [root@control ansible]# ansible test -m user -a "user=tom"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "comment": "",
    9.    "create_home": true,
    10.    "group": 1000,
    11.    "home": "/home/tom",
    12.    "name": "tom",
    13.    "shell": "/bin/bash",
    14.    "state": "present",
    15.    "system": false,
    16.    "uid": 1000
    17. }
    18. [root@node1 ~]# cat /etc/passwd | grep ^tom
    19. tom:x:1000:1000::/home/tom:/bin/bash
    20. [root@control ansible]# ansible test -m user -a "name=jerry uid=1010 group=adm groups=daemon,root home=/home/jerry"
    21. 设置tom密码为123456,用sha512加密
    22. [root@control ansible]# ansible test -m user -a "name=tom password={{'123456'|password_hash('sha512')}}"
    23. node1 | CHANGED => {
    24.    "ansible_facts": {
    25.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    26.   },
    27.    "append": false,
    28.    "changed": true,
    29.    "comment": "",
    30.    "group": 1000,
    31.    "home": "/home/tom",
    32.    "move_home": false,
    33.    "name": "tom",
    34.    "password": "NOT_LOGGING_PASSWORD",
    35.    "shell": "/bin/bash",
    36.    "state": "present",
    37.    "uid": 1000
    38. }
    39. [root@node1 ~]# cat /etc/shadow | grep tom
    40. tom:$6$Dfyqw6ty.pPwnyZm$SRpTqqORuCbPFGcdPuT8sNHHmIpHJAslaoDgk1RCA6gIAEEeg9tvz8MBxj7mGR1j4LV7GVN.1teZQ7OUaP51J1:19670:0:99999:7:::
    41. 删除tom用户,不删除家目录
    42. [root@control ansible]# ansible test -m user -a "name=tom state=absent"
    43. node1 | CHANGED => {
    44.    "ansible_facts": {
    45.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    46.   },
    47.    "changed": true,
    48.    "force": false,
    49.    "name": "tom",
    50.    "remove": false,
    51.    "state": "absent"
    52. }
    53. 删除jerry用户,同时删除家目录
    54. [root@control ansible]# ansible test -m user -a "name=jerry state=absent remove=yes"
    55. node1 | CHANGED => {
    56.    "ansible_facts": {
    57.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    58.   },
    59.    "changed": true,
    60.    "force": false,
    61.    "name": "jerry",
    62.    "remove": true,
    63.    "state": "absent"
    64. }
    65. [root@node1 ~]# ls /home
    66. tom
    group模块

    创建、删除组

    常用选项:

    name:待创建的组名

    gid:组的ID号

    state:prensent表示创建、它是默认选项。absent是删除

    1. test组的主机上创建名为devops的组
    2. [root@control ansible]# ansible test -m group -a "name=devops state=present"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "gid": 1000,
    9.    "name": "devops",
    10.    "state": "present",
    11.    "system": false
    12. }
    13. test组的主机上删除名为devops的组
    14. [root@control ansible]# ansible test -m group -a "name=devops state=absent"
    15. node1 | CHANGED => {
    16.    "ansible_facts": {
    17.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    18.   },
    19.    "changed": true,
    20.    "name": "devops",
    21.    "state": "absent"
    22. }
    yum_repository

    用于配置yum

    常用选项:

    file:指定文件名

    其他选项,请于文件内容对照

    1. test组中的主机上,配置yum
    2. [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"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "repo": "App",
    9.    "state": "present"
    10. }
    11. [root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
    12. [App]
    13. baseurl = ftp://192.168.88.240/rhel8/AppStream
    14. enabled = 1
    15. gpgcheck = 0
    16. name = app
    17. 再次执行
    18. [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"
    19. node1 | CHANGED => {
    20.    "ansible_facts": {
    21.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    22.   },
    23.    "changed": true,
    24.    "repo": "BaseOs",
    25.    "state": "present"
    26. }
    27. [root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
    28. [App]
    29. baseurl = ftp://192.168.88.240/rhel8/AppStream
    30. enabled = 1
    31. gpgcheck = 0
    32. name = app
    33. [BaseOs]
    34. baseurl = ftp://192.168.88.240/rhel8/BaseOs
    35. enabled = 1
    36. gpgcheck = 0
    37. name = BaseOs
    yum模块

    用于rpm软件包管理,如安装、升级、卸载

    常用选项:

    name:包名

    state:状态。present表示安装,如果已安装则忽略;latest表示安装或升级到最新版本;absent表示卸载

    1. test组中的主机上安装wget
    2. [root@control ansible]# ansible test -m yum -a "name=wget state=present"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "msg": "",
    9.    "rc": 0,
    10.    "results": [
    11.        "Installed: wget",
    12.        "Installed: wget-1.19.5-8.el8_1.1.x86_64"
    13.   ]
    14. }
    15. [root@node1 ~]# yum -y install wget
    16. Updating Subscription Management repositories.
    17. Unable to read consumer identity
    18. This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    19. 上次元数据过期检查:0:00:59 前,执行于 2023年11月09日 星期四 17时16分34秒。
    20. 软件包 wget-1.19.5-8.el8_1.1.x86_64 已安装。
    21. 依赖关系解决。
    22. 无需任何处理。
    23. 完毕!
    24. test组中的主机上卸载wget
    25. [root@control ansible]# ansible test -m yum -a "name=wget state=absent"
    26. node1 | CHANGED => {
    27.    "ansible_facts": {
    28.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    29.   },
    30.    "changed": true,
    31.    "msg": "",
    32.    "rc": 0,
    33.    "results": [
    34.        "Removed: wget-1.19.5-8.el8_1.1.x86_64"
    35.   ]
    36. }
    service模块

    用于控制服务。启动、关闭、重启、开机自启

    常用选项:

    name:控制的服务名

    state: started表示启动 stopped表示关闭 restarted表示重启

    enabled: yes表示设置开机自启;no表示设置开启不启动

    1. test主机上安装httpd
    2. [root@control ansible]# ansible test -m yum -a "name=httpd state=latest"
    3. test主机上启动httpd,并设置它开机启动
    4. [root@control ansible]# ansible test -m service -a "name=httpd state=started enabled=yes"
    5. [root@node1 ~]# systemctl status httpd
    6. ● httpd.service - The Apache HTTP Server
    7.   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    8.   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

    lvg模块

    创建、删除卷组,修改卷组大小

    常用选项:

    vg:定义卷组名。vg: volume group

    pvs:由哪些物理卷构成。pvs:physical volumes

    1. 先手工进行gpt分区
    2. [root@node1 ~]# lsblk
    3. NAME         MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    4. sr0            11:0    1  7.9G  0 rom  
    5. nvme0n1       259:0    0   20G  0 disk
    6. ├─nvme0n1p1   259:1    0   1G  0 part /boot
    7. └─nvme0n1p2   259:2    0   19G  0 part
    8. ├─rhel-root 253:0    0   17G  0 lvm /
    9. └─rhel-swap 253:1    0   2G  0 lvm [SWAP]
    10. nvme0n2       259:3    0   20G  0 disk
    11. [root@node1 ~]# fdisk /dev/nvme0n2
    12. 欢迎使用 fdisk (util-linux 2.32.1)。
    13. 更改将停留在内存中,直到您决定将更改写入磁盘。
    14. 使用写入命令前请三思。
    15. 设备不包含可识别的分区表。
    16. 创建了一个磁盘标识符为 0xdf955d41 的新 DOS 磁盘标签。
    17. 命令(输入 m 获取帮助):m
    18. 帮助:
    19. DOS (MBR)
    20.   a   开关 可启动 标志
    21.   b   编辑嵌套的 BSD 磁盘标签
    22.   c   开关 dos 兼容性标志
    23. 常规
    24.   d   删除分区
    25.   F   列出未分区的空闲区
    26.   l   列出已知分区类型
    27.   n   添加新分区
    28.   p   打印分区表
    29.   t   更改分区类型
    30.   v   检查分区表
    31.   i   打印某个分区的相关信息
    32. 杂项
    33.   m   打印此菜单
    34.   u   更改 显示/记录 单位
    35.   x   更多功能(仅限专业人员)
    36. 脚本
    37.   I   从 sfdisk 脚本文件加载磁盘布局
    38.   O   将磁盘布局转储为 sfdisk 脚本文件
    39. 保存并退出
    40.   w   将分区表写入磁盘并退出
    41.   q   退出而不保存更改
    42. 新建空磁盘标签
    43.   g   新建一份 GPT 分区表
    44.   G   新建一份空 GPT (IRIX) 分区表
    45.   o   新建一份的空 DOS 分区表
    46.   s   新建一份空 Sun 分区表
    47. 命令(输入 m 获取帮助):g 创建GPT分区表
    48. 已创建新的 GPT 磁盘标签(GUID: 66C691FD-9290-5A40-A7FE-7140003BB76B)。
    49. 命令(输入 m 获取帮助):n 新建分区
    50. 分区号 (1-128, 默认  1): 回车,使用1号分区
    51. 第一个扇区 (2048-41943006, 默认 2048): 起始位置,回车
    52. 上个扇区,+sectors 或 +size{K,M,G,T,P} (2048-41943006, 默认 41943006): +5G 结束位置+5G
    53. 创建了一个新分区 1,类型为“Linux filesystem”,大小为 5 GiB。
    54. 命令(输入 m 获取帮助):n 新建分区
    55. 分区号 (2-128, 默认  2): 回车 使用2号分区
    56. 第一个扇区 (10487808-41943006, 默认 10487808): 起始位置,回车  
    57. 上个扇区,+sectors 或 +size{K,M,G,T,P} (10487808-41943006, 默认 41943006):  
    58. 创建了一个新分区 2,类型为“Linux filesystem”,大小为 15 GiB。
    59. 命令(输入 m 获取帮助):p
    60. Disk /dev/nvme0n2:20 GiB,21474836480 字节,41943040 个扇区
    61. 单元:扇区 / 1 * 512 = 512 字节
    62. 扇区大小(逻辑/物理):512 字节 / 512 字节
    63. I/O 大小(最小/最佳):512 字节 / 512 字节
    64. 磁盘标签类型:gpt
    65. 磁盘标识符:66C691FD-9290-5A40-A7FE-7140003BB76B
    66. 设备               起点     末尾     扇区 大小 类型
    67. /dev/nvme0n2p1     2048 10487807 10485760   5G Linux 文件系统
    68. /dev/nvme0n2p2 10487808 41943006 31455199 15G Linux 文件系统
    69. 命令(输入 m 获取帮助):w
    70. 分区表已调整。
    71. 将调用 ioctl() 来重新读分区表。
    72. 正在同步磁盘。
    73. [root@node1 ~]# lsblk
    74. NAME         MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    75. sr0            11:0    1  7.9G  0 rom  
    76. nvme0n1       259:0    0   20G  0 disk
    77. ├─nvme0n1p1   259:1    0   1G  0 part /boot
    78. └─nvme0n1p2   259:2    0   19G  0 part
    79. ├─rhel-root 253:0    0   17G  0 lvm /
    80. └─rhel-swap 253:1    0   2G  0 lvm [SWAP]
    81. nvme0n2       259:3    0   20G  0 disk
    82. ├─nvme0n2p1   259:4    0   5G  0 part
    83. └─nvme0n2p2   259:5    0   15G  0 part
    84. test组中的主机上创建名为myvg的卷组,该卷组由/dev/nvme0n2p1组成
    85. [root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/nvme0n2p1"
    86. node1 | CHANGED => {
    87.    "ansible_facts": {
    88.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    89.   },
    90.    "changed": true
    91. }
    92. 在node1上查看卷组
    93. [root@node1 ~]# vgs
    94. VG   #PV #LV #SN Attr   VSize   VFree
    95. myvg   1   0   0 wz--n- <5.00g <5.00g
    96. rhel   1   2   0 wz--n- <19.00g     0
    97. 扩容卷组。卷组由PV构成,只要向卷组中加入新的PV,即可实现扩容
    98. [root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/nvme0n2p1,/dev/nvme0n2p2"
    99. node1 | CHANGED => {
    100.    "ansible_facts": {
    101.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    102.   },
    103.    "changed": true
    104. }
    105. [root@node1 ~]# vgs
    106. VG   #PV #LV #SN Attr   VSize   VFree
    107. myvg   2   0   0 wz--n-  19.99g 19.99g
    108. rhel   1   2   0 wz--n- <19.00g     0
    lvol模块

    创建、删除逻辑卷,修改逻辑卷大小

    常用选项

    vg:指定在哪个卷组上创建逻辑卷

    lv:创建的逻辑卷名。lv:logical volume

    size:逻辑卷的大小,不写单位,以M为单位

    1. test组中的主机上创建名为mylv的逻辑卷,大小为2G
    2. [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=2G"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "msg": ""
    9. }
    10. 在node1查看逻辑卷
    11. [root@node1 ~]# lvs
    12. LV   VG   Attr       LSize   Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    13. mylv myvg -wi-a-----   2.00g                                                    
    14. root rhel -wi-ao---- <17.00g                                                    
    15. swap rhel -wi-ao----   2.00g    
    16. mylv扩容至4GB
    17. [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=4G"
    18. node1 | CHANGED => {
    19.    "ansible_facts": {
    20.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    21.   },
    22.    "changed": true,
    23.    "lv": "mylv",
    24.    "size": 2.0,
    25.    "vg": "myvg"
    26. }
    27. [root@node1 ~]# lvs
    28. LV   VG   Attr       LSize   Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    29. mylv myvg -wi-a-----   4.00g
    filesystem模块

    格式化逻辑卷

    常用选项:

    fstype: 指定文件系统类型

    dev:指定要格式化的设备,可以是分区,可以是逻辑卷

    1. test组中的主机上,把/dev/myvg/mylv格式化为xfs
    2. [root@control ansible]# ansible test -m filesystem -a "dev=/dev/myvg/mylv fstype=xfs"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true
    8. }
    9. 在node1上查看格式化结果
    10. [root@node1 ~]# blkid /dev/myvg/mylv
    11. /dev/myvg/mylv: UUID="7d0f9e53-bbd8-4da3-9bd5-85ae90fc290b" TYPE="xfs"
    mount模块

    用于挂载文件系统

    常用选项:

    path:挂载点。如果挂载点不存在,自动创建。

    src:待挂载的设备

    fstype:文件系统类型

    state: mounted,表示永久挂载

    1. test组中的主机上,把/dev/myvg/mylv永久挂在到/data
    2. [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv fstype=xfs state=mounted"
    3. node1 | CHANGED => {
    4.    "ansible_facts": {
    5.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    6.   },
    7.    "changed": true,
    8.    "dump": "0",
    9.    "fstab": "/etc/fstab",
    10.    "fstype": "xfs",
    11.    "name": "/data",
    12.    "opts": "defaults",
    13.    "passno": "0",
    14.    "src": "/dev/myvg/mylv"
    15. }
    16. 在node1上查看
    17. [root@node1 ~]# cat /etc/fstab
    18. /dev/mapper/rhel-root   /                       xfs     defaults        0 0
    19. UUID=cb52237f-a2b2-423c-9d18-66892297474c /boot                   xfs     defaults        0 0
    20. /dev/mapper/rhel-swap   swap                   swap   defaults        0 0
    21. /dev/myvg/mylv /data xfs defaults 0 0
    22. 对逻辑卷mylv进行扩容到5G
    23. ansible test -m lvol -a "vg=myvg lv=mylv size=5G"
    24. [root@node1 ~]# df -h
    25. 文件系统               容量 已用 可用 已用% 挂载点
    26. /dev/mapper/myvg-mylv  4.0G   61M  4.0G    2% /data
    27. 但挂载点不会更新为5G
    28. 扩容时应该加上resizefs参数
    29. [root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=7G resizefs=yes"
    30. node1 | CHANGED => {
    31.    "ansible_facts": {
    32.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    33.   },
    34.    "changed": true,
    35.    "lv": "mylv",
    36.    "size": 6.0,
    37.    "vg": "myvg"
    38. }
    39. [root@node1 ~]# df -h |grep data
    40. /dev/mapper/myvg-mylv  7.0G   83M  7.0G    2% /data
    41. 卸载
    42. [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv fstype=xfs state=mounted"
    43. [root@node1 ~]# df -h |grep data
    44. 重新挂载
    45. [root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv state=mounted fstype=xfs"
    46. node1 | CHANGED => {
    47.    "ansible_facts": {
    48.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    49.   },
    50.    "changed": true,
    51.    "dump": "0",
    52.    "fstab": "/etc/fstab",
    53.    "fstype": "xfs",
    54.    "name": "/data",
    55.    "opts": "defaults",
    56.    "passno": "0",
    57.    "src": "/dev/myvg/mylv"
    58. }
    59. [root@node1 ~]# df -h |grep data
    60. /dev/mapper/myvg-mylv  7.0G   83M  7.0G    2% /data
    61. 永久卸载
    62. [root@control ansible]# ansible test -m mount -a "path=/data state=absent"
    63. [root@node1 ~]# cat /etc/fstab |grep data
    64. [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 
  • 相关阅读:
    Linux入门之使用 dmesg 查看引导日志
    GEE|时间序列分析(二)
    自动化运维:Ansible基础与命令行模块操作
    kafka 文件存储 消息同步机制
    什么?你还不知道ERD Online要干什么
    .NET周报 【5月第3期 2023-05-21】
    安装Jenkins
    LeetCode刷题(python版)——Topic65.有效数字
    4520. 质数
    固话号码认证有什么好处?固话号码认证有什么作用?
  • 原文地址:https://blog.csdn.net/weixin_65562581/article/details/134321115