I am using Ansible and I have this configuration in my inventory/all:

[master] ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[slave] ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[app] ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant


I don't want to repeat all the parameters for each new instance. How can I configure them just in one place? Is there any file with these parameters?


You can add following section to your inventory file:


Note: Before Ansible 2.0 ansible_user was ansible_ssh_user.

  • 4
    Above Ansible 2.0, it should be: [all:vars] ansible_connection=ssh ansible_port=22 ansible_user=admin – zx1986 Apr 6 '16 at 2:52
  • 2
    @zx1986 where did you read that? – 030 Sep 14 '16 at 7:27
  • 3
    @030 here: docs.ansible.com/ansible/… – zx1986 Sep 14 '16 at 10:20
  • Note that port 22 is the default for ansible_port, so you don't need to explicitly specify it unless different. Interested if anyone knows how to apply variables to more than one group at a time (but not 'all'), comma separation doesn't seem to work. – William Turrell Oct 21 '16 at 15:17
  • 1
    @WilliamTurrell a little late but.. you can create a group of groups (with the :children modifier) and then set the variables afterwards (with the :vars modifier). – Lasse Halberg Haarbye May 6 '17 at 8:15

Group Variables

You can set variables that apply to all hosts by using the playbook layout specified in Ansible's Best Practices document and creating a group_vars/all file where you define them.

# file: group_vars/all
ansible_connection: ssh 
ansible_ssh_user: vagrant 
ansible_ssh_pass: vagrant

[edit] I'm confused though at what you're trying to do though. You shouldn't need to specify the Ansible user or password in the inventory. If you're using Vagrant you definitely don't, and if you're calling Ansible from the command-line you can specify the user with --user=vagrant and have it ask for the password with --ask-pass.

  • 1
    I only need to do a better organization of my infrastructure. If always I will use vagrant:vagrant I want to put it configuration in some place where Ansible load by default, but I don't know where. Every connection to be executed has to use it. – Robert Sep 17 '14 at 13:20
  • Must the file be named group_vars/all/main.yml or simply group_vars/all? – realtebo Aug 11 '18 at 14:46
  • 1
    @realtebo The file must be named group_vars/all. – xloto Mar 22 '19 at 15:02
  • 1
    -k for ask pass is not a lot of fun for "all" when each host has a different password. Yes you need sshpass, and yes you probably need to seed known_hosts if you have never connected before ~\.ssh\config is the way to go. – mckenzm May 16 '19 at 1:37
  • 1
    @realtebo both work. You can use former to separate variables that needs encryption from other variables – ivan_onys Dec 25 '19 at 7:54

I think that it is better to use ssh key installation onto all the servers across the pull. You should run ssh-copy-id only per node and install your ssh key everywhere for ansible's ability to log in using your ssh key. It will be more secure not to save passwords into playbook/inventory.

For doing this you should generate your ssh key pair and run ssh-copy-id for all the servers afterwards.

  • 3
    If someone can steal the password from your configuration file they'll likely be able to your ssh keys as well. And this has nothing at all to do with answering OPs question. This question wasnt asking for opinions on alternatives. – Muh Fugen Dec 17 '17 at 13:01
  • @MuhFugen > How can I configure them just in one place? Is there any file with these parameters? This is an exact phrase which leaves the door for the opinion as well as response gives an understanding of right direction. Stealing configuration (that could live in the git repo somewhere) is not the same as stealing private key. – podarok Dec 18 '17 at 13:20

Add below to inventory hosts.

For Ansible <2.0:


For Ansible >=2.0:

ansible_connection=ssh # actually default mode smart is OK
ansible_pass=vagrant # or ansible_ssh_pass=vagrant
  • 1
    The link mentions ansible_ssh_pass, not ansible_pass. – Matthias Weiler Oct 22 '18 at 18:30
  • 1
    According the documentation it should be ansible_password. Tested it and that seems to work. – 030 Apr 26 '20 at 21:59

Disclaimer: I've only tested this on OSX. Based on the various docs, I expect it to work on other platforms.

"project directory" refers to the base directory for the Vagrant project -- the directory that contains Vagrantfile.

Ansible Inventory file auto-generated by Vagrant:

Vagrant creates an inventory file with the default Ansible connection vars. Look for it in <project directory>/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.

This file will be regenerated by Vagrant as-needed, so manual edits will get overwritten. However, according to the Vagrant docs, you can specify multiple machines, group vars, etc in Vagrantfile and they'll be added to this inventory file.

Configure Ansible to default to this inventory file:

To make this file the default used by the ansible command when you're in the project directory (on the host), add an ansible.cfg file in your project directory with these contents, changing the path as needed:

inventory = ./path/to/inventory

To confirm that this inventory file is being used, look for it as the default reported by ansible:

(from within the project directory)

$ ansible | grep inventory ERROR! Missing target hosts -i INVENTORY, --inventory-file=INVENTORY specify inventory host path (default=./.vagrant/provis ioners/ansible/inventory/vagrant_ansible_inventory) or

To confirm your hosts:

$ ansible all --list-hosts hosts (2): master slave

Using Ansible with these hosts:

From within the project directory, you should then be able to use ansible as normal with the hosts that you defined in Vagrantfile.

For example:

ansible slave -a 'hostname'

You have to set the right variables. You need to use:

# yml syntax
ansible_user: myusername
ansible_password: mypassword

# inventory syntax
host ansible_user=myusername ansible_password=mypassword

You need to read very carefully, because ansible_ssh_password and ansible_ssh_user do not work in version: ansible 2.9.6


Below is the steps for how to Set default Ansible username/password for SSH connection

ansible.cfs configuration will be

$ cat ansible.cfg
inventory = ./inventory

become = True
become_method = sudo
become_user = root

Inventory file will be

$ cat inventory


ansible execution success result

$ ansible App1 -m ping | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    "changed": false,
    "ping": "pong"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.