Working of adhoc commands and Modules in Ansible

black laptop computer turned on showing computer codes
Reading Time: 3 minutes

What is Ansible

Let see the working of adhoc commands and modules in ansible in this blog.Ansible is an open-source automation tool or platform used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. It is agent less means you don’t need to install any other software or firewall ports on the client systems you want to automate.It uses a very simple language (YAML) that allow you to describe your automation jobs in a way that approaches plain English.

Step to install ansible for linux machine :

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

Introduction to ad hoc command and modules

An Ansible ad hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. Ad hoc commands are runs individually to perform quick functions and it can’t be reusable .

When you have to reboot all your company servers ,power off all the machines and many more task to perform there we can use adhoc command. ad hoc commands are great for tasks you repeat rarely you could execute a quick one-liner in Ansible without writing a playbook. Idempotency is present in adhoc command.

These commands are not used for configuration management and deployment, because they are uses one time.

Syntax

$ ansible [pattern] -m [module] -a "[module options]" 

Example :

$ ansible webservers -a “ls”

$ ansible webservers -a "touch file1.txt"

$ ansible webservers[0] -a "touch file1.txt"
  • here webservers [0] means it will create file in the first node of webservers.
  • We can perform operation on particular node then we can pass argument in ” [ ] “.
  • Let there are 10 node in a group and you perform some task on starting 5 node then argument we can pass as ” [0-4]”.
  • webservers[-1] refer the last node of the group.

When we use ad-hoc commands we do not provide the module name, the “command” module will be used by default. This is fine for a one time task, but anything more complex will require the use of an Ansible Playbook

Introduction to modules

Modules are like small programs that Ansible pushes out from a control machine to all the nodes or remote hosts. It can be written in the from of command line or in a playbook task. When using command line then we pass command by “-m “. Ansible executes all the modules for installing updates or whatever the required task is, and then removes them when finished. Ansible executes each module, usually on the remote target node, and collects return value in JSON format. Idempotency is present in module.

Module written in the form of command line

ansible webservers -m service -a "name=httpd state=started"  
ansible webservers -m ping  

ansible web -b -m copy -a "src = "file path " dest= /tmp"

Each module supports taking arguments. Mainly all modules take key=value arguments, space delimited.

Some module takes no arguments, and the shell/command modules take the string of the command which you want to execute.

Example

ansible webservers -m command -a "/sbin/reboot -t now"

Module written in the form of YAML

- name: restart webserver  
  service:  
    name: httpd  
    state: restarted  

There are lots of module in ansible some of them are as :

1. Copy

The copy module allows you to copy a file from the Ansible control node to the target hosts. In addition to copying the file, it allows you to set ownership, permissions, to the destination file. Here’s an example of using the copy module to copy a “message of the day” configuration file to the target hosts:

---
- hosts: web 
  user: root
  become: yes
  connection: ssh
  tasks:
    - name: Copy file with owner and permissions
      copy:
        src: ./filesend2.txt
        dest: /Downloads
        owner: root
        group: root
        mode: '0644'

2. File

The file  module allows you to control the state of files and directories ,setting permissions and ownership .

---
- hosts: web 
  user: root
  become: yes
  connection: ssh
  tasks:
    - name: change file with owner and permissions
      file:
        path: /Downloads/filesend2.txt
        owner: root
        group: root
        mode: '0744'

3. Package

The package module allows you to install, update, or remove software packages from your target system using the operating system standard package manager.

---
- hosts: web 
  user: root
  become: yes
  connection: ssh
  tasks:
  - name: install ntpdate
    package:
      name: ntpdate
      state: present

Each module can be used by the Ansible API, or by the ansible or ansible-playbook programs.If you need functionality that is not available in any of the thousands of Ansible modules found in collections, you can easily write your own custom module.

References

Ansible Documents

Other Doc.

Written by 

Harshit Gupta is a Software Consultant at knoldus Inc having few year experience in DevOps . He always eager to learn new technology and helping to others .