Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible include_vars continue if file not found

If I have something like this:

- include_vars: this_file_doesnt_exist.yml

ansible will throw the error "input file not found at ..." and stop the provisioning process.

I'm wondering if it's possible to allow the provisioning process to continue if the file isn't found.

My use case is the following:

  • try to load variables file
  • execute tasks if those variables exist

Example:

- 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

Ansible version: 1.9.1

like image 719
joao cenoura Avatar asked Jun 05 '15 20:06

joao cenoura


2 Answers

You can just ignore_errors on the include_vars task:

- include_vars: nonexistant_file
  ignore_errors: yes

EDIT

With ansible > 1.6.5 I am getting

test.yml

---

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

    - debug:
        msg="The show goes on"

PLAY [localhost]


GATHERING FACTS *************************************************************** ok: [localhost]

TASK: [include_vars nonexistent_file] ***************************************** failed: [localhost] => {"failed": true, "file": "/home/ilya/spielwiese/ansible/nonexistent_file"} msg: Source file not found. ...ignoring

TASK: [debug msg="The show goes on"] ****************************************** ok: [localhost] => { "msg": "The show goes on" }

like image 122
ProfHase85 Avatar answered Nov 04 '22 02:11

ProfHase85


You can use with_first_found to archive this.

- include_vars: "{{ item }}"
  with_first_found:
   - this_file_doesnt_exist.yml

I'm not 100% sure it will work without complain if not at least one file matched. In case it doesn't work, you will need to add an empty fallback file:

- include_vars: "{{ item }}"
  with_first_found:
   - this_file_doesnt_exist.yml
   - empty_falback.yml
like image 43
udondan Avatar answered Nov 04 '22 02:11

udondan