如何使用 Ansible 重命名文件或目录? 首先,我想澄清的是,我将提供一个仅使用 Ansible 原生模块的解决方案,而不是通过 shell 模块调用 Unix 的 mv 命令。 今天我们将讨论 Ansible 的两个模块:copy 和 file。 它们的完整名称分别是 ansible.builtin.copy 和 ansible.builtin.file,这意味着它们是 Ansible 自带的模块集合的一部分,并且与 Ansible 一起分发。 这两个模块都非常稳定,已经存在多年。 copy 模块的用途是将文件复制到远程位置。 文件成功复制后,我们可以使用 file 模块删除源文件。

参数: copy 模块

  • dest(路径):目标路径
  • src(字符串):源文件路径,假定在控制主机上,可以是相对路径或绝对路径。
  • remote_src(布尔值):no/yes。从 Ansible 2.0 版本开始,copy 模块支持 remote_src 参数。如果设置为 True,它将在远程/目标机器上查找源文件。从 Ansible 2.8 开始,remote_src 支持递归复制。

file 模块

  • path(字符串,别名:dest、name):文件路径
  • state(字符串):file、absent、directory、hard、link、touch 参数列表非常丰富,但我将总结最有用的部分。 唯一必需的参数是 path,它指定了你要操作的文件或目录的文件系统路径。 state 参数定义了我们要修改的对象类型,默认值是 file,但我们也可以处理目录、硬链接、符号链接,或者通过 touch 选项仅更新访问时间。在我们的用例中,我们将使用 absent 选项。

示例: 让我们通过一个实际的 Ansible 剧本,来演示如何使用 Ansible 重命名文件或目录。

代码: rename/file.yml 文件内容如下:

1 ---
2 - name: 重命名文件或目录
3   hosts: all
4   vars:
5     mysrc: "~/foo"
6     mydst: "~/bar"
7   tasks:
8     - name: 检查文件是否存在
9       ansible.builtin.stat:
10        path: "{{ mysrc }}"
11      register: check_file_name
12 
13     - name: 打印调试信息
14       ansible.builtin.debug:
15         var: check_file_name
16 
17     - name: 使用新名称复制文件
18       ansible.builtin.copy:
19         remote_src: true
20         src: "{{ mysrc }}"
21         dest: "{{ mydst }}"
22       when: check_file_name.stat.exists
23 
24     - name: 删除旧文件
25       ansible.builtin.file:
26         path: "{{ mysrc }}"
27         state: absent
28       when: check_file_name.stat.exists

执行: 文件不存在时的输出:

1 $ ansible-playbook -i demo/inventory rename/file.yml
2 PLAY [重命名文件或目录] *******************************************
3 TASK [Gathering Facts] *******************************************
4 ok: [demo.example.com]
5 TASK [检查文件是否存在] *******************************************
6 ok: [demo.example.com]
7 TASK [打印调试信息] **********************************************
8 ok: [demo.example.com] => {
9     "check_file_name": {
10         "changed": false,
11         "failed": false,
12         "stat": {
13             "exists": false
14         }
15     }
16 }
17 TASK [使用新名称复制文件] *****************************************
18 skipping: [demo.example.com]
19 TASK [删除旧文件] ***********************************************
20 skipping: [demo.example.com]
21 PLAY RECAP *******************************************************
22 demo.example.com           : ok=3    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

文件存在时的输出:

1 $ ansible-playbook -i demo/inventory rename/file.yml
2 PLAY [重命名文件或目录] *******************************************
3 TASK [Gathering Facts] *******************************************
4 ok: [demo.example.com]
5 TASK [检查文件是否存在] *******************************************
6 ok: [demo.example.com]
7 TASK [打印调试信息] **********************************************
8 ok: [demo.example.com] => {
9     "check_file_name": {
10         "changed": false,
11         "failed": false,
12         "stat": {
13             "atime": 1632319723.592112,
14             "attr_flags": "",
15             "attributes": [],
16             "block_size": 4096,
17             "blocks": 0,
18             "charset": "binary",
19             "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
20             "ctime": 1632319723.592112,
21             "dev": 64768,
22             "device_type": 0,
23             "executable": false,
24             "exists": true,
25             "gid": 10,
26             "gr_name": "wheel",
27             "inode": 134764246,
28             "isblk": false,
29             "ischr": false,
30             "isdir": false,
31             "isfifo": false,
32             "isgid": false,
33             "islnk": false,
34             "isreg": true,
35             "issock": false,
36             "isuid": false,
37             "mimetype": "inode/x-empty",
38             "mode": "0644",
39             "mtime": 1632319723.592112,
40             "nlink": 1,
41             "path": "/home/devops/foo",
42             "pw_name": "devops",
43             "readable": true,
44             "rgrp": true,
45             "roth": true,
46             "rusr": true,
47             "size": 0,
48             "uid": 1001,
49             "version": "1745015340",
50             "wgrp": false,
51             "woth": false,
52             "writeable": true,
53             "wusr": true,
54             "xgrp": false,
55             "xoth": false,
56             "xusr": false
57         }
58     }
59 }
60 TASK [使用新名称复制文件] *****************************************
61 changed: [demo.example.com]
62 TASK [删除旧文件] ***********************************************
63 changed: [demo.example.com]
64 PLAY RECAP *******************************************************
65 demo.example.com           : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

目录存在时的输出:

1 $ ansible-playbook -i demo/inventory rename/file.yml
2 PLAY [重命名文件或目录] *******************************************
3 TASK [Gathering Facts] *******************************************
4 ok: [demo.example.com]
5 TASK [检查文件是否存在] *******************************************
6 ok: [demo.example.com]
7 TASK [打印调试信息] **********************************************
8 ok: [demo.example.com] => {
9     "check_file_name": {
10         "changed": false,
11         "failed": false,
12         "stat": {
13             "atime": 1632319779.5021598,
14             "attr_flags": "",
15             "attributes": [],
16             "block_size": 4096,
17             "blocks": 0,
18             "charset": "binary",
19             "ctime": 1632319779.5021598,
20             "dev": 64768,
21             "device_type": 0,
22             "executable": true,
23             "exists": true,
24             "gid": 10,
25             "gr_name": "wheel",
26             "inode": 898820,
27             "isblk": false,
28             "ischr": false,
29             "isdir": true,
30             "isfifo": false,
31             "isgid": false,
32             "islnk": false,
33             "isreg": false,
34             "issock": false,
35             "isuid": false,
36             "mimetype": "inode/directory",
37             "mode": "0755",
38             "mtime": 1632319779.5021598,
39             "nlink": 2,
40             "path": "/home/devops/foo",
41             "pw_name": "devops",
42             "readable": true,
43             "rgrp": true,
44             "roth": true,
45             "rusr": true,
46             "size": 6,
47             "uid": 1001,
48             "version": "1317302979",
49             "wgrp": false,
50             "woth": false,
51             "writeable": true,
52             "wusr": true,
53             "xgrp": true,
54             "xoth": true,
55             "xusr": true
56         }
57     }
58 }
59 TASK [使用新名称复制文件] *****************************************
60 ok: [demo.example.com]
61 TASK [删除旧文件] ***********************************************
62 changed: [demo.example.com]
63 PLAY RECAP *******************************************************
64 demo.example.com           : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0