Ansible发送文件到第一个遇到的目的地 [英] Ansible send file to the first met destination

查看:420
本文介绍了Ansible发送文件到第一个遇到的目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为成千上万个节点发送一个配置文件,因为有一些定制可能有5或6个路径的文件(主机只有一个文件,但路径可以变化),并没有一个简单的方法用事实确定默认位置。基于这一点,我正在寻找一些方法来设置复制模块的dest,就像我们可以设置src,用一个

< http://docs.ansible.com/ansible/playbooks_loops.html#finding-first-matched-files =nofollow> with_first_found 循环



像这样:

  copy:src = / foo / {{ansible_hostname}} / nrpe.cfg dest ={{item}} 
with_items:
- /etc/nagios/nrpe.cfg
- / usr / local / nagios / etc / nrpe.cfg
- /usr/lib64/nagios/etc/nrpe.cfg
- /usr/lib/nagios/etc/nrpe.cfg
- /opt/nagios/etc/nrpe.cfg

PS:我发送nrpe。



编辑1 :I' ve设法使用@ydaetskcoR的帮助,像这样:

   -  name:find nrpe.cfg 
stat:
path:{{item}}
with_items:
- /etc/nagios/nrpe.cfg
- / usr / local / nagios / etc / nrpe .cfg
- /usr/lib64/nagios/etc/nrpe.cfg
- /usr/lib/nagios/etc/nrpe.cfg
- / opt / nagios / etc / nrpe.cfg
register:nrpe_stat
no_log:True

- name:Copy nrpe.cfg
copy:src = / foo / {{ ansible_hostname}} / nrpe.cfg dest ={{item.stat.path}}
当:item.stat.exists
no_log:True
with_items:
- {{nrpe_stat.results}}


解决方案

可以简单地搜索已经存在的 nrpe.cfg 文件,然后将该位置注册为要用于复制任务的变量。



你可以通过使用 find 的shell / command任务来执行,也可以通过 stat 检查是否存在。



所以你可能有这样的:

   -  name:find nrpe.cfg 
shell:find / -name nrpe.cfg
register:nrpe_path

- name:overwrite nrpe.cfg
copy:src = / foo / {{ansible_hostname}} / nrpe .cfg dest ={{item}}
with_items:
- nrpe_path.stdout_lines
时间:nrpe_path.stdout!=
注册:nrpe_copied
b $ b - name:将nrpe.cfg复制到框(如果还没有)
copy:src = / foo / {{ansible_hostname}} / nrpe.cfg dest ={{default_nrpe_path}}
:nrpe_copied未定义

Mxx 在评论中指出,我们有第三个任务回退到复制到某个默认路径(可能 / etc / nagios / 或者任何其他路径真的)如果 find 没有找到 nrpe.cfg

要使用 stat 而不是shell / command任务,你可以这样做:

   -  name:find nrpe.cfg 
stat:
path:{{item}}
with_items:
- /etc/nagios/nrpe.cfg
- /usr/local/nagios/etc/nrpe.cfg
- /usr/lib64/nagios/etc/nrpe.cfg
- /usr/lib/nagios/etc/nrpe.cfg
- /opt/nagios/etc/nrpe.cfg
注册:nrpe_stat

- name:overwrite nrpe.cfg
copy:src = / foo / {{ansible_hostname}} / nrpe.cfg dest ={{item.stat.path}}
当:item.stat.exists
with_items:
- {{nrpe_stat.results}}


I'm sending a config file for thousands of nodes, because of some customisation there's maybe 5 or 6 paths to that file (There's only one file for host but the path can vary) and there isn't a easy way to figure out the default location with facts.

Based on this, I'm looking for some way to set the "dest" of copy module like we can set the "src", with a with_first_found loop.

Something like that:

copy: src=/foo/{{ ansible_hostname }}/nrpe.cfg dest="{{item}}
with_items:
    - "/etc/nagios/nrpe.cfg"
    - "/usr/local/nagios/etc/nrpe.cfg"
    - "/usr/lib64/nagios/etc/nrpe.cfg"
    - "/usr/lib/nagios/etc/nrpe.cfg"
    - "/opt/nagios/etc/nrpe.cfg"

PS: I'm sending nrpe.cfg so if someone knows a better way to find where's the default nrpe.cfg it will be a lot easier.

EDIT 1: I've managed to work with the help from @ydaetskcoR like this:

- name: find nrpe.cfg
  stat:
    path: "{{ item }}"
  with_items:
    - "/etc/nagios/nrpe.cfg"
    - "/usr/local/nagios/etc/nrpe.cfg"
    - "/usr/lib64/nagios/etc/nrpe.cfg"
    - "/usr/lib/nagios/etc/nrpe.cfg"
    - "/opt/nagios/etc/nrpe.cfg"
  register: nrpe_stat
  no_log: True

- name: Copy nrpe.cfg
  copy: src=/foo/{{ ansible_hostname }}/nrpe.cfg dest="{{item.stat.path}}"
  when: item.stat.exists
  no_log: True
  with_items:
    - "{{nrpe_stat.results}}"

解决方案

One option could be to simply search for the already existing nrpe.cfg file and then register that location as a variable to be used for the copy task.

You could do that either through a shell/command task that just uses find or loop through a bunch of locations with stat to check if they exist.

So you might have something like this:

- name: find nrpe.cfg
  shell: find / -name nrpe.cfg
  register: nrpe_path

- name: overwrite nrpe.cfg
  copy: src=/foo/{{ ansible_hostname }}/nrpe.cfg dest="{{item}}"
  with_items:
    - nrpe_path.stdout_lines
  when: nrpe_path.stdout != ""
  register: nrpe_copied

- name: copy nrpe.cfg to box if not already there
  copy: src=/foo/{{ ansible_hostname }}/nrpe.cfg dest="{{ default_nrpe_path }}"
  when: nrpe_copied is not defined

As Mxx pointed out in the comments, we have a third task to fall back to copying to some default path (potentially /etc/nagios/ or any other path really) if the nrpe.cfg file hasn't been found by find.

To use stat rather than a shell/command task you could do something like this:

- name: find nrpe.cfg
  stat: 
    path: {{ item }}
  with_items:
    - "/etc/nagios/nrpe.cfg"
    - "/usr/local/nagios/etc/nrpe.cfg"
    - "/usr/lib64/nagios/etc/nrpe.cfg"
    - "/usr/lib/nagios/etc/nrpe.cfg"
    - "/opt/nagios/etc/nrpe.cfg"
  register: nrpe_stat

- name: overwrite nrpe.cfg
  copy: src=/foo/{{ ansible_hostname }}/nrpe.cfg dest="{{item.stat.path}}"
  when: item.stat.exists
  with_items:
    - "{{nrpe_stat.results}}"

这篇关于Ansible发送文件到第一个遇到的目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