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?
You can add following section to your inventory file:
Note: Before Ansible 2.0
ansible_user
wasansible_ssh_user
.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.[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
.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.
Add below to inventory hosts.
For Ansible <2.0:
For Ansible >=2.0:
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 anansible.cfg
file in your project directory with these contents, changing the path as needed: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 inVagrantfile
.For example:
You have to set the right variables. You need to use:
You need to read very carefully, because
ansible_ssh_password
andansible_ssh_user
do not work in version: ansible 2.9.6Below is the steps for how to Set default Ansible username/password for SSH connection
ansible.cfs configuration will be
Inventory file will be
ansible execution success result