Ansible configuration in Linux Step By Step

Ansible configuration in Linux Step By Step

Ansible is a configuration management tool. To configure ansible in linux you must have installed ansible in your machine. In the following steps i will show you how to configure ansible in linux ubuntu centos rhel amazon aws ec2 machines. To configure ansible in Linux we need two machines one is ansible master machine and another one is client machine. in the following examples i will use 10.0.0.27 as ansible master control machine and 10.0.0.25 as client machine.

To install ansible in your master machine follow below articles.

Ansible configuration In Client Machine

Create User:

Create one user in client machine and assign password to that user. Here in my client machine i created the devops user.

[root@ip-10-0-0-25 ~]# adduser devops
[root@ip-10-0-0-25 ~]# passwd devops
Changing password for user devops.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@ip-10-0-0-25 ~]#

Make This User As Sudo User:

To install any packages in client machine devops user must have sudo permissions. So assign sudo permissions with password less. To assign sudo permssions to any user we have to add that user in /etc/sudoers  file

vi /etc/sudoers 

add below line

devops ALL=(ALL) NOPASSWD: ALL

Allow SSH From Master:

when you are executing any playbooks an client machine from master control machine. First ansible master machine will do ssh to client machine and then it will execute the tasks that you defined.

so allow port no 22 of client machine from master Ip address

in client security groups add this inbound rule

port     source
22       10.0.0.27(ip of master)

Allow user to connect with username/password

ansible will connect as devops user to client machine. so ansible master machine will do ssh to client like ssh devops@10.0.0.25.  So for that we have to allow devops user to connect from master machine with username and password.

vi /etc/ssh/sshd_config 
add below line
PasswordAuthentication yes

restart sshd service
service sshd restart

Ansible configuration In master Control Machine:

in ansible master machine we have to generate ssh keys and share with client machine as devops user for that run below commands

ssh-keygen
ssh-copy-id devops@10.0.0.25
ssh-copy-id devops@ip-of-client

it will promt for devops password enter password

after sharing ssh keys with client machine verify is your ssh working as expected or not by using below command.

ssh devops@10.0.0.25

It will connect to your client machine. If it is connected then the configuration is successful and now you can run ansible playbooks.

Run Ansible Ad-Hoc Commands:

ansible -m ping all -i '10.0.0.25,' -u devops --become
ansible -m yum -a "name=httpd state=present" all -i '10.0.0.25,' -e "ansible_user=devops" --become
ansible -m yum -a "name=httpd state=present" all -i '10.0.0.25,' -u devops --become
ansible -m yum -a "name=httpd state=present" all -i '10.0.0.25,' --user devops --become

You can use anyone of the above commands to run ansible Ad-hoc commands.

Here -u or --user means ansible will connect to client machine as devops user.
Ansible master will do ssh to client machine like ssh devops@10.0.0.25 and it will execute playbooks as devops user.

Run Ansible Playbooks:

[root@ip-10-0-0-27 ~]$ cat abc.yml
---
- hosts: all
  gather_facts: no
  remote_user: devops
  become: yes

  tasks:
  - name: install httpd
    yum: name=httpd state=present

  - name: install docker
    yum: name=docker state=latest

To install any packages in linux we need root user or a user with sudo permissions. Here devops user is sudo user and the task will be executed as devops sudo user. Thats why we mentioned become yes.

 

Leave a Reply

Your email address will not be published. Required fields are marked *