Answers: <p>That's what the configuration file is for. Create an <code>ansible.cfg</code> in the directory from where you run <code>ansible</code>, like so:</p>
  
  <pre>
  [defaults]
  inventory = hosts
  ask_vault_pass = True
  
  [privilege_escalation]
  become_ask_pass = True
  </pre>
  
  <p>See also <a href="http://docs.ansible.com/ansible/latest/reference_appendices/config.html" rel="nofollow noreferrer">http://docs.ansible.com/ansible/latest/reference_appendices/config.html</a></p>
  
  <p>The <code>ansible.cfg</code> in the working directory precedes <code>/etc/ansible/ansible.cfg</code>. The full search order is described on the page linked above. Command line options you pass on invocation override all configuration files.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-05-17T14:07:14.253Z
Answers: Thanks, that works great! Is there a way to specify `ask_vault_pass` in the playbook though? My folder contains multiple playbooks, some require the vault and others don't.
Answers: Yvo
Answers: CC BY-SA 4.0
Answers: 2018-05-17T14:53:07.577Z
Answers: According to http://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html, no. You can specify the `vault_password` as a secret, but I'm not sure that's an option for you. I haven't used vaults yet, so can't tell you more.
Answers: simonz
Answers: CC BY-SA 4.0
Answers: 2018-05-17T12:09:26.667Z
Answers: 2018-05-17T12:18:07.763Z
Answers: 2018-05-17T12:18:07.763Z
Answers: simonz
Body: <p>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.</p>
  
  <p>For example my playbook needs the following arguments:</p>
  
  <ul>
  <li>the hosts file: <code>-i hosts</code></li>
  <li>the password for become: <code>-K</code></li>
  <li>the password for the vault: <code>--ask-vault-pass</code></li>
  <li>the name of the playbook: <code>myplaybook.yml</code></li>
  </ul>
  
  <p>It would be easier if I could just run the following command (especially for co-workers):<br>
  <code>ansible-playbook myplaybook.yml</code></p>
  
  <p>And specify in the playbook, that by default it should look for a <code>hosts</code> 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.</p>
  
  <p>I guess I'm looking for adding something like this to my playbook:</p>
  
  <pre><code>vars:
    inventory_file: hosts
    ask_become_pass: true
    ask_vault_pass: true
  </code></pre>
  
  <p>Is that possible?</p>
  
Tags: <ansible>
Title: Specify default arguments for Ansible playbook
UserDisplayName: Yvo
ThreadText: # Specify default arguments for Ansible playbook
  
  (Asked by: username_0 on 2018-05-17)
  
  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?
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-05-17. Score: 3 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: Thanks, that works great! Is there a way to specify `ask_vault_pass` in the playbook though? My folder contains multiple playbooks, some require the vault and others don't.  
  > **username_1**: According to http://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html, no. You can specify the `vault_password` as a secret, but I'm not sure that's an option for you. I haven't used vaults yet, so can't tell you more.  
  
  ---
  
  