Ansible Introduction: A Simple Guide for Starters
Hello folks, you must’ve come to this article after reading the title so yeah we’re gonna be learning Ansible and try to write a simple ansible playbook to do some stuff on another laptop.
First things first, What is Ansible?
Ansible is an automation tool used to configure systems, deploy software, and manage infrastructure on a large scale using code instead of manual commands.
In simpler terms, if you're a DevOps person with several servers that need configuration updates, Ansible is the tool you should use.
So, let’s talk how Ansible works or what do we need to do write our first playbook.
Sorry I just get ahead with myself and let me introduce you to some buzz words around Ansible.

So basically, Ansible has three main components.
Control node - This is the system where Ansible is installed, and you can control the managed nodes from here.
Managed node - These are the systems that will be controlled by the control node.
Inventory - This is the list of nodes that the control node needs to manage.
Ansible uses simple, human-readable scripts called playbooks. They are used to automate tasks on your managed nodes.
Now, that's enough theory. Let's start writing our first playbook. Before we begin, we need to install a few things to make Ansible work.
Let’s first set up our managed node. You can use Ansible to automate servers deployed elsewhere, or you can run tasks on your local laptop. I have a spare laptop, so I'm going to use it. You can also check the documentation to automate servers. But for this demo, we're going to use our local laptop.
- Let's start preparing our managed node. First, we need Python, so let's install it:
# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-apt -y
# macOS
brew install python
- Enable SSH access:
# Ubuntu/Debian
sudo apt install openssh-server -y
sudo systemctl enable ssh
sudo systemctl start ssh
# macOS
sudo systemsetup -setremotelogin on
And the reason why we need the SSH access is, Ansible communicates with managed machines through SSH. Without SSH access, it cannot log in to the target system to execute commands or apply configurations.
- Note the managed node’s IP:
ip a # Linux
ifconfig # macOS
Now let’s prepare our controller node(Main laptop) and first we need to install Ansible on it:
brew install ansible # macOS sudo apt install ansible -y # UbuntuEnsure SSH key exists:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsaCopy public key to managed node for passwordless login:
#Ubuntu ssh-copy-id user@<managed-node-ip> #macos cat ~/.ssh/id_rsa.pub | ssh user@<managed-node-ip> 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'And where do you get the
managed-node-ip, check the Step 3 for that.Test SSH access:
ssh user@<managed-node-ip>Also, remember if you don’t what to write in place of
user- then run the commandwhoamiand check your usernameNow let’s create our inventory file,
[remote-laptop] managed-laptop ansible_host=<managed-node-ip> ansible_user=<username>Replace
<managed-node-ip>with the second laptop’s IP.Replace
<username>with the SSH user on the managed node.
Test the connectivity first:
ansible -i inventory.ini all -m pingExpected output:
SUCCESSfrom managed-laptop.Now let’s write our playbook and for this demo purpose we’re gonna be installing the NodeJs version 22 on our manged node and make it default. Create a new file called
update_node.yaml```bash
name: Manage node version with nvm hosts: remote-laptop become: no vars: nvm_dir: "{{ansible_env.HOME}}/.nvm" tasks:
name: List installed node versions shell: | export NVM_DIR={{ nvm_dir }} [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" nvm list args: executable: /bin/bash register: nvm_list
name: Print nodejs versions debug: msg="{{ nvm_list.stdout }}"
name: Install node version 22 shell: | export NVM_DIR={{ nvm_dir }} [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" nvm install 22 nvm use 22 nvm alias default 22 args: executable: /bin/bash
name: Verify node 22 shell: | export NVM_DIR={{ nvm_dir }} [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" node -v args: executable: /bin/bash register: node_version
name: Show node version debug: msg="{{ node_version.stdout }}"
Let’s break down our playbook:
* `name`: A human-readable description of the playbook.
* `hosts`: Targets the `remote-laptop` group from the inventory.
* `become: no`: We don't need `sudo` because NVM works in the user environment by default.
* `vars`: Defines `nvm_dir` as the NVM installation directory (default is the user's home).
* `tasks`: The tasks we need to run on the managed node.
* `name`: The name of the task.
* `shell`: Loads NVM in the shell environment for the following commands.
* `args`: `executable: /bin/bash` tells Ansible to run the task using bash instead of the default shell.
* `register: nvm_list`: Stores the output in `nvm_list` for later use.
* `debug: msg="{{ nvm_list.stdout }}"`: Displays the output stored in `nvm_list`.
Now time to test our first playbook:
```bash
ansible-playbook -i inventory.in update_node.yaml
You'll see the output in the terminal, but the real magic happens when you open the terminal on your managed-node laptop and check the Node version. It will be version 22, as specified in our inventory file.
And boom, you've automated the process of updating the Node version. I know it's very basic, but this is just for reference. You can always add more features to this; it's just for learning purposes.
If you enjoyed the article, please follow for more!