使用ansible grep然后格式化输出

时间:2018-08-22 14:54:36

标签: grep ansible xdebug

我是Ansible的新手,想知道如何实现以下目标:

我有很多服务器。然后,我想对每一个运行grep命令,然后合并输出。

我想在每台服务器上运行

 egrep -i 'abc' /etc/fstab

然后我正在寻找如下输出:

hostname1:  abd.........
hostname1:  ......abc...
hostname2:  ....abc.....
hostname3:  .abc........

根据我的在线研究,我的剧本应如下所示:

- hosts: all

  tasks:
  - name: search abc filesystem
    shell: egrep -i 'abc' /etc/fstab
    register: result

  - debug:
    msg: "{{ ansible_play_hosts | map('extract', hostvars, 'result') | map(attribute='stdout_lines') }}"
    run_once: yes

  - name: Copy content
    local_action: copy content={{ ansible_play_hosts | map('extract', hostvars, 'result') | map(attribute='stdout') | list }} dest=/home/result.txt
    run_once: yes

但是,我无法使输出看起来像我想要的样子,并且grep命令似乎并没有在所有服务器上使用abc

如果有人能解释这样做的正确方法,将不胜感激。

0 个答案:

没有答案