赞
踩
作用:可以实现批量管理主机信息 PS:command模块有缺陷,无法识别特殊符号信息:"$HOME" "<" ">" "|" ";" "&" 模块常用指令参数: creates: 判断一个文件是否存在,如果存在就不执行相应命令 removers:判断一个文件是否存在,如果存在就执行相应命令 chdir: 执行命令前,进行目录一个切换 PS: (1)万能模块在使用时缺陷,不具有幂等性(第一次执行结果和多次执行结果相同) 当不具有幂等性时,会对剧本编写产生问题??? (2)操作步骤可能会过于繁琐 批量执行脚本功能: 第一个历程:编写脚本 [root@ansible project]# vim test.sh [root@ansible project]# cat test.sh #!/bin/bash echo "hello world" [root@ansible project]# chmod +x test.sh [root@ansible project]# ./test.sh hello world 第二个历程:需要将脚本文件分发给所有主机 [root@ansible project]# ansible webservers -m copy -a "src=/opt/project/test.sh dest=/opt/project" 192.168.137.135 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "checksum": "6aa73e56214888452816de465fa1c8329ac9e119", "dest": "/opt/project", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/opt/project", "size": 85, "state": "file", "uid": 0 } 192.168.137.136 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "checksum": "6aa73e56214888452816de465fa1c8329ac9e119", "dest": "/opt/project", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/opt/project", "size": 85, "state": "file", "uid": 0 } 第三个历程:设置脚本文件权限 [root@ansible project]# ansible webservers -m file -a "path=/opt/project mode=755" 192.168.137.136 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/opt/project", "size": 85, "state": "file", "uid": 0 } 192.168.137.135 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/opt/project", "size": 85, "state": "file", "uid": 0 } 第四个历程:执行脚本文件 [root@ansible project]# ansible webservers -m shell -a "/opt/project" 192.168.137.135 | CHANGED | rc=0 >> hello world 192.168.137.136 | CHANGED | rc=0 >> hello world
作用:专门批量执行脚本功能 第一个历程:编写脚本 第二个历程:批量执行脚本 [root@ansible project]# ansible webservers -m script -a "/opt/project/test.sh" 192.168.137.135 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.137.135 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.137.135 closed." ], "stdout": "hello world\r\n", "stdout_lines": [ "hello world" ] } 192.168.137.136 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.137.136 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.137.136 closed." ], "stdout": "hello world\r\n", "stdout_lines": [ "hello world" ] }
作用:可以实现批量分发数据文件 常用参数指令: src: 指定要分发文件数据信息 dest: 将分发文件保存到哪个目标路径 owner: 指定文件分发后的属主信息 group: 指定文件分发后的属组信息 mode: 指定文件分发后的权限信息 backup: 当文件名称重复时,会将原文件备份,再进行覆盖 content: 在被管理主机上创建文件并设置信息内容 directory_mode: 可以递归设置目录中数据权限(只是针对复制目录时) force:(了解) 可以避免相同名称文件覆盖 force=yes 默认设置 表示会覆盖 force=no 不会对相同文件进行覆盖 remote_src: 将被管理主机上文件进行本地备份保存 validate: 验证分发后的文件合法性 123456789101112131415
实践操作:
(1)批量分发文件信息,并修改文件属主 属组 和权限信息
[root@ansible project]# ansible webservers -m copy -a "src=/opt/project/abc.txt dest=/tmp/ mode=666 owner=tom group=tom" 192.168.137.135 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "1a82a29241cefb4e3e6599a809e234e580101003", "dest": "/tmp/abc.txt", "gid": 1001, "group": "tom", "mode": "0666", "owner": "tom", "path": "/tmp/abc.txt", "size": 14, "state": "file", "uid": 1001 } 192.168.137.136 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "1a82a29241cefb4e3e6599a809e234e580101003", "dest": "/tmp/abc.txt", "gid": 1001, "group": "tom", "mode": "0666", "owner": "tom", "path": "/tmp/abc.txt", "size": 14, "state": "file", "uid": 1001 }
2)在被管理主机上创建文件信息,并设置文件的内容
[root@ansible ~]# ansible webservers -m copy -a "content=hello_dog dest=/tmp/abc.txt" 192.168.137.136 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "7f3d38f956a244629bac0f6c5ef9b63d677c20aa", "dest": "/tmp/abc.txt", "gid": 1001, "group": "tom", "md5sum": "b168d7d7ec5af35034436a2f0aa34ec7", "mode": "0666", "owner": "tom", "size": 9, "src": "/root/.ansible/tmp/ansible-tmp-1666505224.513224-2159-172028776136002/source", "state": "file", "uid": 1001 } ······· [root@ansible ~]# ansible webservers -m shell -a " cat /tmp/abc.txt " 192.168.137.136 | CHANGED | rc=0 >> hello_dog 192.168.137.145 | CHANGED | rc=0 >> hello_dog 192.168.137.135 | CHANGED | rc=0 >> hello_dog
(3)将目录信息复制到被管理主机上,并且将目录以及下面所有内容权限统一改为777
1
利用copy模块分发目录信息时:
目录后面有 / 表示将目录下面数据内容进行整体分发
目录后面没有 / 表示将目录下面数据内容以及目录本身都做分发
<Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。