Ansible Reference Documents

Piyush KabraPiyush Kabra
4 min read
  1. Understanding Target and Manage Nodes

  2. Launching Instances (e.g., AWS EC2)

  3. Using Git Bash for Ansible

  4. Using Vim Editor for Playbooks

  5. Using Ansible command Module


๐Ÿ”น 1. Target Node & Manage Node

  • Manage Node: The system where Ansible is installed (your local machine or control node).

  • Target Node: The remote system (e.g., a Linux server or EC2 instance) managed by Ansible.

โœ… Configuration

  1. Install Ansible on the manage node.

  2. Ensure SSH access to the target node.

Example Inventory (hosts.ini):

[target]
192.168.1.101 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

Test connection:

ansible target -i hosts.ini -m ping

๐Ÿ”น 2. Instances (e.g., Launch EC2 on AWS)

If using AWS:

  1. Use the AWS Console or CLI to launch an Ubuntu EC2 instance.

  2. Make sure the instance:

    • Is in a public subnet

    • Has port 22 open (SSH)

    • Is associated with a key pair (e.g., mykey.pem)

  3. SSH into it using:

ssh -i ~/.ssh/mykey.pem ubuntu@<public-ip>

Update your Ansible hosts.ini like:

[aws]
<public-ip> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/mykey.pem

๐Ÿ”น 3. Git Bash

If youโ€™re on Windows, use Git Bash to run Ansible commands.

โœ… Steps:

  • Install Git for Windows

  • Use Git Bash terminal

  • Ensure ansible is available via WSL or a VM like Ubuntu

Run a command:

ansible all -i hosts.ini -m ping

๐Ÿ”น 4. Vim Editor

Use Vim to edit your Ansible playbooks on Linux/macOS/WSL.

Basic Usage:

vim playbook.yml

Vim Shortcuts:

  • i โ€“ insert mode

  • Esc โ€“ normal mode

  • :wq โ€“ write and quit

  • :q! โ€“ quit without saving

Example Playbook:

- name: Test playbook
  hosts: target
  tasks:
    - name: Run date command
      ansible.builtin.command: date

๐Ÿ”น 5. Ansible command Module

โœ… Ad-hoc Example:

ansible target -i hosts.ini -m command -a "uname -a"

โœ… Playbook Example (command_play.yml):

- name: Run commands using Ansible
  hosts: target
  tasks:
    - name: Display uptime
      ansible.builtin.command: uptime

    - name: List root directory
      ansible.builtin.command: ls -l /

Run it:

ansible-playbook -i hosts.ini command_play.yml

โœ… Summary of Required Files

  • hosts.ini (inventory)

  • command_play.yml (playbook)

  • Use Git Bash or a terminal to run Ansible commands

  • Use vim to edit .yml files


  1. Using ansible-doc command

  2. Using the command module

  3. Using the file module

  4. Managing nodes (inventory, ad-hoc commands, playbook)


๐Ÿ”น 1. ansible-doc Command

Purpose: View documentation for Ansible modules.

Step-by-Step:

# View all available modules
ansible-doc -l

# View documentation for a specific module (e.g., command)
ansible-doc command

๐Ÿ”น 2. command Module

Purpose: Run shell commands on remote nodes.

Example: Run uptime on remote host

Inventory file (hosts.ini):
[web]
192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
Ad-hoc command:
ansible web -i hosts.ini -m command -a "uptime"
In a playbook (command_playbook.yml):
- name: Run uptime command
  hosts: web
  tasks:
    - name: Show uptime
      ansible.builtin.command: uptime

Run it:

ansible-playbook -i hosts.ini command_playbook.yml

๐Ÿ”น 3. file Module

Purpose: Manage file properties (permissions, ownership, creation/removal).

Example 1: Create a directory

- name: Create a directory
  hosts: web
  tasks:
    - name: Make /tmp/demo directory
      ansible.builtin.file:
        path: /tmp/demo
        state: directory
        mode: '0755'

Example 2: Remove a file

- name: Remove a file
  hosts: web
  tasks:
    - name: Delete /tmp/oldfile
      ansible.builtin.file:
        path: /tmp/oldfile
        state: absent

๐Ÿ”น 4. Managing Nodes (Inventory + Ad-Hoc + Playbook)

Inventory example (hosts.ini):

[web]
192.168.1.10

[db]
192.168.1.11

Ad-hoc command:

ansible all -i hosts.ini -m ping

Playbook example (setup_env.yml):

- name: Ensure environment is set up
  hosts: all
  tasks:
    - name: Create a directory
      ansible.builtin.file:
        path: /opt/appdir
        state: directory
        mode: '0755'

    - name: Run hostname command
      ansible.builtin.command: hostname

Run the playbook:

ansible-playbook -i hosts.ini setup_env.yml

Would you like me to package all this into ready-to-run files (.yml, hosts.ini, etc.) or walk you through setting this up in your environment?

0
Subscribe to my newsletter

Read articles from Piyush Kabra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Piyush Kabra
Piyush Kabra