Ansible Reference Documents

Understanding Target and Manage Nodes
Launching Instances (e.g., AWS EC2)
Using Git Bash for Ansible
Using Vim Editor for Playbooks
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
Install Ansible on the manage node.
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:
Use the AWS Console or CLI to launch an Ubuntu EC2 instance.
Make sure the instance:
Is in a public subnet
Has port 22 open (SSH)
Is associated with a key pair (e.g.,
mykey.pem
)
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 modeEsc
โ 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 commandsUse
vim
to edit.yml
files
Using
ansible-doc
commandUsing the
command
moduleUsing the
file
moduleManaging 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?
Subscribe to my newsletter
Read articles from Piyush Kabra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
