Ansible의 원시 모듈을 실행 중입니다.루트가 아닌 사용자 사용어떤 조건을 확인하는 명령을 내리고 싶습니다. 예를 들어 디렉토리를 삭제하기 전에 디렉토리가 존재하는지 여부를 확인하고 싶습니다. 다음 명령과 같습니다.
[ -d "$DIR" ] && echo "Yes"
이제 루트가 아닌 사용자에게 sudoers 파일에서 이 명령을 실행할 수 있는 권한을 부여하고 싶습니다.
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