首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >任务结果到文件

任务结果到文件
EN

Stack Overflow用户
提问于 2020-09-08 02:42:59
回答 1查看 308关注 0票数 1

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

目前,这是我的代码:

代码语言:javascript
复制
---

- 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    

尝试使用代码

代码语言:javascript
复制
---

- 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

它给了我一个错误:

代码语言:javascript
复制
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"}

主机通常我把它限制在极限柱上。文件的理想输出可能包含我输入的主机名和命令?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-09-08 07:49:18

你可能搞错了缩痕。尝试;

代码语言:javascript
复制
---
- 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”消息。如果该任务不存在,那么您的复制任务将失败。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63786438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档