Ansible Basics and Ad-hoc Commands

Ansible Basics and Ad-hoc Commands

Ansible Basics and Ad-hoc Commands

·

5 min read

Ansible Basics and Ad-hoc commands.

What is Ansible?

Ansible is a simple configuration management and IT automation engine for multi-tier deployments. It automates both cloud and on-premise provisioning & configuration. It automates cloud provisioning. Rather than managing one system at a time, Ansible uses a model that inter-relates the entire IT infrastructure and enables you to manage everything using something called an Infrastructure as Code (IAC) approach. Ansible is secure and agentless. It relies on OpenSSH and the code written in YAML format. Ansible nodes are run on Unix systems but they can be used to configure changes across Unix as well as Windows systems

Ansible Use Cases:

  • Provisioning: Provisioning is creating new infrastructure. Ansible allows for application management, deployment, orchestration, and configuration management.

  • Continuous Delivery: Ansible provides a simpler way to automatically deploy applications. All required services for a deployment can be configured from a single system. Continuous Integration (CI) tool can be used to run Ansible playbook which can be used to test and automatically deploy the application to production if tests are passed.

  • Application Deployment: Ansible provides a simpler way to deploy applications across the infrastructure. Deployment of multi-tier applications can be simplified and the infrastructure can be easily changed over time.

  • Ansible for Cloud Computing: Ansible makes it easy to provision instances across all cloud providers. Ansible contains multiple modules and allows to manage of large cloud infrastructure across the public-private and hybrid cloud.

  • Ansible for Security and Compliance: You can define security policies in Ansible which will automate security policy across all machines in the network. Security roles once configured in an Ansible node will be embedded across all machines in the network automatically.

Ansible Vs Puppet (Both are Configuration Management)

Ansible : 

Using Push Mechanism

Its using the Agentless approach

It's also integrated with multiple clouds. Only the IP Should be publicly accessible and SSH password less should be enabled that should be allowed from Ansible Machine.

Puppet

Using Pull Mechanism.

Using Master /Slave Architecture which is much more complex.

Let's start with Practical:

We can start from the Ansible Master server, we have update the package and install ansible.

https://docs.ansible.com/ansible/latest/index.html check ansible documentation

With ansible –version command we can check whether ansible has been installed or not?

So we have launched two instances one is Ansible Master and Target Server ; through Master server we will going to configure the target server.

So we will going to initiate the password less authentication. Prerequisite for Ansible able to communicate with the target server without any password.

Here we will try to access the target server from the Ansible server with ssh <Ip address> but it will not work unless and until we can do password less authentication. From Ansible server we will execute the command “ ssh-keygen” to create the key.

Follow the below steps:

ID_rsa is a Private key used to login to this local machine and it's very sensitive. We should not share the key with anyone; this is the best practice.

Id_rsa.pub key to communicate with both the server , so we can try to communicate with target server from the Ansible server with public key.

Then go the target server and execute the command “ssh-keygen” and in vim_authorized file key we have to copy the public key of the Ansible server.

So we can able to connect without the password from Ansible server to target server.

Ansible Ad-hoc commands:

Inventory files we can store anywhere it's not mandatory to store in specific locations. By default ansible store the inventory file is in /etc/ansible/hosts

What is an inventory file?

Inventory file is nothing but we have to store our target server IP address within the Inventory file. You can mention as many as server IP addresses in that inventory file.

So will write one inventory file as below: we have mentioned target server IP address in the file.

With ansible -i inventory <ip address of the target server> we are having only one target server as of now for the practice purpose if we have 100 target servers then we can group them in the inventory file and give the name of the group or just write <all> its fetching all the servers in the inventory file.

With above command we have created the file “ devopslearner” with one command.

The syntax above command -a as an argument & -m for the module which we get each and every module from the ansible official site already shared screenshot above.

We can check on the target server & the file has been created. So Boom its very easy to implement on the multiple servers with single command.

In the below example we can check the disk usage of the target server in one single line command so in this way we can output disk usage of the multiple servers.

What if there are two different server groups? Or if we want to exclude a few of the servers ? or the use case here is to run a certain number of playbooks on the DB server & web server.

Then open the inventory file and we can do grouping of our’s server :

Post this if we want to execute ansible command only on the web server then we can execute the below command. If there is one web server it would be executing on only one server and if we put 100 servers in the file then it would execute on the 100 webservers.

Happy Learning !!