Ansible - 2

 Ad-hoc commands, Modules and playback.

-----------------------------------------

Go to AWS account --> create 3 EC2 instances in same AZ.

Take access of all machines via putty

Now go inside ansible server and download ansible package -> ec2-user -> sudo su


-> wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

now do "ls"

#yum install epel-release-latest.7.noarchrpm -y

# yum update -y

Now we have to install all the packages one by one

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


Now go to hosts file inside ansible server and paste private-ip of node1 and node2


#vi /etc/ansible/hosts


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


# vi /etc/ansible/ansible.cfg

uncommented

inventory = /etc/ansible/hosts

sudo-user = root


Now create one user in all the three instances.

# adduser ansible

Now set password for this user

# passwd ansible

Now switch as ansible user

# su - ansible

this ansible user don't have sudo priviledges right now if you want to give sudo priviledge to ansible user.


{root@ip}# visudo

now go inside this file,

root ALL=(ALL) ALL

ansible ALL=(ALL)       NOPASSWD

:wq!


Now do this thing in other nodes also 

Now go to ansible server and try to install httpd package as a ansible user.

{ansible@ip} sudo yum install httpd -y


Now establish connection between server and node Go to ansible server.

[ansible@ip] $ ssh 172.31.41.24

o/p -> permission denied

Now we have to do some changes in sshd_config file go to ansible server


{root@ip} # vi /etc/ssh/sshd_config

Do some changes @ saved the file 

Do this work in node1 and node1 also


Now verify in ansible server

[root@ip] su - ansible

{ansible@ip} ssh 172.31.41.240


Now it ask for password, enter the password, after that you will be inside node1


-----------------------------------------------------------------------------------

Now go to anisble server and create keys 

Run this command as ansible user

[ansible@ip] $ ssh-keygen

$ ls -a

o/p -> .ssh

[ansible@ip] $ cd .ssh/

[ansible@ip] $ ls

o/p -> id_rsa id_rsa_pub

Now i need to copy public key in both the nodes.

[ansible@ip .ssh]$ssh-copy-id ansible@172.31.41.240

ask for password

[ansible@ip .ssh]$ssh-copy-id ansible@172.31.41.228

ask for password

Now verify, go to ansible server

[ansible@ssh] $ cd ..

[ansible@ssh] $ ssh 172.31.41.240

Now, you will enter into node1 without password


=======================================================

Host patterns

-------------


-> "all" pattern refers to all the machines in an inventory

-> ansible all --list-hosts

-> ansible <groupname> --list-hosts

-> ansible <groupname>[0] --list-hosts

like ansible demo[0] --list-hosts

groupname[0] -> picks first machine of group

groupname[1] -> picks second machine of group

groupname[-1] -> picks last machine of group

groupname[0:1] -> picks first two machines in the group

groupname[2:5] -> picks 3, 4&5, &6 machine in the group


Group seperated by a colon can be used to use hosts from multiple groups.

groupname1:groupname2

like ansible demo[1:5]:devops[2:5] --list-hosts

Comments

Popular posts from this blog

GIT - 3

Docker - 6

GIT - 1