如果找不到文件,则Ansible include_vars继续

joa*_*ura 7 ansible ansible-playbook

如果我有这样的事情:

- include_vars: this_file_doesnt_exist.yml
Run Code Online (Sandbox Code Playgroud)

ansible将抛出错误"输入文件未找到..."并停止供应过程.

我想知道如果找不到文件,是否可以允许配置过程继续.

我的用例如下:

  • 尝试加载变量文件
  • 如果存在这些变量,则执行任务

例:

- include_vars: aptcacher.yml

- name: use apt-cache
  template: src=01_proxy.j2 dest=/etc/apt/apt.conf.d/01_proxy owner=root group=root mode=644
  sudo: true
  when: aptcacher_host is defined
Run Code Online (Sandbox Code Playgroud)

Ansible版本:1.9.1

Pro*_*e85 5

你可以只ignore_errorsinclude_vars任务上:

- include_vars: nonexistant_file
  ignore_errors: yes
Run Code Online (Sandbox Code Playgroud)

编辑

随着 ansible > 1.6.5 我得到

测试文件

---

- hosts: localhost
  tasks:
    - include_vars: nonexistent_file
      ignore_errors: yes

    - debug:
        msg="The show goes on"
Run Code Online (Sandbox Code Playgroud)

播放 [本地主机]


收集事实 **************************************************** *************** 好的:[本地主机]

任务:[include_vars nonexistent_file] ********************************************* 失败:[ localhost] => {"failed": true, "file": "/home/ilya/spielwiese/ansible/nonexistent_file"} 味精:未找到源文件。...忽略

任务:[debug msg="演出继续"] ***************************************** ***** ok: [localhost] => { "msg": "节目继续" }


udo*_*dan 1

您可以使用with_first_found它来存档。

- include_vars: "{{ item }}"
  with_first_found:
   - this_file_doesnt_exist.yml
Run Code Online (Sandbox Code Playgroud)

如果至少有一个文件不匹配,我不能 100% 确定它会毫无抱怨地工作。如果它不起作用,您将需要添加一个空的后备文件:

- include_vars: "{{ item }}"
  with_first_found:
   - this_file_doesnt_exist.yml
   - empty_falback.yml
Run Code Online (Sandbox Code Playgroud)