【问题标题】:ansible multiple per-host output to fileansible 多个每主机输出到文件
【发布时间】:2017-09-18 01:44:21
【问题描述】:

我想在多个主机上运行 Ansible 剧本并将输出注册到一个变量。现在使用这个变量,我想将输出复制到单个文件。问题是最终文件中只有一个主机的输出。如何将所有主机的输出一个接一个地添加到一个文件中。我不想使用serial = 1,因为如果我们有多个主机,它会大大减慢执行速度。

-

     hosts: all
      remote_user: cisco
      connection: local
      gather_facts: no

      vars_files:
      - group_vars/passwords.yml

      tasks:
      - name: Show command collection
        ntc_show_command:
          connection: ssh
          template_dir: /ntc-ansible/ntc-templates/templates
          platform: cisco_ios
          host: "{{ inventory_hostname }}"
          username: "{{ ansible_ssh_user }}"
          password: "{{ ansible_ssh_pass }}"
          command: "{{commands}}"
        register: result

      - local_action:
          copy content="{{result.response}}" dest='/home/user/show_cmd_ouput.txt'

【问题讨论】:

    标签: ansible


    【解决方案1】:

    result 变量将在每台运行任务ntc_show_command 的主机上注册为事实,因此您应该通过hostvars 字典访问该值。

    - local_action:
        module: copy
        content: "{{ groups['all'] | map('extract', hostvars, 'result') | map(attribute='response') | list }}"
        dest: /home/user/show_cmd_ouput.txt
      run_once: true
    

    您还需要run_once,因为该操作仍将运行与组中主机一样多的次数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2020-09-04
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多