Skip to content

Instantly share code, notes, and snippets.

@tonyskapunk
Created August 24, 2017 21:38
Show Gist options
  • Save tonyskapunk/5cfafc428350b78fe02e1b9cbb0316b0 to your computer and use it in GitHub Desktop.
Save tonyskapunk/5cfafc428350b78fe02e1b9cbb0316b0 to your computer and use it in GitHub Desktop.
includes in ansible
---
- debug:
msg: "magic"
...
---
- debug:
msg: "more magic"
...

without "do_magic"

$ ansible-playbook test.yml -vv
Using /path/to/.ansible.cfg as config file
statically included: /path/to/more_magic.yml

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [localhost] ***************************************************************
META: ran handlers

TASK [debug] *******************************************************************
task path: /path/to/more_magic.yml:2
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_items include all magic] ********************************************
task path: /path/to/test.yml:23
skipping: [localhost] => (item=magic.yml)  => {"changed": false, "item": "magic.yml", "skip_reason": "Conditional result was False", "skipped": true}
skipping: [localhost] => (item=more_magic.yml)  => {"changed": false, "item": "more_magic.yml", "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_first_found include magic] ******************************************
task path: /path/to/test.yml:33
skipping: [localhost] => (item=/path/to/magic.yml)  => {"changed": false, "item": "/path/to/magic.yml", "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_first_found include magic] ******************************************
task path: /path/to/test.yml:43

TASK [some magic include] ******************************************************
task path: /path/to/test.yml:54
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0  

with "do_magic"

$ ansible-playbook test.yml -e "do_magic" -vv
Using /path/to/.ansible.cfg as config file
statically included: /path/to/more_magic.yml

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [localhost] ***************************************************************
META: ran handlers

TASK [debug] *******************************************************************
task path: /path/to/more_magic.yml:2
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_items include all magic] ********************************************
task path: /path/to/test.yml:23
skipping: [localhost] => (item=magic.yml)  => {"changed": false, "item": "magic.yml", "skip_reason": "Conditional result was False", "skipped": true}
skipping: [localhost] => (item=more_magic.yml)  => {"changed": false, "item": "more_magic.yml", "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_first_found include magic] ******************************************
task path: /path/to/test.yml:33
skipping: [localhost] => (item=/path/to/magic.yml)  => {"changed": false, "item": "/path/to/magic.yml", "skip_reason": "Conditional result was False", "skipped": true}

TASK [with_first_found include magic] ******************************************
task path: /path/to/test.yml:43

TASK [some magic include] ******************************************************
task path: /path/to/test.yml:54
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0
---
## Ansible 2.3.2.0
- hosts: localhost
gather_facts: "False"
tasks:
## Common include
# This will evaluate(?) the tasks in more_magic.yml
# even if "do_magic" is not defined
# This is because by default "static" is "yes"
# and all children inherit the conditional "when" clause
- name: some magic include
include: more_magic.yml
when: do_magic is defined
## Include using "with_" clauses
# "with_items" will behave as setting static: no.
# This task will include all the files in "with_items"
# Evaluating all the child tasks in the files, only when "do_magic" is defined
- name: with_items include all magic
include: "{{ item }}"
with_items:
- magic.yml
- more_magic.yml
when: do_magic is defined
# "with_first_found" will behave as setting static: no.
# This taks will include the first file found from the list.
# Evaluating the children only when the do_magic is defined
- name: with_first_found include magic
include: "{{ item }}"
with_first_found:
- magic.yml
- more_magic.yml
when: do_magic is defined
# If a file is not found this can be used, see the difference in formatting
# compared to the previous one, also note the "skip" attribute which avoids failure
- name: with_first_found include magic
include: "{{ item }}"
with_first_found:
- skip: "true"
files:
- no_magic.yml
when: do_magic is defined
# This one will not evaluate the child tasks when "do_magic" is not defined
# as we are explicily setting static: "no"
- name: some magic include
include: more_magic.yml
static: no
when: do_magic is defined
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment