Ansible 将多个输出组合成一个变量?
是否有可能将多个输出组合成一个变量以从中提取信息Ansible 将多个输出组合成一个变量?,ansible,Ansible,是否有可能将多个输出组合成一个变量以从中提取信息 --- - name: Get info ios_command: commands: - show run int GigabitEthernet0/1.10 | in ip add - show run int GigabitEthernet0/1.20 | in ip add - show run int GigabitEthernet0/1.30 | in ip add - s
---
- name: Get info
ios_command:
commands:
- show run int GigabitEthernet0/1.10 | in ip add
- show run int GigabitEthernet0/1.20 | in ip add
- show run int GigabitEthernet0/1.30 | in ip add
- show run int GigabitEthernet0/1.40 | in ip add
register: results_info
- set_fact:
info2: "{{ results_info.stdout[0].split(' ') }}"
我尝试了几种不同的方法,但似乎只有一行保存在输出中
我尝试了下面的建议。。。从剧本上逃跑
---
- name: Get info
ios_command:
commands: "{{ item }}"
with_items:
- echo "show run int GigabitEthernet0/1.10 | in ip add"
- echo "show run int GigabitEthernet0/1.20 | in ip add"
- echo "show run int GigabitEthernet0/1.30 | in ip add"
- echo "show run int GigabitEthernet0/1.40 | in ip add"
register: echo_output
- set_fact:
info: "{{ echo_output.results}}"
- debug:
msg: "{{ echo_output.results}}"
我得到的错误是
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined
我认为这是可能的。我尝试在linux中运行多个echo命令,并能够将所有结果捕获到变量中。以下是我尝试过的:
---
- name: Test Ansible
hosts: localhost
gather_facts: false
tasks:
- name: echo
command: "{{ item }}"
with_items:
- echo "Hi1"
- echo "Hi2"
- echo "Hi3"
register: echo_output
- set_fact:
info: "{{ echo_output.results}}"
- debug:
msg: "{{ echo_output.results}}"
为了帮助理解,请添加如何执行此代码(cmdline)以及建议将此代码放置在何处。@dpetrini我用完整的剧本文件更新了代码。您可以使用以下语法从命令行运行playbook:ansible playbook。ymlI试图合并到我的代码中,但在尝试运行时出错。@jmyths,您看到的错误是什么?我可以运行上面的代码,而不会在我的机器上出现任何问题。