Ansible 将文件发送到第一个遇到的目的地 [英] Ansible send file to the first met destination
问题描述
我正在为数千个节点发送一个配置文件,由于某些自定义,该文件可能有 5 或 6 个路径(主机只有一个文件,但路径可能会有所不同)并且没有一种简单的方法可以用事实找出默认位置.
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.
基于此,我正在寻找某种方法来设置复制模块的dest",就像我们可以设置src"一样,使用 with_first_found
循环.
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.
类似的东西:
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.cfg,所以如果有人知道找到默认 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:我设法在@ydaetskcoR 的帮助下像这样工作:
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}}"
推荐答案
一种选择可能是简单地搜索已经存在的 nrpe.cfg
文件,然后将该位置注册为要使用的变量用于复制任务.
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.
您可以通过仅使用 find
的 shell/命令任务来执行此操作,也可以使用 stat
检查它们是否存在.
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
正如 Mxx 在评论中指出的那样,我们还有第三个任务要回退到复制到某个默认路径(如果 find
没有找到 nrpe.cfg
文件,则可能是 /etc/nagios/
或任何其他路径.
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
.
要使用 stat
而不是 shell/命令任务,您可以执行以下操作:
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屋!