![Ansible: 블록 구조 블록이 사용되는 경우에도 include_tasks에서 발생하는 치명적인 오류](https://rvso.com/image/717732/Ansible%3A%20%EB%B8%94%EB%A1%9D%20%EA%B5%AC%EC%A1%B0%20%EB%B8%94%EB%A1%9D%EC%9D%B4%20%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94%20%EA%B2%BD%EC%9A%B0%EC%97%90%EB%8F%84%20include_tasks%EC%97%90%EC%84%9C%20%EB%B0%9C%EC%83%9D%ED%95%98%EB%8A%94%20%EC%B9%98%EB%AA%85%EC%A0%81%EC%9D%B8%20%EC%98%A4%EB%A5%98.png)
하나의 호스트 변수에 정의된 일부 값을 기반으로 여러 작업을 포함하려고 하는데 block-rescue 블록을 사용하는 경우에도 일부 치명적인 오류가 발생합니다. 변수가 있어요프로필에 정의됨호스트_vars/hostname.yml:
profiles: '["profile1","trap1"]'
그리고 역할테스트_프로필~에/etc/ansible/roles. 여기 작업 디렉터리에는 다음과 같은 .yml 파일이 있습니다.profile1.yml, profile2.yml, main.yml.
main.yml 파일의 내용은 다음과 같습니다.
- name: import profiles
block:
- include_tasks: "{{ item }}.yml"
with_items: "{{ profiles|default([]) }}"
rescue:
- debug: msg='Error encountered while trying to include a .yml file corresponding to a defined profile from profiles variable. Profiles - "{{ profiles }}"'
when: profiles is defined
플레이북의 내용은 다음과 같습니다.
- name: Test profiles config
hosts: myhost
roles:
- test_profiles
출력은 다음과 같습니다.
TASK [test_profiles : include_tasks] ***********************************************************************************************************************************************************************
included: /etc/ansible/roles/test_profiles/tasks/profile.yml for <my_host>
fatal: [<my_host>]: FAILED! => {"reason": "Unable to retrieve file contents\nCould not find or access '/etc/ansible/trap1.yml'"}
TASK [test_profiles : Ensure that profile directory exists into the D:\profiles directory] ************************************************************************************************
ok: [<my_host>]
TASK [test_profiles : Update profile.properties file] ***************************************************************************************************************************************************
ok: [<my_host>]
TASK [test_profiles : debug] *******************************************************************************************************************************************************************************
ok: [<my_host>] => {
"msg": "Error encountered while trying to include a .yml file corresponding to a defined profile from profiles variable. Profiles - \"[\"profile1\",\"trap1\"]\""
}
to retry, use: --limit @/etc/ansible/playbook_test_profiles.retry
PLAY RECAP ********************************************************************************************************************************************************************************************************
<my_host> : ok=5 changed=0 unreachable=0 failed=1
내 관점에서는 치명적인 오류가 나타나서는 안된다고 생각합니다. 여기서 내가 뭘 잘못하고 있고 어떻게 해결할 수 있습니까? 나는 또한언제조건부이지만 성공하지 못했습니다.
나는 사용하고있다앤서블 2.4.2.0일부 Windows 호스트에 대해 작업이 수행됩니다.
답변1
block
/는 rescue
오류 발생을 방지하지 않습니다. 실패한 작업을 감지하고 rescue
블록을 실행하여 오류를 복구합니다. 그러나 실패한 작업은 여전히 존재하며 플레이 요약에 표시됩니다.
플레이북을 디자인할 때 빠른 실패 접근 방식을 사용하는 것이 좋습니다. 귀하의 경우 로컬 파일을 스캔하여 사용자 입력(제공된 구성)이 유효한지, 즉 프로필 파일이 있는지 테스트할 수 있습니다. 사용자 assert
/ fail
모듈.
답변2
결국 나는 블록 구조 블록을 제거했습니다. 내 .yml 파일이 존재하는지 확인하는 방법을 찾았습니다. 이는역할_경로변수(현재 역할의 경로를 반환합니다 - Ansible 1.8부터 사용 가능 - 역할 내부에서만 작동함) 및 테스트is_file.
위에서 설명한 역할에 대한 내 main.yml은 다음과 같습니다.
- name: Import profiles
include_tasks: "{{ item }}.yml"
with_items: "{{ profiles|default([]) }}"
when: (role_path + '/tasks/' + item + '.yml') | is_file
이 검사로 인해 치명적인 예외가 더 이상 발생하지 않습니다. Trap1.yml 파일을 건너뜁니다.
출력은 다음과 같습니다.
TASK [test_profiles : Import profiles] ***********************************************************
skipping: [<my_host>] => (item=trap1)
included: /etc/ansible/roles/test_profiles/tasks/profile1.yml for <my_host>
TASK [test_profiles : Ensure that profile directory exists into the D:\profiles directory] *******
ok: [<my_host>]
TASK [test_profiles : Update profile.properties file] ********************************************
changed: [<my_host>]
to retry, use: --limit @/etc/ansible/playbook_test_profiles.retry
PLAY RECAP ***************************************************************************************
<my_host> : ok=4 changed=1 unreachable=0 failed=0
이 솔루션에는 문제가 없지만 다른 제안도 받아들일 수 있습니다.