优秀的编程知识分享平台

网站首页 > 技术文章 正文

ansible-script模块和copy模块(ansible 模块)

nanyue 2024-07-20 23:55:56 技术文章 12 ℃

script模块


将管理节点上的脚本传递到被管理节点执行
首先我们造一个脚本

[root@bogon ~]# cat a.sh
touch /tmp/testfile



[root@bogon ~]# ansible web -i hosts -m script -a "/root/a.sh"
192.168.216.132 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.216.132 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.216.132 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}


你可以顺便用ls去目录下查看一下是否创建了哪个文件
[root@bogon ~]# ansible web -i hosts -m shell -a "ls -l /tmp"
192.168.216.132 | CHANGED | rc=0 >>
总用量 4
drwx------. 2 root root  41 4月  22 18:15 ansible_command_payload_qALE89
-rwx------. 1 root root 836 4月  19 16:27 ks-script-NOcGKL
drwx------. 3 root root  17 4月  22 16:20 systemd-private-5e53fc11da3041328152096042f601e2-vmtoolsd.service-kh7K1u
-rw-r--r--. 1 root root   0 4月  22 18:14 testfile
-rw-------. 1 root root   0 4月  19 16:19 yum.log

copy模块

copy主要用于管理节点和被管理节点之间的文件拷贝
常用参数:
src 指定拷贝文件的源地址
dest 指定拷贝文件的目标地址
backup 拷贝文件前,若原目标文件发生了变化,则对目标文件进行备份
woner 指定新拷贝文件的所有者
group 指定新拷贝文件的所属组
mode 指定新拷贝文件的权限

[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy"
192.168.216.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 0,
    "group": "root",
    "md5sum": "73e7fc60b74215b4c898d18f7ddd5a2e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "src": "/root/.ansible/tmp/ansible-tmp-1650623446.0-3781-130537558434375/source",
    "state": "file",
    "uid": 0
}
[root@bogon ~]# ansible web -i hosts -m shell -a "ls /etc/yum.repos.d/"
192.168.216.132 | CHANGED | rc=0 >>
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
nginx.repocopy

backup实验,如果被管理节点上的文件发生了改变,添加backup=yes,可以在复制过去之前备份原文件,如果没有添加backup=yes,则会直接覆盖,不管节点上的文件是否改变

[root@bogon ~]# ssh 192.168.216.132
Last login: Fri Apr 22 18:31:28 2022 from 192.168.216.133
[root@localhost ~]#
我先ssh过去一个节点
我在本地把他文件给改了
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
总用量 32
-rw-r--r--. 1 root root 1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root root  649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 11月 30 2016 CentOS-Vault.repo
-rw-r--r--. 1 root root  353 4月  22 18:30 nginx.repocopy
[root@localhost yum.repos.d]# cat nginx.repocopy
drwx------. 2 root root  41 4月  22 18:15 ansible_command_payload_qALE89
-rwx------. 1 root root 836 4月  19 16:27 ks-script-NOcGKL
drwx------. 3 root root  17 4月  22 16:20 systemd-private-5e53fc11da3041328152096042f601e2-vmtoolsd.service-kh7K1u
-rw-r--r--. 1 root root   0 4月  22 18:14 testfile
-rw-------. 1 root root   0 4月  19 16:19 yum.log
[root@localhost yum.repos.d]# echo aaaaaa>nginx.repocopy
[root@localhost yum.repos.d]# cat nginx.repocopy
aaaaaa

然后我们再执行
[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy"
192.168.216.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 0,
    "group": "root",
    "md5sum": "73e7fc60b74215b4c898d18f7ddd5a2e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "src": "/root/.ansible/tmp/ansible-tmp-1650624305.21-3939-134858939572640/source",
    "state": "file",
    "uid": 0
}

节点看一下是被覆盖了
[root@localhost yum.repos.d]# cat nginx.repocopy
drwx------. 2 root root  41 4月  22 18:15 ansible_command_payload_qALE89
-rwx------. 1 root root 836 4月  19 16:27 ks-script-NOcGKL
drwx------. 3 root root  17 4月  22 16:20 systemd-private-5e53fc11da3041328152096042f601e2-vmtoolsd.service-kh7K1u
-rw-r--r--. 1 root root   0 4月  22 18:14 testfile
-rw-------. 1 root root   0 4月  19 16:19 yum.log
如果添加backup=yes则会添加一个备份
我再次改掉文件
[root@localhost yum.repos.d]# echo aaaaaa>nginx.repocopy
[root@localhost yum.repos.d]# cat nginx.repocopy
aaaaaa

