Ansible: My First Impression

David Lin
3 min readMay 6, 2023

--

Ansible is an open-source automation tool that allows users to automate the deployment, configuration, and management of infrastructure, and designed to be simple and flexible. Having used it for the first time recently, I’m sharing my understandings and thoughts on the tool.

What exactly is Ansible?

The core of Ansible is a push-based agentless architecture, meaning that there is no need to install any software on the target machines. Ansible mainly uses SSH connections to remotely execute tasks on the target machines, which are defined in Ansible playbooks written in YAML.

Ansible playbooks

Ansible playbooks describe the desired state of the infrastructure components, and tasks in the playbook are executed using pre-built modules that perform specific actions that can include installing software, managing users, or updating configurations.

Idempotence

An extremely useful feature of Ansible is its idempotence. Ansible tasks are designed to be repeatable, which means that multiple runs of the same playbook should produce the same results. Ansible accomplishes this by checking the current state of the target machines before executing a task, and only performing the task if it’s necessary to bring the target to the desired state.

Multi-hosts

Another key feature of Ansible is its ability to work with multiple types of hosts, including Linux, Windows, and network devices. Ansible provides built-in modules for managing different types of infrastructure components, and users can also create custom modules to extend its functionality.

The Ansible control node manages the execution of the playbooks, keeping track of which tasks have been completed and which ones are still pending. It also provides reporting and logging capabilities, allowing users to monitor the progress of their automation tasks.

Trying it out

The following steps will help you to get started with using Ansible:

  1. Create a new file called ping.yml and open it in your preferred text editor.
  2. Add the following lines to the file and save it:

In the playbook, name describes what the playbook does, hosts specifies the remote server on which to run the playbook, become tells Ansible to elevate the user's privileges to root using sudo (it’s not really necessary in this example but it is common to use this feature), and tasks contains the actual work to be done, which is to ping the remote server using the ping module.

To execute the playbook, run the following command in your terminal:

# Install ansible
pip3 install ansible
# Execute the playbook
ansible-playbook ping.yml

This command tells Ansible to execute the ping.yml playbook. Ansible will connect to the remote server specified in the playbook, ping it using the ping module, and exit.

On Successful Ping:

PLAY [Ping remote server] *****************************************************

TASK [Gathering Facts] ********************************************************
ok: [remote-server]

TASK [Ping the remote server] **************************************************
ok: [remote-server]

PLAY RECAP *********************************************************************
remote-server : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

The ping task was successful and returned an "ok" message, indicating that the remote server is reachable.

On Failed Ping:

PLAY [Ping remote server] *****************************************************

TASK [Gathering Facts] ********************************************************
ok: [remote-server]

TASK [Ping the remote server] **************************************************
fatal: [remote-server]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host remote-server port 22: Connection timed out\r\n", "unreachable": true}

PLAY RECAP *********************************************************************
remote-server : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0

The ping task failed and returned an "UNREACHABLE" message, indicating that Ansible was unable to connect to the remote server via SSH. The message also includes a detailed error message that explains the reason for the failure.

Note that this is an example and failure messages may differ with different errors.

That’s it! You’ve just created and executed an Ansible playbook using the ping module. While this is a simple example, the ping module can be used as a basic connectivity test as part of more complex playbooks.

Thank you for taking the time to read this! I hope that this article has helped you to gain a better understanding of these technologies and how they can be used to protect user data and provide a seamless experience for your users. If you have any questions or would like to learn more, please don’t hesitate to reach out.
To see what I’m working on, visit my
GitHub or my personal website.

--

--

David Lin
David Lin

Written by David Lin

Aspiring Software Engineer, building for a better future!

No responses yet