【问题标题】:Ansible with host specific files, but fallback to default filesAnsible 与主机特定文件,但回退到默认文件
【发布时间】:2019-06-30 02:32:31
【问题描述】:

我目前正在尝试习惯 Ansible,但我未能实现看似常见的用例:

假设我在roles/nginx 中有一个角色nginx,其中一项任务是设置自定义默认页面:

- name: install nginx default page
  copy:
    src: "index.html"
    dest: /var/www/html/
    owner: root
    mode: 0644

Ansible 将在以下位置查找文件:

roles/nginx/files
roles/nginx
roles/nginx/tasks/files
roles/nginx/tasks
files
./

现在由于某种原因,单个主机应该收到完全不同的文件。 我知道我可以将文件 src 路径更改为 src: "{{ inventory_hostname }}/index.html" 但它只会在特定于主机的目录中搜索。

有没有办法改变搜索路径,以便 Ansible 首先在特定于主机的目录中查找文件,然后回退到公共目录?

我不想在编写角色时决定文件是否需要特定于主机。我宁愿覆盖角色默认文件而不改变基本角色。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    问:“Is there a way to alter the search paths so that Ansible will look for files in host-specific directories first but fall-back to common directories?

    A:一般来说,这种方式是无法改变搜索路径的。但是,使用first_found 可以定义如何搜索特定文件。例如

    - copy:
        src: "{{ lookup('first_found', findme) }}"
        dest: /scratch/tmp/
        owner: root
        mode: 0644
      vars:
        findme:
          - "{{ inventory_hostname }}/index.html"
          - "{{ role_path }}/files/index.html"
          - "{{ role_path }}/files/defaults/index.html"
    

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2021-09-11
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多