restart rsyslog 태스크 수행 ▶ install apache2 에러 ▶ print msg 핸들러 실행
💠 작업 실패 조건 지정
쉘 스크립트 실행 결과가 실패 또는 에러 ▶ 앤서블에서는 작업 성공으로 간주
failed_when
작업이 실패 했음을 나타내는 조건 지정
사용자 추가 쉘 스크립트 ▶ tnode1 의 root 폴더에 생성 adduser-script.sh
#!/bin/bash
# 사용자 계정 및 패스워드가 입력되었는지 확인
if [[ -n $1 ]] && [[ -n $2 ]]
then
UserList=($1)
Password=($2)
# for문을 이용하여 사용자 계정 생성
for (( i=0; i < ${#UserList[@]}; i++ ))
do
# if문을 사용하여 사용자 계정이 있는지 확인
if [[ $(cat /etc/passwd | grep ${UserList[$i]} | wc -l) == 0 ]]
then
# 사용자 생성 및 패스워드 설정
useradd ${UserList[$i]}
echo ${Password[$i]} | passwd ${UserList[$i]} --stdin
else
# 사용자가 있다고 메시지를 보여줌.
echo "this user ${UserList[$i]} is existing."
fi
done
else
# 사용자 계정과 패스워드를 입력하라는 메시지를 보여줌.
echo -e 'Please input user id and password.\nUsage: adduser-script.sh "user01 user02" "pw01 pw02"'
fi
ansible_builtin.shell 모듈 ▶ adduser-script.sh 실행
failed_when 구문
command_result.stdout 변수에 "please input user id and passsword" 문자열이 있으면 작업을 실패로 처리
- hosts: tnode1-centos.exp.com
tasks:
- name: run user add script
ansible.builtin.shell: /root/adduser-script.sh
register: command_result
failed_when: "'please input user id and password' in command_result.stdout"
- name: print msg
ansible.builtin.debug:
msg: "this is next task"
ansible-playbook 실행
플레이북에서 failed_when 구문을 사용하지 않았다면 ▶ 태스트가 실패했는지 모른 채 다음 태스크 실행
failed_when 구문 삭제 ignore_errors: yes 구문 추가
ansible.builtin.fail: 모듈과 when 구문 이용
Please input user id and password 문자열이 :있으면 메시지 출력하고 실패 처리 태스크 추가
- hosts: tnode1-centos.exp.com
tasks:
- name: run user add script
ansible.builtin.shell: /root/adduser-script.sh
register: command_result
ignore_errors: yes
- name: report script failure
ansible.builtin.fail:
msg: "{{ command_result.stdout }}"
when: "'Please input user id and password' in command_result.stdout"
ansible-playbook 실행
run user add script 태스크는 정상 실행 ▶ report script failure 태스크는 실패 ▶ 플레이 중단
💠 앤서블 블록 및 오류처리
block
오류를 제어하는 문법
작업을 논리적으로 그룹화하는 절
작업 실행 방법을 제어하는 데 사용
rescue 문과 alwayys 문을 함께하여 오류 처리
block
실행할 기본 작업 정의
rescue
block 절에 정의된 작업이 실패하는 경우 실행할 작업 정의
always
block 및 rescue 절에 정의된 작업 성공/실패 관계없이 항상 실행되는 작업 정의
block
ansible.builtin.find: 모듈 ▶ /var/log/daily_log 디렉토리 검색 ▶ 없으면 warning 발생 ▶ 다음 태스크로
failed_when 구문 ▶ result.msg 변수에 Not all paths 메시지 발견되면 실패 처리
rescue
디렉터리가 없으면 디렉터리를 생성
always
로그 파일 생성
- hosts: tnode2-ubuntu.exp.com
vars:
logdir: /var/log/daily_log
logfile: todays.log
tasks:
- name: configure log env
block:
- name: find directory
ansible.builtin.find:
path: "{{ logdir }}"
register: result
failed_when: "'Not all paths' in result.msg"
rescue:
- name: make directory when not found directory
ansible.builtin.file:
path: "{{ logdir }}"
state: directory
mode: '0755'
always:
- name: create file
ansible.builtin.file:
path: "{{ logdir }}/{{ logfile }}"
state: touch
mode: '0644'
ansible-playbook 실행
find directory 태스크
디렉터리을 찾지 못해 오류 발생
make directory when not found directory 태스크
디렉터리 생성
create file 태스크
파일 생성
tnode2-ubuntu 노드에서 확인
ansible-playbook 실행
rescue 구문에 make directory when not found directory 태스크 는 실행하지 않음