Ansible File 模块

AnsibleAnsibleBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将探索 Ansible 的 File 模块,该模块允许你管理远程主机上的文件和目录。File 模块提供了广泛的功能,例如创建、删除、修改权限以及检查文件和目录的存在性。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("`Ansible`")) -.-> ansible/ModuleOperationsGroup(["`Module Operations`"]) ansible(("`Ansible`")) -.-> ansible/PlaybookEssentialsGroup(["`Playbook Essentials`"]) ansible/ModuleOperationsGroup -.-> ansible/debug("`Test Output`") ansible/ModuleOperationsGroup -.-> ansible/file("`Manage Files/Directories`") ansible/ModuleOperationsGroup -.-> ansible/shell("`Execute Shell Commands`") ansible/ModuleOperationsGroup -.-> ansible/stat("`File Statistics`") ansible/ModuleOperationsGroup -.-> ansible/template("`Generate Files from Templates`") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("`Execute Playbook`") subgraph Lab Skills ansible/debug -.-> lab-289654{{"`Ansible File 模块`"}} ansible/file -.-> lab-289654{{"`Ansible File 模块`"}} ansible/shell -.-> lab-289654{{"`Ansible File 模块`"}} ansible/stat -.-> lab-289654{{"`Ansible File 模块`"}} ansible/template -.-> lab-289654{{"`Ansible File 模块`"}} ansible/playbook -.-> lab-289654{{"`Ansible File 模块`"}} end

在远程主机上创建文件

在这一步骤中,你将使用 Ansible 的 File 模块在远程主机上创建一个文件。

首先,创建一个新的 Ansible playbook 文件,命名为 /home/labex/project/file-module-playbook.yaml,并在文本编辑器中打开它。
将以下内容添加到 playbook 文件中:

- hosts: localhost
  tasks:
    - name: Create a file on remote host
      file:
        path: /home/labex/file.txt
        state: touch
  • file: 用于操作文件系统的 Ansible 模块。
  • path: 指定文件的路径,本例中为 /home/labex/file.txt
  • state: 指定文件的状态。这里,touch 表示如果文件不存在则创建,如果文件已存在则更新其访问和修改时间戳。

该 playbook 的目的是在远程主机上创建一个名为 file.txt 的文件。

然后,使用以下命令运行 playbook:

ansible-playbook file-module-playbook.yaml

示例输出:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Create a file on remote host] ********************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

最后,验证文件 file.txt 是否已在远程主机的指定路径中创建。

ll /home/labex/file.txt

示例输出:

-rw-rw-r-- 1 labex labex 0 Mar 10 03:12 file.txt

你将看到消息,表明 /home/labex/file.txt 已成功创建。

管理文件权限

在这一步骤中,你将学习如何使用 Ansible 的 File 模块管理远程主机上的文件权限。

首先,修改现有的 playbook 文件,删除所有内容并添加以下内容到 playbook 文件中:

- hosts: localhost
  tasks:
    - name: Set file permissions
      file:
        path: /home/labex/file.txt
        mode: "0644"
  • file: 用于操作文件系统的 Ansible 模块。
  • path: 指定文件的路径,本例中为 /home/labex/file.txt
  • mode: 此参数用于设置文件的权限模式。将 "0644" 替换为所需的文件权限模式。有关权限模式的更多信息,请参考 chmod 文档。

该 playbook 的目的是将文件 /home/labex/file.txt 的权限设置为 0644

然后,使用以下命令运行 playbook:

ansible-playbook file-module-playbook.yaml

示例输出:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Set file permissions] ****************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

最后,验证文件权限是否已在远程主机上按指定设置。

ll /home/labex/file.txt

示例输出:

-rw-r--r-- 1 labex labex 0 Mar 10 03:12 /home/labex/file.txt

这里的 -rw-r--r-- 表示 /home/labex/file.txt 的权限模式已成功设置为 0644

删除远程主机上的文件

在这一步骤中,你将学习如何使用 Ansible 的 File 模块删除远程主机上的文件。

首先,修改现有的 playbook 文件,删除所有内容并添加以下内容到 playbook 文件中:

- hosts: localhost
  tasks:
    - name: Delete a file on remote host
      file:
        path: /home/labex/file.txt
        state: absent
  • file: 用于操作文件系统的 Ansible 模块。
  • path: 指定要删除的文件的路径,即 /home/labex/file.txt
  • state: 此参数表示文件应处于 absent 状态。因此,该任务的目标是删除指定路径的文件。

该 playbook 的目的是删除远程主机上的文件 /home/labex/file.txt

然后,使用以下命令运行 playbook:

ansible-playbook file-module-playbook.yaml

示例输出:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Delete a file on remote host] ********************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

最后,验证文件 file.txt 是否已被删除且不再存在于远程主机上。

ll /home/labex/file.txt

示例输出:

ls: cannot access '/home/labex/file.txt': No such file or directory

此消息表明 /home/labex/file.txt 文件已成功删除。

检查文件是否存在

在这一步骤中,你将学习如何使用 Ansible 的 File 模块检查远程主机上文件的存在性。

首先,修改现有的 playbook 文件,删除所有内容并添加以下内容到 playbook 文件中:

- hosts: localhost
  tasks:
    - name: Check file existence on remote host
      stat:
        path: /home/labex/file.txt
      register: file_info

    - name: Print file existence
      debug:
        msg: "File exists: {{ file_info.stat.exists }}"
  • stat: 这是 Ansible 的一个模块,用于获取文件或目录的状态信息。
  • path: 指定要检查的文件的路径,即 /home/labex/file.txt
  • register: 使用 register 关键字将模块执行的结果存储在变量 file_info 中。
  • debug: 这是 Ansible 的一个模块,用于打印调试信息。
  • msg: 使用 debug 模块打印一条消息,其中包含有关文件存在性的信息,该信息通过 file_info.stat.exists 获取。

该 playbook 的目的是检查远程主机上文件 /home/labex/file.txt 的存在性,并将信息打印到标准输出。

然后,使用以下命令运行 playbook:

ansible-playbook file-module-playbook.yaml

示例输出:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Check file existence on remote host] *************************************
ok: [localhost]

TASK [Print file existence] ****************************************************
ok: [localhost] => {
    "msg": "File exists: False"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

最后,观察输出以查看文件 file.txt 是否存在于远程主机上。
"msg": "File exists: False" 表示文件 /home/labex/file.txt 不存在。

总结

恭喜!你已经成功完成了 Ansible File Module 实验。你学习了如何使用 File 模块在远程主机上创建文件和目录、管理文件权限、删除文件以及检查文件是否存在。

File 模块是 Ansible 中的一个强大工具,它使你能在自动化任务中执行各种与文件相关的操作。现在,你可以自信地在 Ansible playbook 中使用 File 模块来高效地管理文件和目录。

继续探索 Ansible 文档和其他模块,以扩展你的知识并提升自动化技能。祝你使用 Ansible 愉快!

您可能感兴趣的其他 Ansible 教程