Ansible の raw module ステートメントでテストを実行するにはどうすればいいですか?

Ansible の raw module ステートメントでテストを実行するにはどうすればいいですか?

私はAnsibleの生のモジュールを実行しています非ルートユーザーの使用そして、何らかの条件をチェックするコマンドを指定したいとします。たとえば、ディレクトリを削除する前に、そのディレクトリが存在するかどうかをチェックしたいとします。次のコマンドのように。

[ -d "$DIR" ] && echo "Yes"

ここで、sudoers ファイルでこのコマンドを実行する権限を非 root ユーザーに付与します。

xyzUser ALL = (ALL) NOPASSWD: 'something/that/lets/me/run/that/command'

したがって、このコマンドと他のすべての条件ベースのコマンドを実行できるようにするために、sudoers に何を書き込めるかを知りたいのです。

答え1

たとえば、ディレクトリを削除する前に、そのディレクトリが存在するかどうかを確認したいとします。

- name: Example playbook to make sure a dir is absent
  hosts: my_host_group
  # Connect with a non-root user
  remote_user: my_remote_user
  # Use sudo, as you say you need it. Configure it on the relevant host
  become: true 

  vars:
    dir_to_remove: /path/to/dir/on/host

  tasks:
    - name: "remove {{ dir_to_remove }} if exists. Do nothing otherwise"
      file:
        path: "{{ dir_to_remove }}"
        state: absent

関連情報