HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Specify default arguments for Ansible playbook

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
argumentsplaybookdefaultforspecifyansible

Problem

When I create playbooks I often add a comments section at the top on how to run the Ansible playbook. With more options becoming available in Ansible, the list of command-line arguments also seems to grow. It made me curious to see if there is a way to specify defaults for these arguments in the playbook.

For example my playbook needs the following arguments:

  • the hosts file: -i hosts



  • the password for become: -K



  • the password for the vault: --ask-vault-pass



  • the name of the playbook: myplaybook.yml



It would be easier if I could just run the following command (especially for co-workers):

ansible-playbook myplaybook.yml

And specify in the playbook, that by default it should look for a hosts inventory file and ask for sudo + vault passwords. Of course I could create a script and wrap the playbook command, but that adds another layer on top of it.

I guess I'm looking for adding something like this to my playbook:

vars:
  inventory_file: hosts
  ask_become_pass: true
  ask_vault_pass: true


Is that possible?

Solution

That's what the configuration file is for. Create an ansible.cfg in the directory from where you run ansible, like so:

[defaults]
inventory = hosts
ask_vault_pass = True

[privilege_escalation]
become_ask_pass = True

See also http://docs.ansible.com/ansible/latest/reference_appendices/config.html

The ansible.cfg in the working directory precedes /etc/ansible/ansible.cfg. The full search order is described on the page linked above. Command line options you pass on invocation override all configuration files.

Context

StackExchange DevOps Q#4119, answer score: 3

Revisions (0)

No revisions yet.