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プレイブックファイル /home/labex/project/stat-module-playbook.yaml を作成し、テキストエディタで開きます。
プレイブックファイルに以下の内容を追加します。

- 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: これは、プレイブックを実行する対象のホストを指定します。この場合、対象ホストが localhost なので、プレイブックはローカルホストで実行されます。
  • tasks: 実行するタスクのリストです。
  • name: これは、タスクの目的を識別するためのタスクの説明的な名前です。
  • stat: このAnsibleモジュールは、path パラメータで指定されたリモートホスト上のファイルに関する情報を収集します。
  • register: stat モジュールの出力を、後で使用するために file_info という名前の変数に保存します。
  • debug: プレイブックの実行中にデバッグ情報を表示します。
  • msg: stat モジュールを使って取得したファイル情報を含むメッセージを出力します。これには、ファイルサイズ、所有者(UIDとGID)、および権限が含まれます。

要するに、このプレイブックは、ローカルホスト上の /home/labex/project/file.txt にある特定のファイルに関する情報を取得し、サイズ、所有者、権限などの様々な詳細を表示するように設計されています。

次に、/home/labex/project ディレクトリに file.txt という名前のファイルを作成します。

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

最後に、以下のコマンドでプレイブックを実行します。

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モジュールを使って、リモートホスト上のファイルの存在を確認します。

まず、既存のプレイブックファイルの内容をすべて削除し、以下の内容を追加して修正します。

- 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: これは、プレイブックを実行する対象のホストを指定します。この場合、対象ホストが localhost なので、プレイブックはローカルホストで実行されます。
  • tasks: 実行するタスクのリストです。
  • name: これは、タスクの目的を識別するためのタスクの説明的な名前です。
  • stat: このAnsibleモジュールは、path パラメータで指定されたリモートホスト上のファイルに関する情報を収集します。
  • register: stat モジュールの出力を、後で使用するために file_info という名前の変数に保存します。
  • debug: プレイブックの実行中にデバッグ情報を表示します。
  • msg: stat モジュールを使って取得した情報に基づいて、ファイルが存在するかどうかを示すメッセージを出力します。

要するに、このプレイブックは、ローカルホスト上の /path/on/remote/host/file.txt にある特定のファイルの存在を確認し、そのファイルが存在するかどうかを示すメッセージを表示するように設計されています。

次に、以下のコマンドでプレイブックを実行します。

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モジュールを使って、リモートホスト上のファイルの変更タイムスタンプを取得します。

まず、既存のプレイブックファイルの内容をすべて削除し、以下の内容を追加して修正します。

- 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: これは、プレイブックを実行する対象のホストを指定します。この場合、対象ホストが localhost なので、プレイブックはローカルホストで実行されます。
  • tasks: 実行するタスクのリストです。
  • name: これは、タスクの目的を識別するためのタスクの説明的な名前です。
  • stat: このAnsibleモジュールは、path パラメータで指定されたリモートホスト上のファイルに関する情報を収集します。
  • register: stat モジュールの出力を、後で使用するために file_info という名前の変数に保存します。
  • debug: プレイブックの実行中にデバッグ情報を表示します。
  • msg: stat モジュールを使って取得したファイルの変更タイムスタンプを含むメッセージを出力します。

要するに、このプレイブックは、ローカルホスト上の /home/labex/project/file.txt にある特定のファイルの変更タイムスタンプを取得し、そのタイムスタンプを表示するように設計されています。

次に、以下のコマンドでプレイブックを実行します。

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のプレイブックでこのモジュールを活用して、高度なファイル関連の操作を行い、収集した情報に基づいて適切な判断を下すことができます。

Ansibleのドキュメントをさらに探求し、さまざまなモジュールを試して、オートメーション技術を向上させましょう。Ansibleを楽しんでください!