patternMinor
Discovering extra args needed for running ansible playbooks
Viewed 0 times
neededargsdiscoveringrunningforplaybooksansibleextra
Problem
I've got a number of ansible playbooks that I come back to from time to time, some have required and optional arguments that I forget about from time to time.
I know there's a list-tags, list-hosts and list-tasks option baked into the ansible-playbook command, there's no list-args, which would be super useful, but maybe it means I'm using ansible wrong?
So what strategies do devops engineers use to manage "api's" for ansible playbooks? Maybe just a recursive grep command to list non-standard args that aren't registered variables like this
only better.
For instance, I've got a program I want to put on a device called "showstopper", but I specify the version with extra_args and if I don't specify the version, it doesn't get installed.
This works good when I run it the way I intended to run it, but in 2 months when I want to run it on another machine, I forgot to specify showstopper_version and the last few machines I've built went out without that configured on it.
I know there's a list-tags, list-hosts and list-tasks option baked into the ansible-playbook command, there's no list-args, which would be super useful, but maybe it means I'm using ansible wrong?
So what strategies do devops engineers use to manage "api's" for ansible playbooks? Maybe just a recursive grep command to list non-standard args that aren't registered variables like this
grep -or "{{[a-zA-Z_ ]\+}}" roles/. | grep -v "ansible" | grep -v "item"only better.
For instance, I've got a program I want to put on a device called "showstopper", but I specify the version with extra_args and if I don't specify the version, it doesn't get installed.
- name: download showstopper from artifactory
local_action: get_url url=https://example.net/artifactory/generic-release-local/com/cdw/mans/silo/showstopper/showstopper-{{ showstopper_version }}.noarch.rpm dest=/tmp validate_certs=no
run_once: true
when: showstopper_version is defined
- name: copy showstopper to silo
copy: src=/tmp/showstopper-{{ showstopper_version }}.noarch.rpm dest=/tmp
when: showstopper_version is defined
- name: Install showstopper
yum: name=/tmp/showstopper-{{ showstopper_version }}.noarch.rpm state=present
notify:
- start cron
when: showstopper_version is definedThis works good when I run it the way I intended to run it, but in 2 months when I want to run it on another machine, I forgot to specify showstopper_version and the last few machines I've built went out without that configured on it.
Solution
If an extra variable is truly required, you can add
assert statements at the top of the role to stop it from executing if they're not defined:┌─[jamesph@geror] - [~/temp] - [Sat Jan 13, 09:40]
└─[$]> cat assert.yml
- hosts: localhost
vars:
foo: False
tasks:
- assert:
that:
- "foo is defined"
- "bar is defined"
┌─[jamesph@geror] - [~/temp] - [Sat Jan 13, 09:40]
└─[$]> ansible-playbook assert.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] *************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [localhost]
TASK [assert] ****************************************************************************************
fatal: [localhost]: FAILED! => {
"assertion": "bar is defined",
"changed": false,
"evaluated_to": false
}
to retry, use: --limit @/Users/jamesph/temp/assert.retry
PLAY RECAP *******************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1Code Snippets
┌─[jamesph@geror] - [~/temp] - [Sat Jan 13, 09:40]
└─[$]> cat assert.yml
- hosts: localhost
vars:
foo: False
tasks:
- assert:
that:
- "foo is defined"
- "bar is defined"
┌─[jamesph@geror] - [~/temp] - [Sat Jan 13, 09:40]
└─[$]> ansible-playbook assert.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] *************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [localhost]
TASK [assert] ****************************************************************************************
fatal: [localhost]: FAILED! => {
"assertion": "bar is defined",
"changed": false,
"evaluated_to": false
}
to retry, use: --limit @/Users/jamesph/temp/assert.retry
PLAY RECAP *******************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1Context
StackExchange DevOps Q#2969, answer score: 4
Revisions (0)
No revisions yet.