任务结果到文件

问题描述

我有一个运行Cisco nxos命令的简单剧本,该剧本运行成功。 想知道什么代码将所有结果保存到文件中,而不管我拥有多少台主机,并使用Survey输入文件名。

当前,这是我的代码

---

- name: run multiple commands on remote nodes
  nxos_command:
    commands:
      - show clock
      - show int status
      - show cdp neigh
      - show int desc
      - show port-channel summ
      - show vpc
      - show vpc role    

尝试使用代码

---

- name: run multiple commands on remote nodes
register: myshell_output

nxos_command:
 commands:
   - show clock
   - show int status
   - show cdp neigh
   - show int desc
   - show port-channel summ
   - show vpc
   - show vpc role
   

   - name: Saving data to local file
     copy:
       content: "{{ myshell_output.stdout|join('\n') }}"
       dest: "/tmp/hello.txt"
     delegate_to: localhost

它给我一个错误

Failed! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleunsafeText object' has no attribute 'stdout'\n\nThe error appears to be in '/tmp/awx_1869_7__9l_9l/project/roles/bcpcommands/tasks/main.yml': line 3,column 3,but may\nbe elsewhere in the file depending on the exact Syntax problem.\n\nThe offending line appears to be:\n\n\n- name: run multiple commands on remote nodes\n  ^ here\n"}

通常,主机在Ansible-Tower LIMIT列中对其进行限制。 文件的理想输出可能包含主机名和我键入的命令?

谢谢

解决方法

您可能缩进错了。试试;

---
- hosts: my_host
  tasks:
    - name: run multiple commands on remote nodes
      nxos_command:
        commands: "{{ item }}"
      loop:
        - show clock
        - show int status
        - show cdp neigh
        - show int desc
        - show port-channel summ
        - show vpc
        - show vpc role
      register: myshell_output

    - debug:
        msg: "{{ myshell_output }}"

    - name: Saving data to local file and include hostname
      copy:
        content: "{{ myshell_output.stdout|join('\n') }} hostname: {{ inventory_hostname }}"
        dest: "/tmp/hello.txt"
      delegate_to: localhost

编辑主机名。

调试任务必须输出“ stdout”消息。如果该文件夹不存在,那么您的复制任务将失败。