Ansible Basics Tutorial-Ansible Basics Commands

Ansible Basics Tutorial-Ansible Basics Commands

Ansible is a automation tool which is used for configuration management. Using ansible we can configure web servers, databases, user configuration, etc. With ansible we can configure Linux and windows machines, we can provision servers in cloud. Using ansible we can deploy the code in multiple servers or remote servers. Using ansible we can run scripts in remote machines. In this recipe we are going to discuss about ansible basics tutorial and ansible basic commands.

Ansible Basic Example

lets assume that you want to install Apache2(httpd) in 100 servers. if you do this manually like login to all servers and running the commands manually. Will take lot of time and we need to put lot of effort. At this situation we can use ansible. Using ansible with in few seconds we can run all the commands in remote servers. Ansible connect to the remote servers using SSH and it will perform the tasks. For this we can use ansible in two ways. one is using ansible hd-hoc commands or other one is writing ansible playbook. 

ansible basics

Ansible Basic Ad-Hoc Commands

Using ansible ad-hoc commands we can install apache2 in all the target servers.

Ex:

ansible all -m apt -a “name=apache2 state=present”

with this single command we can install apache2 in all the 100 servers. Here all represents all the 100 servers and m represent module and a represents arguments. To install any package in ubuntu or debian systems we use ansible apt module. Using arguments we can tell to ansible that which package you want to install. To install any package we will mention state as present and to remove the package we will mention state as absent.

Ansible Basic Playbook

Same above command we can put in one file and we can run that file. But with above command we can run only one command. let’s assume that we want to install apche2(httpd) in all target 100 servers and start apchae2. In this situation we cannot use ansible ad-hoc commands. At this situation we can use ansible playbook. For this we have to create one yml file, that file will be called as ansible-playbook in that file we will mention all the list of commands, which you want to execute on the target servers.

abcd.yml

---
- hosts: all
  tasks:
  - name: install apchae2 in all 100 servers
    apt:
     name: apache2
     state: present
  - name: start apche2 in all 100 servers
    service:
     name: apache2
     state: started

Here you can see two tasks one is installing apache2 in all target servers and starting apache2 in all the servers.

Run Ansible Playbooks

By using ansible-playbook command we can execute ansible playbooks.

ansible-playbook   abcd.yml

Hit enter, this will execute all the commands which you written in that file will be executed in alll 100 target servers.

Leave a Reply

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