Ansible Stat 模块

AnsibleAnsibleBeginner
立即练习

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

介绍

在本实验中,你将探索 Ansible 的 Stat 模块,该模块允许你收集远程主机上文件和目录的信息。Stat 模块提供了各种属性和信息,例如文件大小、所有权、权限和修改时间戳。


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/stat("`File Statistics`") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("`Execute Playbook`") subgraph Lab Skills ansible/debug -.-> lab-290192{{"`Ansible Stat 模块`"}} ansible/stat -.-> lab-290192{{"`Ansible Stat 模块`"}} ansible/playbook -.-> lab-290192{{"`Ansible Stat 模块`"}} end

获取文件信息

在这一步中,你将使用 Ansible 的 Stat 模块来收集远程主机上某个文件的信息。

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

- hosts: localhost
  tasks:
    - name: Get file information
      stat:
        path: /home/labex/project/file.txt
      register: file_info

    - name: Print file information
      debug:
        msg: |
          File size: {{ file_info.stat.size }}
          Ownership: {{ file_info.stat.uid }}:{{ file_info.stat.gid }}
          Permissions: {{ file_info.stat.mode }}
  • hosts:指定运行 playbook 的目标主机。在本例中,playbook 将在本地主机上运行,因为目标主机是 localhost
  • tasks:这是要运行的任务列表。
  • name:这是任务的描述性名称,用于标识任务的用途。
  • stat:这个 Ansible 模块用于收集远程主机上由 path 参数指定的文件的信息。
  • register:将 stat 模块的输出存储在一个名为 file_info 的变量中,以便后续使用。
  • debug:在 playbook 执行期间打印调试信息。
  • msg:输出一条消息,其中包含使用 stat 模块检索到的文件信息,包括文件大小、所有权(UID 和 GID)以及权限。

总结来说,这个 playbook 旨在检索本地主机上位于 /home/labex/project/file.txt 的特定文件的信息,并打印出诸如大小、所有权和权限等详细信息。

接下来,在 /home/labex/project 目录下创建一个名为 file.txt 的文件。

echo "This is the content of the file." > /home/labex/project/file.txt

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

ansible-playbook stat-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 [Get file information] ****************************************************
ok: [localhost]

TASK [Print file information] **************************************************
ok: [localhost] => {
    "msg": "File size: 33\nOwnership: 5000:5000\nPermissions: 0664\n"
}

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

检查文件是否存在

在这一步中,你将使用 Ansible 的 Stat 模块来检查远程主机上某个文件是否存在。

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

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

    - name: Print file existence
      debug:
        msg: "File exists: {{ file_info.stat.exists }}"
  • hosts:指定运行 playbook 的目标主机。在本例中,playbook 将在本地主机上运行,因为目标主机是 localhost
  • tasks:这是要运行的任务列表。
  • name:这是任务的描述性名称,用于标识任务的用途。
  • stat:这个 Ansible 模块用于收集远程主机上由 path 参数指定的文件的信息。
  • register:将 stat 模块的输出存储在一个名为 file_info 的变量中,以便后续使用。
  • debug:在 playbook 执行期间打印调试信息。
  • msg:输出一条消息,指示文件是否存在,基于使用 stat 模块检索到的信息。

总结来说,这个 playbook 旨在检查本地主机上位于 /home/labex/project/file.txt 的特定文件是否存在,并打印一条消息指示文件是否存在。

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

ansible-playbook stat-module-playbook.yaml

观察输出,查看文件 file.txt 是否存在于远程主机上。
示例输出:

[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: True"
}

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

获取文件修改时间戳

在这一步中,你将使用 Ansible 的 Stat 模块来获取远程主机上某个文件的修改时间戳。

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

- hosts: localhost
  tasks:
    - name: Get file modification timestamp
      stat:
        path: /home/labex/project/file.txt
      register: file_info

    - name: Print file modification timestamp
      debug:
        msg: "File modification timestamp: {{ file_info.stat.mtime }}"
  • hosts:指定运行 playbook 的目标主机。在本例中,playbook 将在本地主机上运行,因为目标主机是 localhost
  • tasks:这是要运行的任务列表。
  • name:这是任务的描述性名称,用于标识任务的用途。
  • stat:这个 Ansible 模块用于收集远程主机上由 path 参数指定的文件的信息。
  • register:将 stat 模块的输出存储在一个名为 file_info 的变量中,以便后续使用。
  • debug:在 playbook 执行期间打印调试信息。
  • msg:输出一条消息,其中包含使用 stat 模块检索到的文件的修改时间戳。

总结来说,这个 playbook 旨在检索本地主机上位于 /home/labex/project/file.txt 的特定文件的修改时间戳,并打印出时间戳。

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

ansible-playbook stat-module-playbook.yaml

观察输出,查看文件 file.txt 的修改时间戳(以人类可读的格式显示)。
示例输出:

[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 [Get file modification timestamp] *****************************************
ok: [localhost]

TASK [Print file modification timestamp] ***************************************
ok: [localhost] => {
    "msg": "File modification timestamp: 1710555624.2304714"
}

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

总结

恭喜!你已经成功完成了 Ansible Stat 模块的实验。你学会了如何使用 Ansible 中的 Stat 模块收集文件信息、检查文件是否存在以及获取文件的修改时间戳。

Stat 模块是一个强大的工具,能够帮助你收集远程主机上文件和目录的各种属性和信息。现在,你可以在 Ansible playbook 中利用这个模块执行高级的文件相关操作,并根据收集到的信息做出明智的决策。

继续探索 Ansible 文档,并尝试使用不同的模块来提升你的自动化技能。祝你玩转 Ansible!

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