[ansible] directory 가 없을 때 생성하기 (if directory not exists, make the directory)
특정 디렉토리가 없으면 생성하게 하는 것을 ansible에서는 이렇게 표현가능하다.
- name: If exists
debug: msg='/app/src exists'
when: check_path.stat.exists
- name: If not exists
debug: msg='/app/src is not exists, so ansible make /app/src '
when: check_path.stat.exists == false
- name: If not exists 2
file: path=/app/src state=directory
when: check_path.stat.exists == false
<결과>
첫번째 실행
TASK: [tomcat1 | If exists] ***************************************************
skipping: [192.168.1.50]
TASK: [tomcat1 | If not exists] ***********************************************
ok: [192.168.1.50] => {
"item": "",
"msg": "/app/src is not exists, so ansible make /app/src "
}
TASK: [tomcat1 | If not exists 2] *********************************************
changed: [192.168.1.50]
두번째 실행
TASK: [tomcat1 | If exists] ***************************************************
ok: [192.168.1.50] => {
"item": "",
"msg": "/app/src exists"
}
TASK: [tomcat1 | If not exists] ***********************************************
skipping: [192.168.1.50]
TASK: [tomcat1 | If not exists 2] *********************************************
skipping: [192.168.1.50]
debug를 빼면 task 하나를 줄일 수 있다.
- name: If exists
debug: msg='/app/src exists'
when: check_path.stat.exists
- name: If not exists
file: path=/app/src state=directory
when: check_path.stat.exists == false