这边再次执行
[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy backup=yes"
192.168.216.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/etc/yum.repos.d/nginx.repocopy.4873.2022-04-22@18:47:10~",这里可以看到给我们备份了一个出来
    "changed": true,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 0,
    "group": "root",
    "md5sum": "73e7fc60b74215b4c898d18f7ddd5a2e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "src": "/root/.ansible/tmp/ansible-tmp-1650624429.64-3972-151413796226392/source",
    "state": "file",
    "uid": 0
}
咱去本地看看去
[root@localhost yum.repos.d]# cat nginx.repocopy
drwx------. 2 root root  41 4月  22 18:15 ansible_command_payload_qALE89
-rwx------. 1 root root 836 4月  19 16:27 ks-script-NOcGKL
drwx------. 3 root root  17 4月  22 16:20 systemd-private-5e53fc11da3041328152096042f601e2-vmtoolsd.service-kh7K1u
-rw-r--r--. 1 root root   0 4月  22 18:14 testfile
-rw-------. 1 root root   0 4月  19 16:19 yum.log
源文件恢复了同时多了一个备份出来的文件
[root@localhost yum.repos.d]# ll
总用量 36
-rw-r--r--. 1 root root 1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root root  649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 11月 30 2016 CentOS-Vault.repo
-rw-r--r--. 1 root root  353 4月  22 18:47 nginx.repocopy
-rw-r--r--. 1 root root    7 4月  22 18:46 nginx.repocopy.4873.2022-04-22@18:47:10~
[root@localhost yum.repos.d]# cat  nginx.repocopy.4873.2022-04-22@18:47:10~
aaaaaa
如果文件没有发生改变则不会备份
我们直接执行,可以看到没有backup  file
[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy backup=yes"
192.168.216.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/etc/yum.repos.d/nginx.repocopy",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "state": "file",
    "uid": 0
}
本地也没有多文件
[root@localhost yum.repos.d]# ll
总用量 36
-rw-r--r--. 1 root root 1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root root  649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 11月 30 2016 CentOS-Vault.repo
-rw-r--r--. 1 root root  353 4月  22 18:47 nginx.repocopy
-rw-r--r--. 1 root root    7 4月  22 18:46 nginx.repocopy.4873.2022-04-22@18:47:10~

设置用户及用户组实验,你可以直接在复制文件的时候指定用户和属组

[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy owner=nobody group=nobody"
192.168.216.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 99,
    "group": "nobody",
    "mode": "0644",
    "owner": "nobody",
    "path": "/etc/yum.repos.d/nginx.repocopy",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "state": "file",
    "uid": 99
}
可以看到本地的属组属主已变为nobody
[root@localhost yum.repos.d]# ll
总用量 36
-rw-r--r--. 1 root   root   1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root   root   1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root   root    649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root   root    314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root   root    630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root   root   1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root   root   2893 11月 30 2016 CentOS-Vault.repo
-rw-r--r--. 1 nobody nobody  353 4月  22 18:47 nginx.repocopy
-rw-r--r--. 1 root   root      7 4月  22 18:46 nginx.repocopy.4873.2022-04-22@18:47:10~

设置文件的权限

[root@bogon ~]# ansible web -i hosts -m copy -a "src=./nginx.repocopy dest=/etc/yum.repos.d/nginx.repocopy mode=0755"
192.168.216.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "732bf34f2a4f8839e2fd3d064c2f9737df6dcabb",
    "dest": "/etc/yum.repos.d/nginx.repocopy",
    "gid": 99,
    "group": "nobody",
    "mode": "0755",
    "owner": "nobody",
    "path": "/etc/yum.repos.d/nginx.repocopy",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 353,
    "state": "file",
    "uid": 99
}


[root@localhost yum.repos.d]# ll
总用量 36
-rw-r--r--. 1 root   root   1664 11月 30 2016 CentOS-Base.repo
-rw-r--r--. 1 root   root   1309 11月 30 2016 CentOS-CR.repo
-rw-r--r--. 1 root   root    649 11月 30 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root   root    314 11月 30 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root   root    630 11月 30 2016 CentOS-Media.repo
-rw-r--r--. 1 root   root   1331 11月 30 2016 CentOS-Sources.repo
-rw-r--r--. 1 root   root   2893 11月 30 2016 CentOS-Vault.repo
-rwxr-xr-x. 1 nobody nobody  353 4月  22 18:47 nginx.repocopy
可以看到本地的权限已变
-rw-r--r--. 1 root   root      7 4月  22 18:46 nginx.repocopy.4873.2022-04-22@18:47:10~



最近发表
标签列表