如何在 Ansible playbook 中的一个文件中获取多个主机输出?

问题描述 投票:0回答:1

我需要收集多台主机上的所有服务运行状态并将其放入一个输出文件中。

- name: To check status of services running
  hosts: all

  tasks:

    - name: "Check backup service"
      shell:|
         if ps -ef | grep 'netbackup' | grep =v grep > /dev/null; then
                echo ' backup service running'
         else
                echo ' backup service not running'
         fi
      ignore_errors: false
      register: ps_cmd

我想在多个主机上运行上述剧本并将输出收集在一个文件中。

ansible ansible-2.x ansible-inventory ansible-template
1个回答
0
投票

您可以运行下面的

playbook.yml
并通过 grep 将其通过管道传输到文件中:

ansible-playbook playbook.yml|grep running > report.txt
- name: To check status of backups
  hosts: all
  gather_facts: false

  tasks:

    - name: "Check backup service"
      shell: |
         if ps -ef | grep 'netbackup' | grep -v grep > /dev/null; then
           echo "$(hostname) backup service running"
         else
           echo "$(hostname) backup service not running"
         fi
      changed_when: false
      register: ps_cmd

    - name: "Results"
      debug:
        msg: "{{ ps_cmd.stdout_lines }}"
© www.soinside.com 2019 - 2024. All rights reserved.