Sometimes (not often) we need to do some one-off maintenance (like manually deleting some files, force removing a package) before a single deploy.
Using ansible seems like it would be a great tool to do this.
for example:
role/app/defaults/main.yml
deploy_patch: !!null
role/app/tasks/deploy.yml
- include {{ deploy_patch }}
when: deploy_patch
… (rest of deploy tasks go here)
When I need to patch something I would run ansible like
ansible-playbook ... -e "deploy_path=/path/to/20131003-patch.yml" --tags deploy
Where on that run the tasks in the patch file would be included but not on normal deploy runs.
It seems like the include syntax above is invalid, can anyone here think of a different way to go about the desired result or whether it would make sense to allow variable includes like this?
-John
Looks like this is the problem:
- include {{ deploy_patch }}
Should be
- include: “{{ deploy_patch }}”
Note that this variable will need to be defined every time, as because the when statement will be applied to each task within the include.
Yeah I think defaulting deploy_patch to !!null will do the trick so that when
it’s not passed in as an extra arg all of the included tasks will be
skipped.
Even with the quotes it doesn’t seem to work with ansible 1.3.1
test/tasks/main.yml
Where is foo getting defined?
Not seeing it on -e and wanted to be sure.
Thanks!
I’m setting it in test/defaults/main.yml
This seems to work fine:
$ ansible-playbook -vvv -c local --limit “localhost,” ./test.yml -i “localhost,” -e “foo=monkey-patch.yml”
But when i set a default value and don’t pass in a -e option it fails.
Here is a complete test role with output:
test.yml
Roles and includes basically are the same thing, so that’s probably why there are not available to define a role path.
Interestingly for:
- include: “{{ foo }}”
when: foo
if I set “foo: !!null” in defaults/main.yml I get:
ERROR: file not found: /home/jarv/src/configuration/playbooks/roles/test/tasks/{{foo}}
If I set it in vars/main.yml I get:
ERROR: file not found: /home/jarv/src/configuration/playbooks/roles/test/tasks/None
Do you think this should get a bug report or is it too much of an edge case?
"when" does not apply to the include directive, this always happens, it
applies to all tasks included.
Yup!
You might want to use “group_by” conditionally, to create dynamic groups of places where this is true.