How to set default Ansible username/password for SSH connection?

Solution 1:

You can add following section to your inventory file:

[all:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_ssh_pass=vagrant

Note: Before Ansible 2.0 ansible_user was ansible_ssh_user.

Solution 2:

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.


Solution 3:

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.


Solution 4:

Add below to inventory hosts.

For Ansible <2.0:

[all:vars]
ansible_connection=ssh
ansible_ssh_user=vagrant 
ansible_ssh_pass=vagrant

For Ansible >=2.0:

[all:vars]
ansible_connection=ssh # actually default mode smart is OK
ansible_user=vagrant
ansible_pass=vagrant # or ansible_ssh_pass=vagrant

Solution 5:

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:

[defaults]
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'

Tags:

Ansible