• Ansible-常用模块


    1.ansible实现管理的方式
    Ad-Hoc            ##利用ansible命令直接完成管理,主要用于临时命令使用场景
    playbook          ##ansible脚本,主要用于大型项目场景,需要前期的规划

    2.Ad-Hoc执行方式中如何获得帮助
    ansible-doc      ##显示模块帮助的指令
    2.1 格式
    ansible-doc [参数] [模块...]
    2.2 常用参数
    -l            ##列出可用模块
    -s           ##显示指定模块的playbook片段

    1. [root@ansible ~]# ansible-doc -l | wc -l
    2. 3387
    3. [root@ansible ~]# ansible-doc -s shell
    4. - name: Execute shell commands on targets
    5. shell:
    6. chdir: # Change into this directory before running the command.
    7. cmd: # The command to run followed by optional arguments.
    8. creates: # A filename, when it already exists, this step will *not* be run.
    9. executable: # Change the shell used to execute the command. This expects an absolute path to the executable.
    10. free_form: # The shell module takes a free form command to run, as a string. There is no actual parameter named 'free form'. See
    11. the examples on how to use this module.
    12. removes: # A filename, when it does not exist, this step will *not* be run.
    13. stdin: # Set the stdin of the command directly to the specified value.
    14. stdin_add_newline: # Whether to append a newline to stdin data.
    15. warn: # Whether to enable task warnings.
    16. [root@ansible ~]#

    3.ansible命令运行方式及常用参数
    3.1 格式:
    ansible 清单    -m 模块   -a 模块参数

    3.2 常用参数

    1. --version ##显示版本
    2. -m module ##指定模块,默认为command模块
    3. --list ##显示主机列表,也可以用--list-hosts
    4. -v ##详细过程 -vv -vvv更详细过程
    5. -k ##提示输入ssh连接密码,默认key认证
    6. -C ##预执行检测
    7. -T ##执行命令的超时时间,默认10s#
    8. -u ##指定远程执行的用户
    9. -b ##执行sudo切换身份操作
    10. -become-user=USERNAME ##指定sudo的用户
    11. -K ##提示输入sudo密码

    4.ansible的基本颜色代表信
    绿色         ##执行成功但为对远程主机做任何改变
    黄色         ##执行成功并对远程主机做改变
    红色         ##执行失败

    5.ansible中的常用模块

    5.1 command

    注意:Linux中的很多通配符在command模块中不支持

    功能: 在远程主机执行命令,此模块为默认模块

    常用参数:

    chdir##执行命令前先进入到指定目录
    cmd##运行命令指定
    creates##如果文件存在将不运行
    removes##如果文件存在将运行
    free_form##在远程主机中执行的命令,此参数不需要加

    1. 在westos清单主机中建立用户lee
    2. [admin@ansible .ansible]$ ansible westos -m command -a "useradd lee" -u root -k
    3. SSH password:
    4. 172.25.32.12 | CHANGED | rc=0 >>
    5. 172.25.32.11 | CHANGED | rc=0 >>
    6. 在westos清单主机中删除用户lee
    7. [admin@ansible .ansible]$ ansible westos -m command -a "userdel lee" -u root -k
    8. SSH password:
    9. 172.25.32.12 | CHANGED | rc=0 >>
    10. 172.25.32.11 | CHANGED | rc=0 >>
    11. 查看westos清单主机中/etc/passwd/的最后一行
    12. [admin@ansible .ansible]$ ansible westos -m command -a "chdir=/etc tail -n1 passwd" -u root -k
    13. SSH password:
    14. 172.25.32.12 | CHANGED | rc=0 >>
    15. admin:x:1000:1000::/home/admin:/bin/bash
    16. 172.25.32.11 | CHANGED | rc=0 >>
    17. admin:x:1000:1000::/home/admin:/bin/bash
    18. 在westos清单主机中如果/etc/passwd存在的话就不运行tail命令,如果不文件存在就运行tail
    19. [admin@ansible .ansible]$ ansible westos -m command -a "chdir=/etc creates=/etc/passwd tail -n1 passwd" -u root -k
    20. SSH password:
    21. 172.25.32.12 | SUCCESS | rc=0 >>
    22. skipped, since /etc/passwd exists
    23. 172.25.32.11 | SUCCESS | rc=0 >>
    24. skipped, since /etc/passwd exists
    25. 在westos清单主机中如果/etc/passwd存在的话就运行tail命令,如果文件不存在就不运行tail
    26. [admin@ansible .ansible]$ ansible westos -m command -a "chdir=/etc removes=/etc/passwd tail -n1 passwd" -u root -k
    27. SSH password:
    28. 172.25.32.12 | CHANGED | rc=0 >>
    29. admin:x:1000:1000::/home/admin:/bin/bash
    30. 172.25.32.11 | CHANGED | rc=0 >>
    31. admin:x:1000:1000::/home/admin:/bin/bash

    5.2 shell

    功能: 和command功能类似

    常用参数:

    chdir##执行命令前先进入到指定目录
    cmd##运行命令指定
    creates##如果文件存在将不运行
    removes##如果文件存在在将运行
    free_form##在远程主机中执行的命令,此参数不需要加
    executable##指定执行环境,默认为sh

    1. 指定执行环境为/bin/bash,默认为sh
    2. [admin@ansible .ansible]$ ansible westos -m shell -a "executable=sh ps ax | grep $$ " -k
    3. SSH password:
    4. 172.25.32.11 | CHANGED | rc=0 >>
    5. 4628 pts/1 S+ 0:00 sh -c ps ax | grep 3496
    6. 4630 pts/1 S+ 0:00 grep 3496
    7. 172.25.32.12 | CHANGED | rc=0 >>
    8. 4656 pts/1 S+ 0:00 sh -c ps ax | grep 3496
    9. 4658 pts/1 S+ 0:00 grep 3496
    10. 查看当前目录所在的进程
    11. [admin@ansible .ansible]$ ansible westos -m shell -a ' ps ax | grep $$'
    12. 172.25.32.12 | CHANGED | rc=0 >>
    13. 4765 pts/1 S+ 0:00 /bin/sh -c ps ax | grep $$
    14. 4767 pts/1 S+ 0:00 grep 4765
    15. 172.25.32.11 | CHANGED | rc=0 >>
    16. 4737 pts/1 S+ 0:00 /bin/sh -c ps ax | grep $$
    17. 4739 pts/1 S+ 0:00 grep 4737
    18. 查看当前正在运行的进程
    19. [admin@ansible .ansible]$ ansible westos -m shell -a 'ps'
    20. 172.25.32.12 | CHANGED | rc=0 >>
    21. PID TTY TIME CMD
    22. 4864 pts/1 00:00:00 sudo
    23. 4865 pts/1 00:00:00 sh
    24. 4866 pts/1 00:00:00 python
    25. 4867 pts/1 00:00:00 ps
    26. 172.25.32.11 | CHANGED | rc=0 >>
    27. PID TTY TIME CMD
    28. 4835 pts/1 00:00:00 sudo
    29. 4836 pts/1 00:00:00 sh
    30. 4837 pts/1 00:00:00 python
    31. 4838 pts/1 00:00:00 ps

    5.3 script

    功能: 在ansible主机中写好的脚本在受控主机中执行

    1. [admin@ansible .ansible]$ exit ##回到超级用户中
    2. logout
    3. [root@ansible ~]# vim /mnt/westos.sh
    4. [root@ansible ~]# cat /mnt/westos.sh
    5. #!/bin/bash
    6. echo $HOSTNAME
    7. [root@ansible ~]# ansible westos -m script -a "/mnt/westos.sh" -k
    8. SSH password:
    9. 172.25.32.12 | CHANGED => {
    10. "changed": true,
    11. "rc": 0,
    12. "stderr": "Shared connection to 172.25.32.12 closed.\r\n",
    13. "stderr_lines": [
    14. "Shared connection to 172.25.32.12 closed."
    15. ],
    16. "stdout": "node2\r\n",
    17. "stdout_lines": [
    18. "node2"
    19. ]
    20. }
    21. 172.25.32.11 | CHANGED => {
    22. "changed": true,
    23. "rc": 0,
    24. "stderr": "Shared connection to 172.25.32.11 closed.\r\n",
    25. "stderr_lines": [
    26. "Shared connection to 172.25.32.11 closed."
    27. ],
    28. "stdout": "node1\r\n",
    29. "stdout_lines": [
    30. "node1"
    31. ]
    32. }

    5.4 copy

    功能:从ansible主机复制文件到受控主机

    常用参数

    src##源文件
    dest##目的地文件
    owner/group##指定目的地文件所有人
    mode##指定目的地文件权限
    backup=yes##当受控主机中存在文件时备份原文件
    content##指定文本内容直接在受控主机中生成文件

    1. 将/mnt/westos.sh/复制到westos清单被控主机的/mnt/中,当被控主机中存在westos.sh时备份原文件,文件所有人为admin,权限为777
    2. [admin@ansible .ansible]$ ansible westos -m copy -a "src=/mnt/westos.sh dest=/mnt/westos.sh owner=admin mode=777 backup=yes"
    3. 172.25.32.11 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "checksum": "25a5e82036293f48d4a117c91855a16c2d36e0de",
    9. "dest": "/mnt/westos.sh",
    10. "gid": 0,
    11. "group": "root",
    12. "md5sum": "2b9854338cd858ad0f86eb55423c3f03",
    13. "mode": "0777",
    14. "owner": "admin",
    15. "size": 27,
    16. "src": "/home/admin/.ansible/tmp/ansible-tmp-1659037428.57-4624-78249132552260/source",
    17. "state": "file",
    18. "uid": 1000
    19. }
    20. 172.25.32.12 | CHANGED => {
    21. "ansible_facts": {
    22. "discovered_interpreter_python": "/usr/bin/python"
    23. },
    24. "changed": true,
    25. "checksum": "25a5e82036293f48d4a117c91855a16c2d36e0de",
    26. "dest": "/mnt/westos.sh",
    27. "gid": 0,
    28. "group": "root",
    29. "md5sum": "2b9854338cd858ad0f86eb55423c3f03",
    30. "mode": "0777",
    31. "owner": "admin",
    32. "size": 27,
    33. "src": "/home/admin/.ansible/tmp/ansible-tmp-1659037428.58-4626-250519756246854/source",
    34. "state": "file",
    35. "uid": 1000
    36. }
    1. 在westos清单被控主机的/mnt/目录下生成文件westosfile1,文件内容为hello westos/hello linux,文件所有人为admin,权限为777
    2. [admin@ansible .ansible]$ ansible westos -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=admin mode=600"
    3. 172.25.32.11 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    9. "dest": "/mnt/westosfile1",
    10. "gid": 0,
    11. "group": "root",
    12. "mode": "0600",
    13. "owner": "admin",
    14. "path": "/mnt/westosfile1",
    15. "size": 25,
    16. "state": "file",
    17. "uid": 1000
    18. }
    19. 172.25.32.12 | CHANGED => {
    20. "ansible_facts": {
    21. "discovered_interpreter_python": "/usr/bin/python"
    22. },
    23. "changed": true,
    24. "checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    25. "dest": "/mnt/westosfile1",
    26. "gid": 0,
    27. "group": "root",
    28. "mode": "0600",
    29. "owner": "admin",
    30. "path": "/mnt/westosfile1",
    31. "size": 25,
    32. "state": "file",
    33. "uid": 1000
    34. }
    1. 查看westos清单被控主机的/mnt/目录
    2. [admin@ansible .ansible]$ ansible westos -m shell -a "ls /mnt"
    3. 172.25.32.11 | CHANGED | rc=0 >>
    4. westos.sh
    5. westosfile1
    6. 172.25.32.12 | CHANGED | rc=0 >>
    7. westos.sh
    8. westosfile1
    9. [admin@ansible .ansible]$ ansible westos -m shell -a "cat /mnt/westosfile1"
    10. 172.25.32.11 | CHANGED | rc=0 >>
    11. hello westos
    12. hello linux
    13. 172.25.32.12 | CHANGED | rc=0 >>
    14. hello westos
    15. hello linux

    5.5 fetch

    功能:  从受控主机把文件复制到ansible主机,但不支持目录

    常用参数

    src##受控主机的源文件
    dest##本机目录
    flat##基本名称功能

    1. 将受控主机/mnt/westosfile1复制到主机的/mnt/目录下
    2. [root@ansible mnt]# ansible 172.25.32.11 -m fetch -a "src=/mnt/westosfile1 dest=/mnt" -k
    3. SSH password:
    4. 172.25.32.11 | CHANGED => {
    5. "changed": true,
    6. "checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    7. "dest": "/mnt/172.25.32.11/mnt/westosfile1",
    8. "md5sum": "e79f6eb05e162f95e496e8d4d8a24275",
    9. "remote_checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    10. "remote_md5sum": null
    11. }
    1. 将受控主机复制到主机文件名字改为file
    2. [root@ansible mnt]# ansible 172.25.32.11 -m fetch -a "src=/mnt/westosfile1 dest=/mnt/file flat=yes" -k
    3. SSH password:
    4. 172.25.32.11 | CHANGED => {
    5. "changed": true,
    6. "checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    7. "dest": "/mnt/file",
    8. "md5sum": "e79f6eb05e162f95e496e8d4d8a24275",
    9. "remote_checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    10. "remote_md5sum": null
    11. }

    5.6 file

    功能: 设置文件的属性

    常用参数

    path指定文件名称
    state指定操作状态

    touch

    absent

    directory

    link

    hard

    建立

    删除

    递归

    建立软链接

    建立硬连接

    mode设定权限
    group/owner设定文件组/设定文件用户
    src源文件
    dest目标文件
    recurse=yes递归更改

    1. 建立文件
    2. [admin@ansible .ansible]$ ansible westos -m file -a 'path=/mnt/test.sh state=touch'
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "dest": "/mnt/test.sh",
    9. "gid": 0,
    10. "group": "root",
    11. "mode": "0644",
    12. "owner": "root",
    13. "size": 0,
    14. "state": "file",
    15. "uid": 0
    16. }
    17. 删除文件
    18. [admin@ansible .ansible]$ ansible westos -m file -a 'path=/mnt/test.sh state=absent'
    19. 172.25.32.12 | CHANGED => {
    20. "ansible_facts": {
    21. "discovered_interpreter_python": "/usr/bin/python"
    22. },
    23. "changed": true,
    24. "path": "/mnt/test.sh",
    25. "state": "absent"
    26. }
    27. 建立目录
    28. [admin@ansible .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory'
    29. 172.25.32.11 | CHANGED => {
    30. "ansible_facts": {
    31. "discovered_interpreter_python": "/usr/bin/python"
    32. },
    33. "changed": true,
    34. "gid": 0,
    35. "group": "root",
    36. "mode": "0755",
    37. "owner": "root",
    38. "path": "/mnt/westos",
    39. "size": 6,
    40. "state": "directory",
    41. "uid": 0
    42. }
    43. 递归修改目录权限
    44. [admin@ansible .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
    45. 172.25.32.12 | CHANGED => {
    46. "ansible_facts": {
    47. "discovered_interpreter_python": "/usr/bin/python"
    48. },
    49. "changed": true,
    50. "gid": 0,
    51. "group": "root",
    52. "mode": "0777",
    53. "owner": "root",
    54. "path": "/mnt/westos",
    55. "size": 6,
    56. "state": "directory",
    57. "uid": 0
    58. }
    59. 生成软链接
    60. [admin@ansible .ansible]$ ansible westos -m file -a 'src=/mnt/westosfile1 dest=/mnt/westos state=link'
    61. 172.25.32.12 | CHANGED => {
    62. "ansible_facts": {
    63. "discovered_interpreter_python": "/usr/bin/python"
    64. },
    65. "changed": true,
    66. "dest": "/mnt/westos",
    67. "gid": 0,
    68. "group": "root",
    69. "mode": "0777",
    70. "owner": "root",
    71. "size": 16,
    72. "src": "/mnt/westosfile1",
    73. "state": "link",
    74. "uid": 0
    75. }
    76. 生成硬连接
    77. [admin@ansible .ansible]$ ansible westos -m file -a 'src=/mnt/westosfile1 dest=/mnt/westos1 state=hard'
    78. 172.25.32.12 | CHANGED => {
    79. "ansible_facts": {
    80. "discovered_interpreter_python": "/usr/bin/python"
    81. },
    82. "changed": true,
    83. "dest": "/mnt/westos1",
    84. "gid": 0,
    85. "group": "root",
    86. "mode": "0600",
    87. "owner": "admin",
    88. "size": 25,
    89. "src": "/mnt/westosfile1",
    90. "state": "hard",
    91. "uid": 1000
    92. }
    93. 建立文件时设置权限及所有人,所有组
    94. [admin@ansible .ansible]$ ansible westos -m file -a 'path=/mnt/file state=touch owner=admin group=admin mode=777'
    95. 172.25.32.12 | CHANGED => {
    96. "ansible_facts": {
    97. "discovered_interpreter_python": "/usr/bin/python"
    98. },
    99. "changed": true,
    100. "dest": "/mnt/file",
    101. "gid": 1000,
    102. "group": "admin",
    103. "mode": "0777",
    104. "owner": "admin",
    105. "size": 0,
    106. "state": "file",
    107. "uid": 1000
    108. }

    5.7 archive

    作用: 压缩

    常用参数

    path打包目录名称
    path声称打包文件名称
    format打包格式
    owner指定文件所属人
    mode指定文件权限

    1. [admin@ansible .ansible]$ ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=admin mode=700' -k
    2. SSH password:
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "archived": [
    8. "/etc/fstab",
    9. "/etc/crypttab",
    10. "/etc/mtab",
    11. "/etc/resolv.conf",
    12. "/etc/my.cnf",
    13. "/etc/issue",
    14. "/etc/issue.net",
    15. "/etc/libuser.conf",
    16. ........

    5.8 unarchive

    功能:解压缩

    常用参数

    copy

    默认为yes 从ansible主机复制文件到受控主机

    设定为no 从受控主机中寻找src源文件

    remote_src

    功能同copy且相反

    设定为yes 表示包在受控主机

    设定为no表示包在ansible主机

    src包路径,可以使ansible主机也可以使受控主机
    dest受控主机目录
    mode加压后文件权限

    1. ansible westos -m unarchive -a 'src=/opt/etc.tar.gz dest=/mnt owner=admin' #把主控机中/opt/etc.tar.gz解压到受控机/mnt里,解压后所有人是admin
    2. 把受控机中/opt/etc.tar.gz解压到受控机/mnt里,copy=no等同于remote_src=yes
    3. [admin@ansible .ansible]$ ansible westos -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"
    4. 172.25.32.11 | CHANGED => {
    5. "ansible_facts": {
    6. "discovered_interpreter_python": "/usr/bin/python"
    7. },
    8. "changed": true,
    9. "dest": "/mnt",
    10. "extract_results": {
    11. "cmd": [
    12. "/bin/gtar",
    13. "--extract",
    14. "-C",
    15. "/mnt",
    16. "-z",
    17. "-f",
    18. "/opt/etc.tar.gz"
    19. ],
    20. "err": "",
    21. "out": "",
    22. "rc": 0
    23. },
    24. "gid": 0,
    25. "group": "root",
    26. "handler": "TgzArchive",
    27. "mode": "0755",
    28. "owner": "root",
    29. "size": 112,
    30. "src": "/opt/etc.tar.gz",
    31. "state": "directory",
    32. "uid": 0
    33. }

    5.9 hostname

    作用: 管理主机名称

    常用参数:name        ##指定主机名称

    1. [admin@ansible .ansible]$ ansible 172.25.32.11 -m hostname -a 'name=www.westos.org'
    2. 172.25.32.11 | CHANGED => {
    3. "ansible_facts": {
    4. "ansible_domain": "westos.org",
    5. "ansible_fqdn": "www.westos.org",
    6. "ansible_hostname": "www",
    7. "ansible_nodename": "www.westos.org",
    8. "discovered_interpreter_python": "/usr/bin/python"
    9. },
    10. "changed": true,
    11. "name": "www.westos.org"
    12. }

    5.10 cron

    作用:计划任务

    常用参数

    minute##分钟
    hour##小时
    day##天
    month##月
    weekday##周
    name##任务名称
    job##任务脚本或命令
    disabled

    ##yes 禁用计划任务

    ##no 启动计划任务

    state##absent 删除计划任务

    1. 11:11分的时候在/mnt目录建立linux文件
    2. [admin@ansible .ansible]$ ansible westos -m cron -a 'job="touch /mnt/linux" name=test minute=11 hour=11 '
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "envs": [],
    9. "jobs": [
    10. "test"
    11. ]
    12. }
    13. 172.25.32.11 | CHANGED => {
    14. "ansible_facts": {
    15. "discovered_interpreter_python": "/usr/bin/python"
    16. },
    17. "changed": true,
    18. "envs": [],
    19. "jobs": [
    20. "test"
    21. ]
    22. }
    23. 禁止执行这个11:11分的时候在/mnt目录建立linux文件的任务
    24. [admin@ansible .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=11 hour=11 disabled=yes'
    25. 172.25.32.12 | CHANGED => {
    26. "ansible_facts": {
    27. "discovered_interpreter_python": "/usr/bin/python"
    28. },
    29. "changed": true,
    30. "envs": [],
    31. "jobs": [
    32. "test"
    33. ]
    34. }
    35. 172.25.32.11 | CHANGED => {
    36. "ansible_facts": {
    37. "discovered_interpreter_python": "/usr/bin/python"
    38. },
    39. 删除这个11:11分的时候在/mnt目录建立linux文件的任务
    40. [admin@ansible .ansible]$ ansible westos -m cron -a 'job="touch /mnt/linux" name=test minute=11 hour=11 state=absent'
    41. 172.25.32.12 | CHANGED => {
    42. "ansible_facts": {
    43. "discovered_interpreter_python": "/usr/bin/python"
    44. },
    45. "changed": true,
    46. "envs": [],
    47. "jobs": []
    48. }
    49. 172.25.32.11 | CHANGED => {
    50. "ansible_facts": {
    51. "discovered_interpreter_python": "/usr/bin/python"
    52. },
    53. "changed": true,
    54. "envs": [],
    55. "jobs": []
    56. }
    57. "changed": true,
    58. "envs": [],
    59. "jobs": [
    60. "test"
    61. ]
    62. }

    5.11 yum_repository

    作用:配置系统软件仓库源文件

    name##指定仓库名称
    baseurl##指定源路径
    description##指定仓库描述
    file##指定仓库文件名称
    enabled##仓库是否启用
    gpgcheck##仓库是否检测gpgkey
    state##默认值present建立/#absent 为删除

    1. 建立软件仓库源
    2. [admin@ansible .ansible]$ ansible westos -m yum_repository -a "name=AppStream baseurl=http://172.25.32.250/rhel7.6/AppStream description=AppStream gpgcheck=no file=westos" -k
    3. SSH password:
    4. 172.25.32.12 | CHANGED => {
    5. "ansible_facts": {
    6. "discovered_interpreter_python": "/usr/bin/python"
    7. },
    8. "changed": true,
    9. "repo": "AppStream",
    10. "state": "present"
    11. }
    12. 172.25.32.11 | CHANGED => {
    13. "ansible_facts": {
    14. "discovered_interpreter_python": "/usr/bin/python"
    15. },
    16. "changed": true,
    17. "repo": "AppStream",
    18. "state": "present"
    19. }
    20. 删除建立的软件仓库源
    21. [admin@ansible .ansible]$ ansible westos -m yum_repository -a "name=AppStream file=westos_test state=absent" -k
    22. SSH password:
    23. 172.25.32.12 | CHANGED => {
    24. "ansible_facts": {
    25. "discovered_interpreter_python": "/usr/bin/python"
    26. },
    27. "changed": true,
    28. "repo": "AppStream",
    29. "state": "absent"
    30. }
    31. 172.25.32.11 | CHANGED => {
    32. "ansible_facts": {
    33. "discovered_interpreter_python": "/usr/bin/python"
    34. },
    35. "changed": true,
    36. "repo": "AppStream",
    37. "state": "absent"
    38. }

    5.11 yum

    作用: 管理系统中的dnf仓库及管理软件

    name##指定包
    state

    ##指定动作

    #present                  安装

    #latest                     更新

    #absent                   删除

    list##列出指定信息

    disable_gpg_check#禁用gpgkey检测
    enablerepo##指定安装包来源
    disablerepo##禁用安装包来源

    1. 给被控机安装httpd服务
    2. [admin@ansible .ansible]$ ansible westos -m yum -a "name=httpd state=present"
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "changes": {
    9. "installed": [
    10. "httpd"
    11. ]
    12. },
    13. "msg": "",
    14. "rc": 0,
    15. "results": [
    16. "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-88.el7 for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-88.el7.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-88.el7 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7 AppStream 1.2 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7_4.1 AppStream 103 k\n apr-util x86_64 1.5.2-6.el7 AppStream 92 k\n httpd-tools x86_64 2.4.6-88.el7 AppStream 90 k\n mailcap noarch 2.1.41-2.el7 AppStream 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 1.5 M\nInstalled size: 4.3 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 33 MB/s | 1.5 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1.4.8-3.el7_4.1.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_64 2/5 \n Installing : httpd-tools-2.4.6-88.el7.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-88.el7.x86_64 5/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 1/5 \n Verifying : httpd-2.4.6-88.el7.x86_64 2/5 \n Verifying : apr-1.4.8-3.el7_4.1.x86_64 3/5 \n Verifying : apr-util-1.5.2-6.el7.x86_64 4/5 \n Verifying : httpd-tools-2.4.6-88.el7.x86_64 5/5 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7 \n\nDependency Installed:\n apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4.6-88.el7 mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
    17. ]
    18. }
    19. 卸载httpd服务,但不删除依赖关系
    20. [admin@ansible .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=no'
    21. 172.25.32.12 | CHANGED => {
    22. "ansible_facts": {
    23. "discovered_interpreter_python": "/usr/bin/python"
    24. },
    25. "changed": true,
    26. "changes": {
    27. "removed": [
    28. "httpd"
    29. ]
    30. },
    31. "msg": "",
    32. "rc": 0,
    33. "results": [
    34. "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.6-88.el7 @AppStream 3.7 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 3.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.6-88.el7.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.4.6-88.el7 \n\nComplete!\n"
    35. ]
    36. }
    37. 卸载httpd服务,也卸载依赖关系
    38. [admin@ansible .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=yes'
    39. 172.25.32.12 | SUCCESS => {
    40. "ansible_facts": {
    41. "discovered_interpreter_python": "/usr/bin/python"
    42. },
    43. "changed": false,
    44. "msg": "",
    45. "rc": 0,
    46. "results": [
    47. "httpd is not installed"
    48. ]
    49. }
    50. 指定下载的源(通过AppStream来安装)
    51. [admin@ansible .ansible]$ ansible westos -m yum -a 'name=httpd state=present enablerepo=AppStream'
    52. 172.25.32.12 | CHANGED => {
    53. "ansible_facts": {
    54. "discovered_interpreter_python": "/usr/bin/python"
    55. },
    56. "changed": true,
    57. "changes": {
    58. "installed": [
    59. "httpd"
    60. ]
    61. },
    62. "msg": "",
    63. "rc": 0,
    64. "results": [
    65. "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7 AppStream 1.2 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.2 M\nInstalled size: 3.7 M\nDownloading packages:\nDelta RPMs disabled because /usr/bin/applydeltarpm not installed.\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-88.el7.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7 \n\nComplete!\n"
    66. ]
    67. }
    68. 列出httpd的相关信息
    69. [admin@ansible .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=yes'
    70. 172.25.32.12 | SUCCESS => {
    71. "ansible_facts": {
    72. "discovered_interpreter_python": "/usr/bin/python"
    73. },
    74. "changed": false,
    75. "msg": "",
    76. "rc": 0,
    77. "results": [
    78. "httpd is not installed"
    79. ]
    80. }
    81. 更新httpd服务
    82. [admin@ansible .ansible]$ ansible westos -m yum -a 'name="httpd" state=latest'
    83. 172.25.32.11 | SUCCESS => {
    84. "ansible_facts": {
    85. "discovered_interpreter_python": "/usr/bin/python"
    86. },
    87. "changed": false,
    88. "changes": {
    89. "installed": [],
    90. "updated": []
    91. },
    92. "msg": "",
    93. "rc": 0,
    94. "results": [
    95. "All packages providing httpd are up to date",
    96. ""
    97. ]
    98. }

    5.13 service

    作用:  管理系统服务状态

    常用参数

    name##指定服务名称
      state

    ##指定对服务的动作

    #started

    #stoped

    #restarted

    #reloaded

    enabled

    ##设定服务开机是否启动

    #yes开启启动

    #no开机不启动

    1. 开启httpd服务,并指定开机启动
    2. [admin@ansible .ansible]$ ansible westos -m service -a "name=httpd state=started enabled=yes"
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "enabled": true,
    9. "name": "httpd",
    10. "state": "started",
    11. "status": {
    12. "ActiveEnterTimestampMonotonic": "0",
    13. "ActiveExitTimestampMonotonic": "0",
    14. "ActiveState": "inactive",
    15. "After": "-.mount network.target basic.target system.slice remote-fs.target tmp.mount systemd-journald.socket nss-lookup.target",
    16. "AllowIsolate": "no",
    17. "AmbientCapabilities": "0",
    18. ........
    19. 重启httpd服务
    20. [admin@ansible .ansible]$ ansible westos -m service -a "name=httpd state=restarted enabled=yes"
    21. 172.25.32.12 | CHANGED => {
    22. "ansible_facts": {
    23. "discovered_interpreter_python": "/usr/bin/python"
    24. },
    25. "changed": true,
    26. "enabled": true,
    27. "name": "httpd",
    28. "state": "started",
    29. "status": {
    30. "ActiveEnterTimestamp": "Fri 2022-07-29 07:34:50 UTC",
    31. "ActiveEnterTimestampMonotonic": "4964679459",
    32. "ActiveExitTimestampMonotonic": "0",
    33. "ActiveState": "active",
    34. "After": "-.mount systemd-journald.socket network.target nss-lookup.target basic.target tmp.mount remote-fs.target system.slice",
    35. "AllowIsolate": "no",
    36. "AmbientCapabilities": "0",

    5.14 firewalld

    常用参数

    zone##火墙的域
    service##服务名称
    permanent##永久生效
    state

    ##允许    enabled

    ##拒绝    disabled

    immediate##立即生效

    1. 开启火墙并永久指定火墙的域为public 且立即生效,
    2. [admin@ansible .ansible]$ ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
    3. 172.25.32.11 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
    9. }
    10. 172.25.32.12 | CHANGED => {
    11. "ansible_facts": {
    12. "discovered_interpreter_python": "/usr/bin/python"
    13. },
    14. "changed": true,
    15. "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
    16. }
    17. 在被控主机中:
    18. [root@www ~]# firewall-cmd --list-all
    19. public
    20. target: default
    21. icmp-block-inversion: no
    22. interfaces:
    23. sources:
    24. services: ssh dhcpv6-client http
    25. ports:
    26. protocols:
    27. masquerade: no
    28. forward-ports:
    29. source-ports:
    30. icmp-blocks:
    31. rich rules:

    5.15 user

    作用: 模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作

    name##必须参数,用于指定要操作的用户名称。
    group##指定用户所在的基本组。
    gourps##指定用户所在的附加组。
    append##指定添加附加组默认值为no
    shell##指定用户的默认shell。
    uid##指定用户的uid号。
    comment##指定用户的注释信息。
    state

    ##用于指定用户是否存在于远程主机

    #present      建立

    #absent       删除

    remove##当删除用户是删除用户家目录,默认值为no
    password

    ##此参数用于指定用户的密码。但密码为明文,

    ##可以用openssl  password  -6  '密码'生成加密字符

    generate_ssh_key##生成sshkey

    1. 建立lee用户
    2. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee'
    3. 172.25.32.11 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "comment": "",
    9. "create_home": true,
    10. "group": 1001,
    11. "home": "/home/lee",
    12. "name": "lee",
    13. "shell": "/bin/bash",
    14. "state": "present",
    15. "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    16. "stderr_lines": [
    17. "useradd: warning: the home directory already exists.",
    18. "Not copying any file from skel directory into it.",
    19. "Creating mailbox file: File exists"
    20. ],
    21. "system": false,
    22. "uid": 1001
    23. }
    24. 删除lee用户
    25. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee state=absent'
    26. 172.25.32.12 | CHANGED => {
    27. "ansible_facts": {
    28. "discovered_interpreter_python": "/usr/bin/python"
    29. },
    30. "changed": true,
    31. "force": false,
    32. "name": "lee",
    33. "remove": false,
    34. "state": "absent"
    35. }
    36. 指定lee用户的uid为6666
    37. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee uid=6666'
    38. 172.25.32.12 | CHANGED => {
    39. "ansible_facts": {
    40. "discovered_interpreter_python": "/usr/bin/python"
    41. },
    42. "changed": true,
    43. "comment": "",
    44. "create_home": true,
    45. "group": 6666,
    46. "home": "/home/lee",
    47. "name": "lee",
    48. "shell": "/bin/bash",
    49. "state": "present",
    50. "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    51. "stderr_lines": [
    52. "useradd: warning: the home directory already exists.",
    53. "Not copying any file from skel directory into it.",
    54. "Creating mailbox file: File exists"
    55. ],
    56. "system": false,
    57. "uid": 6666
    58. }
    59. 指定lee用户所在的组为admin
    60. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee group=admin'
    61. 172.25.32.12 | CHANGED => {
    62. "ansible_facts": {
    63. "discovered_interpreter_python": "/usr/bin/python"
    64. },
    65. "append": false,
    66. "changed": true,
    67. "comment": "",
    68. "group": 1000,
    69. "home": "/home/lee",
    70. "move_home": false,
    71. "name": "lee",
    72. "shell": "/bin/bash",
    73. "state": "present",
    74. "uid": 6666
    75. }
    76. 指定用户所在的附加组为admin
    77. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee groups=admin'
    78. 172.25.32.12 | CHANGED => {
    79. "ansible_facts": {
    80. "discovered_interpreter_python": "/usr/bin/python"
    81. },
    82. "append": false,
    83. "changed": true,
    84. "comment": "",
    85. "group": 1000,
    86. "groups": "admin",
    87. "home": "/home/lee",
    88. "move_home": false,
    89. "name": "lee",
    90. "shell": "/bin/bash",
    91. "state": "present",
    92. "uid": 6666
    93. }
    94. 生成加密字符【$符是特殊字符,所有要用转译字符】
    95. [admin@ansible .ansible]$ openssl passwd -1 'westos' #设置密码
    96. $1$oD/nYgUs$ztibP8DFmgBBgAxM4r6i/.
    97. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee password="$1$oD/nYgUs$ztibP8DFmgBBgAxM4r6i/."'
    98. 172.25.32.12 | CHANGED => {
    99. "ansible_facts": {
    100. "discovered_interpreter_python": "/usr/bin/python"
    101. },
    102. "changed": true,
    103. "comment": "",
    104. "create_home": true,
    105. "group": 100,
    106. "home": "/home/lee",
    107. "name": "lee",
    108. "password": "NOT_LOGGING_PASSWORD",
    109. "shell": "/bin/bash",
    110. "state": "present",
    111. "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    112. "stderr_lines": [
    113. "useradd: warning: the home directory already exists.",
    114. "Not copying any file from skel directory into it.",
    115. "Creating mailbox file: File exists"
    116. ],
    117. "system": false,
    118. "uid": 1001
    119. }
    120. 生成密钥
    121. [admin@ansible .ansible]$ ansible westos -m user -a 'name=lee generate_ssh_key=yes'
    122. 172.25.32.11 | CHANGED => {
    123. "ansible_facts": {
    124. "discovered_interpreter_python": "/usr/bin/python"
    125. },
    126. "append": false,
    127. "changed": true,
    128. "comment": "",
    129. "group": 100,
    130. "home": "/home/lee",
    131. "move_home": false,
    132. "name": "lee",
    133. "shell": "/bin/bash",
    134. "ssh_fingerprint": "2048 SHA256:5w9/Fcx+J8KIFc1HtjAyMXi/aB76xdPUewv/aGZSG6M ansible-generated on www.westos.org (RSA)",
    135. "ssh_key_file": "/home/lee/.ssh/id_rsa",
    136. "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx53ERXMA8HSsuLcqyoKcwynKuE2Iirn5zOD+6rHMHh+grpJZ/KvrxhMOOyrAMXS81Lm7+qksct2522bnsY7ARB4g6vANtkdM3GrYqffy1/tCAwO4X6HOrPrS3WuX3Fc7M++plvrxt6ze5RSxnRIcDUwRRKeeKmwsHCcpHKNdVYrM/BlBuKfj7ecwMOYZEWGCm2/yeoParqK5d5psy/58yiGclQvMUEl1/8Atguwxsh/T2Ta2pALMLWcWUDYsYaDxl8pKrwnXK0IntPF+b2eGa5Z9HoBS1H32ZBEjb/xGb9WAy0mn8ip4/xEW9qN6PE1RXvAl8ihSvTJw8zNMqcsWF ansible-generated on www.westos.org",
    137. "state": "present",
    138. "uid": 1001
    139. }

    5.16 group

    作用: group 模块可以帮助我们管理远程主机上的组。

    常用参数

    name##用于指定要操作的组名称。
    state

    ##用于指定组的状态

    #present           建立

    #absent            删除

    gid##用于指定组的gid。

    1. 添加组westoslee
    2. [admin@ansible .ansible]$ ansible westos -m group -a 'name=westoslee'
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "gid": 6667,
    9. "name": "westoslee",
    10. "state": "present",
    11. "system": false
    12. }
    13. 172.25.32.11 | CHANGED => {
    14. "ansible_facts": {
    15. "discovered_interpreter_python": "/usr/bin/python"
    16. },
    17. "changed": true,
    18. "gid": 6667,
    19. "name": "westoslee",
    20. "state": "present",
    21. "system": false
    22. }
    23. 指定westoslee组的gid为8888
    24. [admin@ansible .ansible]$ ansible westos -m group -a 'name=westoslee gid=8888'
    25. 172.25.32.11 | CHANGED => {
    26. "ansible_facts": {
    27. "discovered_interpreter_python": "/usr/bin/python"
    28. },
    29. "changed": true,
    30. "gid": 8888,
    31. "name": "westoslee",
    32. "state": "present",
    33. "system": false
    34. }
    35. 172.25.32.12 | CHANGED => {
    36. "ansible_facts": {
    37. "discovered_interpreter_python": "/usr/bin/python"
    38. },
    39. "changed": true,
    40. "gid": 8888,
    41. "name": "westoslee",
    42. "state": "present",
    43. "system": false
    44. }
    45. 删除westoslee组
    46. [admin@ansible .ansible]$ ansible westos -m group -a 'name=westoslee state=absent'
    47. 172.25.32.12 | CHANGED => {
    48. "ansible_facts": {
    49. "discovered_interpreter_python": "/usr/bin/python"
    50. },
    51. "changed": true,
    52. "name": "westoslee",
    53. "state": "absent"
    54. }
    55. 172.25.32.11 | CHANGED => {
    56. "ansible_facts": {
    57. "discovered_interpreter_python": "/usr/bin/python"
    58. },
    59. "changed": true,
    60. "name": "westoslee",
    61. "state": "absent"
    62. }

    5.17 lineinfile

    path##指定要操作的文件。
    line##指定文本内容。 "|+" 表示格式化输入
    regexp

    ##使用正则表达式匹配对应的行当替换文本时

    ##如果有多行文本都能被匹配

    ##则只有最后面被匹配到的那行文本才会被替换

    ##当删除文本时,如果有多行文本都能被匹配

    ##这么这些行都会被删除。

    state

    ##当想要删除对应的文本时需要将state参数的值设置为absent

    #state的默认值为present。

    backrefs

    ##当内容无匹配规则时不对文件做任何更改,默认值为no

    ##向后引用regexp变量信息

    insertafter

    ##借助insertafter参数可以将文本插入到“指定的行”之后

    ##insertafter参数的值可以设置为EOF或者正则表达式

    insertbefore

    ##借助insertbefore参数可以将文本插入到“指定的行”之前

    #insertbefore参数的值可以设置为BOF或者正则表达式

    backup##是否在修改文件之前对文件进行备份。
    create##当要操作的文件并不存在时,是否创建对应的文件。

    1. 给被控机建立/mnt下的westos文件,并编写内容\n表示换行
    2. [admin@ansible .ansible]$ ansible westos -m copy -a 'content="hello westos\nhello test\nhello linux\n" dest=/mnt/westos '
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "changed": true,
    8. "checksum": "868d8bfc146c9de5569f3fca88677b0f35abf30e",
    9. "dest": "/mnt/westos",
    10. "gid": 0,
    11. "group": "root",
    12. "md5sum": "b035847bf1e123742bd8e58647178d0d",
    13. "mode": "0644",
    14. "owner": "root",
    15. "size": 36,
    16. "src": "/home/admin/.ansible/tmp/ansible-tmp-1659096272.79-5119-87992278885050/source",
    17. "state": "file",
    18. "uid": 0
    19. }
    20. 在已经存在的文本(/mnt/westos)中写入nihao
    21. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="nihao"'
    22. 172.25.32.11 | CHANGED => {
    23. "ansible_facts": {
    24. "discovered_interpreter_python": "/usr/bin/python"
    25. },
    26. "backup": "",
    27. "changed": true,
    28. "msg": "line added"
    29. }
    30. 把以hello开头的行替替换成hello westos,匹配到多行的替换只能替换最后一行,其他行不进行替换
    31. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '
    32. 172.25.32.11 | CHANGED => {
    33. "ansible_facts": {
    34. "discovered_interpreter_python": "/usr/bin/python"
    35. },
    36. "backup": "",
    37. "changed": true,
    38. "msg": "line replaced"
    39. }
    40. 把以hello开头的行全部删除 ,匹配到多行的删除会全部删除
    41. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '
    42. 172.25.32.11 | CHANGED => {
    43. "ansible_facts": {
    44. "discovered_interpreter_python": "/usr/bin/python"
    45. },
    46. "backup": "",
    47. "changed": true,
    48. "msg": "line replaced"
    49. }
    50. 将westos文件中满足条件【h后边的任意四个字符,中间任意字符,w后任意五个字符】的行替换为字符\1 {因为 backrefs=no就不向后引用regexp}
    51. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}.*(w.{5}))" line="\1" backrefs=no'
    52. 172.25.32.12 | CHANGED => {
    53. "ansible_facts": {
    54. "discovered_interpreter_python": "/usr/bin/python"
    55. },
    56. "backup": "",
    57. "changed": true,
    58. "msg": "line added"
    59. }
    60. 将westos文件中满足条件【h后边的任意四个字符,中间任意字符,w后任意五个字符】的行替换为regexp的第一部分条件
    61. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'
    62. 172.25.32.12 | SUCCESS => {
    63. "ansible_facts": {
    64. "discovered_interpreter_python": "/usr/bin/python"
    65. },
    66. "backup": "",
    67. "changed": false,
    68. "msg": ""
    69. }
    70. 在文件中最后一行后添加#######ok##########
    71. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertafter=EOF'
    72. 172.25.32.11 | CHANGED => {
    73. "ansible_facts": {
    74. "discovered_interpreter_python": "/usr/bin/python"
    75. },
    76. "backup": "",
    77. "changed": true,
    78. "msg": "line added"
    79. }
    80. 在hello字符前添加#######ok##########;匹配到多行,则在最后一行有hello字符前添加
    81. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=BOF'
    82. 172.25.32.11 | SUCCESS => {
    83. "ansible_facts": {
    84. "discovered_interpreter_python": "/usr/bin/python"
    85. },
    86. "backup": "",
    87. "changed": false,
    88. "msg": ""
    89. }
    90. 在第一行前添加#######ok##########
    91. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=BOF'
    92. 172.25.32.11 | SUCCESS => {
    93. "ansible_facts": {
    94. "discovered_interpreter_python": "/usr/bin/python"
    95. },
    96. "backup": "",
    97. "changed": false,
    98. "msg": ""
    99. }
    100. 在test字符前一行添加
    101. [admin@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=test'
    102. 172.25.32.12 | SUCCESS => {
    103. "ansible_facts": {
    104. "discovered_interpreter_python": "/usr/bin/python"
    105. },
    106. "backup": "",
    107. "changed": false,
    108. "msg": ""
    109. }

    5.18 line |+

    1. [admin@ansible .ansible]$ cat westos.yml
    2. - name: test
    3. hosts: westos
    4. tasks:
    5. - lineinfile:
    6. path: /mnt/westos
    7. line: |+
    8. westos
    9. linux
    10. lee
    11. [admin@ansible .ansible]$ ansible-playbook westos.yml
    12. PLAY [test] ***********************************************************************************************************************
    13. TASK [Gathering Facts] ************************************************************************************************************
    14. ok: [172.25.32.12]
    15. ok: [172.25.32.11]
    16. TASK [lineinfile] *****************************************************************************************************************
    17. changed: [172.25.32.12]
    18. changed: [172.25.32.11]
    19. PLAY RECAP ************************************************************************************************************************
    20. 172.25.32.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    21. 172.25.32.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    5.19 replace

    作用 : 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换

    常用参数

    path##指定要操作的文件
    regexp

    ##指定一个正则表达式

    #文件中与正则匹配的字符串将会被替换。

    replace##指定最终要替换成的字符串。
    backup##是否在修改文件之前对文件进行备份,最好设置为yes。

    1. 把带有westos字符的全部替换成lee,并且备份westos原文件
    2. [admin@ansible .ansible]$ ansible westos -m replace -a 'path=/mnt/westos regexp="westos" replace="lee" backup=yes'
    3. 172.25.32.12 | CHANGED => {
    4. "ansible_facts": {
    5. "discovered_interpreter_python": "/usr/bin/python"
    6. },
    7. "backup_file": "/mnt/westos.10640.2022-07-29@12:35:34~",
    8. "changed": true,
    9. "msg": "1 replacements made"
    10. }
    11. 172.25.32.11 | CHANGED => {
    12. "ansible_facts": {
    13. "discovered_interpreter_python": "/usr/bin/python"
    14. },
    15. "backup_file": "/mnt/westos.10650.2022-07-29@12:35:33~",
    16. "changed": true,
    17. "msg": "1 replacements made"
    18. }

    5.20 setup

    作用: setup模块用于收集远程主机的一些基本信息

    常用参数:   filter                ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

    1. 显示被控机的主机名
    2. [admin@ansible .ansible]$ ansible westos -m setup -a "filter='ansible_fqdn'"
    3. 172.25.32.12 | SUCCESS => {
    4. "ansible_facts": {
    5. "ansible_fqdn": "lb-182-230.above.com",
    6. "discovered_interpreter_python": "/usr/bin/python"
    7. },
    8. "changed": false
    9. }
    10. 172.25.32.11 | SUCCESS => {
    11. "ansible_facts": {
    12. "ansible_fqdn": "www.westos.org",
    13. "discovered_interpreter_python": "/usr/bin/python"
    14. },
    15. "changed": false
    16. }
    17. 显示被控机的ip地址
    18. [admin@ansible .ansible]$ ansible westos -m setup -a "filter='ansible_all_ipv4_addresses'"
    19. 172.25.32.12 | SUCCESS => {
    20. "ansible_facts": {
    21. "ansible_all_ipv4_addresses": [
    22. "172.25.32.12"
    23. ],
    24. "discovered_interpreter_python": "/usr/bin/python"
    25. },
    26. "changed": false
    27. }
    28. 172.25.32.11 | SUCCESS => {
    29. "ansible_facts": {
    30. "ansible_all_ipv4_addresses": [
    31. "172.25.32.11"
    32. ],
    33. "discovered_interpreter_python": "/usr/bin/python"
    34. },
    35. "changed": false
    36. }

    5.21 debug

    作用:调试模块,用于在调试中输出信息

    常用参数

    msg:##调试输出的消息
    var:

    ##将某个任务执行的输出作为变量传递给debug模块

    ##debug会直接将其打印输出

    verbosity:##debug的级别(默认是0级,全部显示)

    1. 输出hello
    2. [admin@ansible .ansible]$ ansible westos -m debug -a 'msg=hello'
    3. 172.25.32.11 | SUCCESS => {
    4. "msg": "hello"
    5. }
    6. 172.25.32.12 | SUCCESS => {
    7. "msg": "hello"
    8. }
    9. 输出被控机的主机名【不能用ansible命令,因为看不到结果,但是可以在playbook中看到效果】
    10. [admin@ansible .ansible]$ cat test.yml
    11. - name: test
    12. hosts: westos
    13. tasks:
    14. - name: debug
    15. debug:
    16. var: ansible_facts['fqdn']
    17. [admin@ansible .ansible]$ ansible-playbook test.yml
    18. PLAY [test] ***********************************************************************************************************************
    19. TASK [Gathering Facts] ************************************************************************************************************
    20. ok: [172.25.32.12]
    21. ok: [172.25.32.11]
    22. TASK [debug] **********************************************************************************************************************
    23. ok: [172.25.32.11] => {
    24. "ansible_facts['fqdn']": "www.westos.org"
    25. }
    26. ok: [172.25.32.12] => {
    27. "ansible_facts['fqdn']": "lb-182-230.above.com"
    28. }
    29. PLAY RECAP ************************************************************************************************************************
    30. 172.25.32.11 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    31. 172.25.32.12 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  • 相关阅读:
    unplugin-vue-components和unplugin-auto-import插件
    搜索技术【深度优先搜索】 - DFS+ 剪枝优化 【POJ No. 2676】数独游戏 Sudoku
    YOLO算法(You Only Look Once)系列讲解与实现(待完善)
    三十、openlayers官网示例解析Double click, Drag and Zoom——第二次点击鼠标拖拽缩放地图效果、取消地图双击放大事件
    2流高手速成记(之七):基于Dubbo&Nacos的微服务简要实现
    InnoDB 存储引擎之 Buffer Pool
    Linux编程基础:1~6章实训编程题
    Java EE改Jakarta
    测试开发如何设计测试用例
    Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
  • 原文地址:https://blog.csdn.net/weixin_66461008/article/details/126047365