9

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

joao cenoura
  • 1,155
  • 2
  • 14
  • 20

3 Answers3

5

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" }

ProfHase85
  • 11,763
  • 7
  • 48
  • 66
  • 1
    I've tried with "ignore_errors" but doesn't seem to work. Ansible will throw the same error "input file not found at ...". Thanks – joao cenoura Jun 06 '15 at 12:26
  • You are using `include_vars` as task? Have you tried the playbook as i posted above? – ProfHase85 Jun 06 '15 at 15:51
  • Just tried your playbook (`ansible-playbook -i localhost, test.yml`) and got the same error "input file not found at ...". Are you using a different command to run your playbook? I'm using ansible 1.9.1. – joao cenoura Jun 06 '15 at 18:26
  • Oh, this is totally not the way ansible is called (just wondering that anything else works with you) . You need to specify an inventory file with -i (and there should be **no** comma. So the call goes like that `ansible-playbook test.yml` or `ansible-playbook -i /path/to/inventory test.yml` – ProfHase85 Jun 06 '15 at 19:30
  • and your inventory file should contain something like `localhost ansible_connection=local` (see documentation) – ProfHase85 Jun 06 '15 at 19:43
  • There's a reason for invoking ansible that way: check [this](http://stackoverflow.com/questions/17188147/how-to-run-ansible-without-specifying-the-inventory-but-the-host-directly), [this](https://github.com/ansible/ansible/issues/9932) or search google "how to run ansible without inventory". Either way it doesn't work. Regarding "if anything else works with me": yes, except this part. My suspicions for now are the different versions. Thanks – joao cenoura Jun 06 '15 at 20:20
  • Got it tested with 1.9.1 . Please try this call `ansible-playbook -e ansible_connection=local -i localhost, test.yml` . I suspect that you maybe did not pass the ansible_local variable to the localhost connection. (If i don't, i get the same error). The call mentioned above works for me – ProfHase85 Jun 06 '15 at 23:05
1

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
udondan
  • 57,263
  • 20
  • 190
  • 175
  • 1
    The first case doesn't work, it needs at least one file matched (outputs "No source file given"). The second case indeed works, but it sounds more like a workaround rather a proper/clean solution. Thanks! – joao cenoura Jun 05 '15 at 20:35
0

To optionally include variables, you can use the with_first_found loop which is based on the ansible.builtin.first_found lookup. With skip: true, you can assert that it does not error, when it does not find any files:

- include_vars: '{{ item }}'
  with_first_found:
    - files:
        - this_file_doesnt_exist.yml
      skip: true

From Ansible 2.2 on, you can also use a regex pattern for files to include. The module will not error if it does not find any files. Caveat: You have to provide a directory name, that does exist:

- include_vars:
    dir: vars
    files_matching: this_file_doesnt_exist.yml
stackprotector
  • 10,498
  • 4
  • 35
  • 64