118

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

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

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

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

[all:children]
master
slave

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?

3

7 Answers 7

178

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.

6
  • 6
    Above Ansible 2.0, it should be: [all:vars] ansible_connection=ssh ansible_port=22 ansible_user=admin
    – zx1986
    Apr 6, 2016 at 2:52
  • 2
    @zx1986 where did you read that?
    – 030
    Sep 14, 2016 at 7:27
  • 4
    @030 here: docs.ansible.com/ansible/…
    – zx1986
    Sep 14, 2016 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. Oct 21, 2016 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). May 6, 2017 at 8:15
29

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.

6
  • 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, 2014 at 13:20
  • Must the file be named group_vars/all/main.yml or simply group_vars/all?
    – realtebo
    Aug 11, 2018 at 14:46
  • 1
    @realtebo The file must be named group_vars/all.
    – xloto
    Mar 22, 2019 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, 2019 at 1:37
  • 1
    @realtebo both work. You can use former to separate variables that needs encryption from other variables
    – ivan_onys
    Dec 25, 2019 at 7:54
10

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
2
  • 1
    The link mentions ansible_ssh_pass, not ansible_pass. Oct 22, 2018 at 18:30
  • 1
    According the documentation it should be ansible_password. Tested it and that seems to work.
    – 030
    Apr 26, 2020 at 21:59
8

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.

2
  • 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, 2017 at 13:01
  • 2
    @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, 2017 at 13:20
2

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'
1

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

1

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

ansible.cfs configuration will be

$ cat ansible.cfg
[defaults]
inventory = ./inventory

[privilege_escalation]
become = True
become_method = sudo
become_user = root

Inventory file will be

$ cat inventory
[App1]
10.163.128.21

[App1:vars]
ansible_password=*************
ansible_ssh_user=Appuser1
host_key_checking=False

ansible execution success result

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .