아래는 Ansible 플레이북 또는 태스크 예시로, vrf 값이 주어지면 먼저 show ip arp vrf all 명령을 실행하고, 에러가 발생하거나 결과가 없으면 vrf 리스트를 순회하면서 show ip arp vrf <vrf_name> 명령을 실행하는 로직입니다.
yaml
- name: Define VRF list (예시)
set_fact:
vrf_list:
- vrf1
- vrf2
- vrf3
- name: Run show ip arp vrf all
cisco.ios.ios_command:
commands:
- show ip arp vrf all
register: arp_all
ignore_errors: yes
- name: Check if arp_all output is valid
set_fact:
arp_all_valid: "{{ (arp_all.failed is not defined or arp_all.failed == false) and (arp_all.stdout[0] | length > 0) }}"
- name: Run show ip arp vrf <vrf> commands if arp_all is invalid
when: not arp_all_valid
block:
- name: Initialize arp_vrf_results
set_fact:
arp_vrf_results: []
- name: Run show ip arp vrf for each VRF
vars:
current_vrf: "{{ item }}"
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ current_vrf }}"
register: arp_vrf_output
loop: "{{ vrf_list }}"
- name: Aggregate arp_vrf results
set_fact:
arp_vrf_results: "{{ arp_vrf_results + [ {'vrf': item.item, 'output': item.stdout[0]} ] }}"
loop: "{{ arp_vrf_output.results }}"
- name: Final output
debug:
msg: >-
{{
'show ip arp vrf all output' if arp_all_valid else 'show ip arp vrf <vrf> outputs',
arp_all.stdout[0] if arp_all_valid else arp_vrf_results
}}
설명
vrf_list에 VRF 이름들을 정의합니다. 실제 환경에 맞게 변경하세요.
show ip arp vrf all 명령을 실행하고 결과를 arp_all에 저장합니다.
결과가 실패하거나 출력이 없으면 arp_all_valid를 false로 설정합니다.
arp_all_valid가 false일 경우, vrf_list를 순회하며 각 VRF에 대해 show ip arp vrf <vrf> 명령을 실행합니다.
각 VRF별 결과를 arp_vrf_results 리스트에 저장합니다.
마지막에 결과를 출력합니다.
필요에 따라 이 코드를 역할(role)이나 플레이북에 맞게 조정해서 사용하세요.
User
register: 값은 다 같은 output_arp을 사용하게 할 수는 없을까?
Assistant
register 변수명을 모두 output_arp로 통일하는 것은 Ansible에서 기본적으로는 불가능합니다.
왜냐하면, 같은 태스크 내에서 loop를 돌면서 register를 하나로 지정하면 마지막 결과만 덮어쓰게 되고, 여러 번 실행되는 태스크가 있을 때는 register가 덮어써져서 이전 결과를 잃게 됩니다.
하지만, 다음과 같은 방법으로 output_arp 변수 하나에 모든 결과를 모으는 효과를 낼 수 있습니다.
방법 1. loop와 register를 함께 쓰고, 결과를 output_arp에 모으기
yaml
- name: Run show ip arp vrf for each VRF
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
loop: "{{ vrf_list }}"
register: output_arp
방법 2. show ip arp vrf all 실행 결과도 output_arp에 저장하고, 실패 시 VRF별 결과도 output_arp에 덮어쓰기
yaml
- name: Run show ip arp vrf all
cisco.ios.ios_command:
commands:
- show ip arp vrf all
register: output_arp
ignore_errors: yes
- name: Check if arp_all output is valid
set_fact:
arp_all_valid: "{{ (output_arp.failed is not defined or output_arp.failed == false) and (output_arp.stdout[0] | length > 0) }}"
- name: Run show ip arp vrf for each VRF if arp_all invalid
when: not arp_all_valid
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
loop: "{{ vrf_list }}"
register: output_arp
이렇게 하면:
처음에 output_arp에 show ip arp vrf all 결과가 저장되고,
실패하거나 결과가 없으면 output_arp가 VRF별 결과 리스트로 덮어써집니다.
전체 예시
yaml
- name: Define VRF list
set_fact:
vrf_list:
- vrf1
- vrf2
- vrf3
- name: Run show ip arp vrf all
cisco.ios.ios_command:
commands:
- show ip arp vrf all
register: output_arp
ignore_errors: yes
- name: Check if arp_all output is valid
set_fact:
arp_all_valid: "{{ (output_arp.failed is not defined or output_arp.failed == false) and (output_arp.stdout[0] | length > 0) }}"
- name: Run show ip arp vrf for each VRF if arp_all invalid
when: not arp_all_valid
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
loop: "{{ vrf_list }}"
register: output_arp
- name: Show final output
debug:
var: output_arp
요약
register 변수명은 태스크마다 다르게 지정하는 게 일반적이지만,
loop를 사용하면 한 번에 여러 결과를 register 하나에 모을 수 있다.
ignore_errors: yes와 조건문으로 실패 시 다른 명령을 실행하고, 같은 register 변수명으로 덮어쓰는 것도 가능하다.
필요하면 이 구조를 참고해서 사용하세요!
User
arp_output 이 변수는 어디서 온거야?
Assistant
arp_output 변수는 제가 예시 설명 중에 임의로 쓴 이름입니다.
원래 질문에서 사용하신 변수명은 output_arp였고, 제가 답변 작성하면서 혼동해서 arp_output이라고 쓴 부분이 있었던 것 같습니다.
즉, arp_output은 원래 존재하는 변수가 아니고, 그냥 예시로 쓴 이름입니다.
실제로는 output_arp 변수를 사용하시면 됩니다.
정리
질문에서 쓰신 변수명: output_arp
제가 예시에서 쓴 변수명 중 실수로 나온 것: arp_output
실제 코드에서는 output_arp 만 사용하세요.
예를 들어,
yaml
- name: Run show ip arp vrf all
cisco.ios.ios_command:
commands:
- show ip arp vrf all
register: output_arp
ignore_errors: yes
- name: Check if arp_all output is valid
set_fact:
arp_all_valid: "{{ (output_arp.failed is not defined or output_arp.failed == false) and (output_arp.stdout[0] | length > 0) }}"
이렇게 output_arp 변수를 계속 사용하시면 됩니다.
필요하면 제가 다시 output_arp 변수명으로 통일한 완성 예시를 만들어 드릴 수도 있습니다!
User
skipping: [G1DMZRTR1052] => (item=vrf-dmz-b2b-unctl) => {
"ansible_loop_var": "item",
"changed": false,
"false_condition": "arp_output is not defined or arp_output | length == 0",
"item": "vrf-dmz-b2b-unctl",
"skip_reason": "Conditional result was False"
이렇게 에러가 나는데 수정해줘
Assistant
에러 메시지를 보면 조건문에서 arp_output 변수가 없거나 길이가 0인지 체크하는데, 실제 변수명은 output_arp여야 하는데 arp_output으로 잘못 쓰여서 발생한 문제로 보입니다.
문제 원인
조건문에 arp_output 변수를 사용했는데, 실제로는 output_arp라는 변수를 사용하고 있음
그래서 arp_output 변수가 없어서 조건 평가가 제대로 안 됨
해결 방법
조건문에서 arp_output → output_arp로 변수명을 수정하세요.
예를 들어,
yaml
when: output_arp is not defined or output_arp | length == 0
또는 output_arp가 리스트가 아니라 register 결과 객체라면 output_arp.stdout 또는 output_arp.results 등 적절한 필드를 체크해야 합니다.
예시
yaml
- name: Run show ip arp vrf for each VRF if arp_all invalid
when: output_arp is not defined or output_arp.stdout | length == 0
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
loop: "{{ vrf_list }}"
register: output_arp
요약
arp_output → output_arp로 변수명 통일
조건문에서 변수 타입에 맞게 체크 (stdout, results 등)
is not defined 체크는 변수명이 정확해야 작동
필요하면 전체 코드를 변수명 맞춰서 다시 정리해 드릴 수 있습니다!
User
name: Execute 'show ip arp vrf' for each VRF
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
register: vrf_command_result
ignore_errors: yes
loop: "{{ vrf_list }}"
loop_control:
label: "{{ item }}"
이 코드에서 아래의 에러가 나오는데 수정해줘
skipping: [G1DMZRTR1052] => (item=vrf-dmz-b2b-unctl) => { "ansible_loop_var": "item", "changed": false, "false_condition": "arp_output is not defined or arp_output | length == 0", "item": "vrf-dmz-b2b-unctl", "skip_reason": "Conditional result was False"
Assistant
주신 에러 메시지와 플레이북 코드 일부를 보면, skipping이 발생한 이유는 when 조건문에서 arp_output 변수를 체크하는데, 실제로는 arp_output 변수가 없거나, 변수명이 다르기 때문입니다.
문제 분석
when 조건에 "arp_output is not defined or arp_output | length == 0"가 있는데,
실제로는 vrf_command_result 또는 output_arp 같은 다른 변수명을 쓰고 있음
따라서 arp_output 변수가 없어서 조건이 False가 되어 태스크가 스킵됨
해결 방법
변수명 통일 when 조건문에서 사용하는 변수명을 실제 register 변수명과 맞춰야 합니다.
변수 타입에 맞게 조건 수정 register 변수는 보통 dict 형태이고, length를 바로 쓸 수 없으니 stdout 또는 results 등 적절한 필드를 체크해야 합니다.
예시 수정
yaml
- name: Execute 'show ip arp vrf' for each VRF
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
register: vrf_command_result
ignore_errors: yes
loop: "{{ vrf_list }}"
loop_control:
label: "{{ item }}"
when: vrf_command_result is not defined or vrf_command_result.results | length == 0
하지만 위처럼 when 조건을 같은 태스크에 쓰면 안 됩니다.
왜냐하면 vrf_command_result는 이 태스크가 실행된 후에 생성되기 때문입니다.
올바른 구조 예시
yaml
- name: Check if arp_output is valid
set_fact:
arp_output_valid: "{{ (output_arp is defined) and (output_arp.stdout[0] | length > 0) }}"
- name: Execute 'show ip arp vrf' for each VRF if arp_output is invalid
cisco.ios.ios_command:
commands:
- "show ip arp vrf {{ item }}"
register: vrf_command_result
ignore_errors: yes
loop: "{{ vrf_list }}"
loop_control:
label: "{{ item }}"
when: not arp_output_valid
요약
when 조건에서 사용하는 변수명과 register 변수명을 반드시 일치시켜야 함
when 조건은 해당 태스크 실행 전에 평가되므로, 이전 태스크에서 변수 생성이 완료되어 있어야 함
register 변수는 dict 형태이므로, length 체크 시 적절한 필드(stdout, results)를 사용해야 함