Ansible Inventory Host Pattern

Ansible Inventory Host Pattern





How to establish SSH connection between server to hosts

Go to AWS account => Create 3 EC2 instances in same AZ => Take access of all machines via putty.


 
Now go inside the ansible server and download ansible packages

=> wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Now do “ls” to check file download and run command to install

#yum install epel-release-latest-6.8.noarch.rpm
#yum update –y

Now we have to install the package one by one of ansible and dependencies 

#yum install git python python-level python-pip openssl ansible -y

Now go to hosts file mode ansible copy and paste private ip of host1 and host2

#vi /etc/ansible/hosts
[demo] (add any group name under group and ip address)
192.168.0.1
192.168.0.2

Now the host file is only working after updating ansible.cfg file

#vi /etc/ansible/ansible.cfg

Uncomment these things.

#inventory = /etc/ansible/hosts
#sudo-user = root

This step need when you don’t want to give root access to any one so you need to create an ansible user

Create one ansible users in all three instances:
#adduser ansible

Set password on the user:
#passwd ansible

Now switch as ansible user:
#su – ansible

Ansible user does not have sudo permission right now, if you want to give sudo permission to ansible user:

#visudo

Now go inside this file and type these line below root: (ansible means user) 

root ALL=(ALL) ALL
ansible ALL=(ALL) NOPASSWD:ALL


Now do this thing in other nodes also and go to ansible server and try to install httpd package as a ansible user:

#sudo yum install httpd –y

Establish connection between server and node go to ansible server:
#ssh <private ip address> error: permission denied 

We have to do some changes in sshd-config file go to ansible server
#vi /etc/ssh/sshd-config
#service sshd restart

Uncomment below lines
PermitRootLogin yes
&
PasswordAuthentication yes
& comment below next first line  
#PasswordAuthentication no

now you can access but it will ask for password every time when you connect host1 and host2 so will have to do next steps.

Generate RSA key and copy public key on both hosts: 

Now go to ansible server and create keys run the command as ansible user:

#ssh-keygen
#ls –a (see hidden file .ssh)
#cd .ssh/
#ls  (see public and private key as id_rsa & id_rsa_pub)

We need to copy the public key in both hosts

#ssh-copy-id ansible@192.168.0.1  (ansible is user and private ip address) 


#ssh-copy-id ansible@192.168.0.2  (ansible is user and private ip address)

Now verify so go to ansible user and check that you can take ssh access with any password and you can install any package with password

Host patterns



When you execute Ansible through an ad hoc command or by running a playbook, you must choose which managed nodes or groups you want to execute against. Patterns let you run commands and playbooks against specific hosts and/or groups in your inventory. An Ansible pattern can refer to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. Patterns are highly flexible - you can exclude or require subsets of hosts, use wildcards or regular expressions,
 and more. Ansible executes on all inventory hosts included in the pattern.

“all” pattern refer to all the machines in an inventory 

ansible all –list-hosts
ansible <group-name> --list-hosts
ansible <group-name>[0] --list-hosts

groupname[0] => picks first machine of group
groupname[1] => picks second machine of group
groupname[-1] => picks last name of group
groupname[0:1] => picks first two machine in the group
groupname[3:5] => picks 4,5 & 6 machine in the group

Group separated by colon can be used to use hosts from multiple groups
groupname1:groupname2     =>  i.e demo[1]:dev[1:4]





Share:

0 comments

Please leave your comments...... Thanks