Frequently used ansible module with example

10 frequently used ansible modules with example

Last Updated on February 18, 2023 by cscontents

Introduction

In this article we will discuss frequently used ansible modules. Ansible has a huge number of modules, but among those a few are mostly used. If you are learning Ansible then it will be great if you are aware of which modules to be focused on.

Prerequisite

Before going through this article, I hope you are aware of below

  • Basics of Ansible
  • Have some hands-on experience of using Ansible (must be aware of ansible playbook, inventory file)
  • YAML syntax

If you want to learn Ansible basics then please check out the below course from KodeKloud which offers one of the best learning material in DevOps world.

Link of Training Course: Ansible for absolute beginners

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Ansible module

An ansible module is small piece of code written in python.

Ansible module:

  • Core Module — come with Ansible by default.
  • Custom Module — we can create our own module.

List of frequently used ansible module

  1. command, win_command
  2. file, win_file
  3. lineinfile, win_lineinfile
  4. get_url, win_get_url
  5. unarchive, win_unzip
  6. shell, win_shell
  7. replace
  8. stat, win_stat
  9. copy, win_copy
  10. ping, wing_ping
command, win_command module
  • command – this module is used for executing any command on Linux machine.
  • win_command – this module is used for executing any command on Windows machine.

An example use of command module is shown below.

- name: test playbook
  hosts: remote_server
  tasks:
    - name: creating folder
      command: mkdir /home/ubuntu/test-folder

From the above example it is clear we can use this command module to execute any command.

file, win_file module
  • file – this module is used to deal (e.g., creating new file, deleting any file etc.) with file in Linux machine.
  • win_file – this module is used to deal (e.g., creating new file, deleting any file etc.) with file in Windows machine.

Example use of this file module is shown below.

- name: create folder
  hosts: remote_server
  tasks:
    - name: testfolder creating
      win_file:
        path: C:\Temp\testfolder
        state: directory

The above playbook will create ‘testfolder’ at ‘C:\Temp’ directory on a Windows machine.

lineinfile, win_lineinfile module
  • lineinfile – using this module we can edit (adding any line, removing any line) any file in Linux machine.
  • win_lineinfile – using this module we can edit (adding any line, removing any line) any file in Windows machine.

Example use of this module is shown below.

- name: example use of lineinfile module
  hosts: remote_server
  tasks:
    - name: test file editing
      lineinfile:
        path: /home/ubuntu/test-file.txt
        state: present
        line: Hello World
get_url, win_get_url module
  • get_url – this module is used to download any file from any web URL on Linux machine.
  • win_get_url – this module is used to download any file from any web URL on Windows machine.

Example use of get_url module is shown below.

- name: get url example use
  hosts: remote_server
  tasks:
    - name: download nginx file
      get_url:
        url: http://nginx.org/download/nginx-1.22.0.zip
        dest: /home/ubuntu/nginx-1.22.0.zip

If the target url require authentication, then we can pass the username & password.

- name: get url example use
  hosts: remote_server
  tasks:
    - name: download nginx file
      get_url:
        url: http://nginx.org/download/nginx-1.22.0.zip
        url_username: username
        usrl_password: password
        dest: /home/ubuntu/nginx-1.22.0.zip
unarchive, win_unzip module
  • unarchive – this module is used to unzip a zipped file in Linux machine.
  • win_unzip – this module is used to unzip any zipped file in Windows machine.

Example use of unarchive module is shown below.

- name: unarchive example use
  hosts: remote_server
  tasks:
    - name: unzipping nginx-1.22.0.zip
      unarchive:
        src: /home/ubuntu/nginx-1.22.0.zip
        dest: /home/ubuntu/
        remote_src: yes
shell, win_shell module
  • shell – this module is used to execute any shell script in Linux machine.
  • win_shell – this module is used to execute any PowerShell script in Windows machine.

An example use of win_shell module is shown below:

- name: unarchive example use
  hosts: remote_server
  tasks:
    - name: Run software_install.ps1 script in PowerShell
      win_shell: C:\Temp\software_install.ps1

If we need to run the script as administrator, we can add few parameters as shown below.

- name: unarchive example use
  hosts: remote_server
  vars:
    user: username
    password: mypassword
  tasks:
    - name: Run software_install.ps1 script in PowerShell
      win_shell: C:\Temp\software_install.ps1
      vars:
        ansible_become: true
        ansible_become_method: runas
        ansible_become_user: "{{ user }}"
        ansible_become_password: "{{ password }}"
replace module
  • replace – this module can be used to replace any specific content in a file in Linux machine.

Example use of replace module is shown below.

- name: example use of replace module
  hosts: remote_server
  tasks:
    - name: Replace jboss binding IP address
      replace:
        path: "{{ EAP_HOME }}/standalone/configuration/standalone.xml"
        after: <interface name="public">
        regexp: '{jboss.bind.address:127.0.0.1}'
        replace: "{jboss.bind.address:10.54.34.234}"
stat, win_stat module
  • stat – this module is used to get facts about file in Linux machine.
  • win_stat – this module is used to get facts about file in Windows machine.

Example use of win_stat module is shown below.

- name: example use of win_stat module
  hosts: remote_server
  tasks:
    - name: retrieve facts about testfolder
      win_stat:
        path: C:\Temp\testfolder
copy, win_copy module
  • copy – this module is used to copy files in Linux machine.
  • win_copy – this module is used to copy files in Windows machine.

Example use of win_copy module is show below.

- name: Example use of win_copy module
  hosts: remote_server
  tasks:
    - name: Copy Test script file to Windows machine
      win_copy:
        src: './Test.ps1'
        dest: 'C:\Temp\Test.ps1'
ping, wing_ping module
  • ping – this nodule is used to ping any Linux machine.
  • win_ping – this module is used to ping any Windows machine.

An example use of ping module is shown below.

- name: Example use of ping module
  hosts: remote_server
  tasks:
    - name: pinging the target Linux machine
      ping:

 

Thank You.

Reference:

https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.