将多个输出合并到一个变量中?

时间:2020-09-30 15:58:11

标签: 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
      - 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

1 个答案:

答案 0 :(得分:0)

我认为有可能。我试图在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}}"