ansibleでファイルの存在確認を行う方法

Ansibleでは、ファイルが存在するかどうかを判断するためにstatモジュールを使用できます。以下に、statモジュールを使用する例を示します。

- name: Check if file exists
  stat:
    path: /path/to/file
  register: result

- name: Print file existence status
  debug:
    msg: "File exists"
  when: result.stat.exists

- name: Print file does not exist status
  debug:
    msg: "File does not exist"
  when: not result.stat.exists

上の例では、statモジュールが指定したパスのファイルの存在 여부를 확인し、その結果をresultという変数に格納します。その後、when条件分岐を使用して、result.stat.existsの値に応じてさまざまなタスクを実行できます。ファイルが存在する場合は、最初のデバッグタスクが実行され、「ファイルが存在します」というメッセージが出力されます。ファイルが存在しない場合は、2番目のデバッグタスクが実行され、「ファイルが存在しません」というメッセージが出力されます。

bannerAds