LinuxUnity LinuxUnity

Posts / Ansible

[Ansible] Part 2: Installing and Getting Started with Ansible

This article explains how to install and configure Ansible on Ubuntu and introduces the basics of using Ansible ad hoc commands.

11/07/2026 5 min read 11 views

Z Zedx

In this article

In this article, I will guide you through installing and configuring Ansible on Ubuntu.

Install Ansible

To install Ansible on Ubuntu, connect to the server via SSH and run the commands below.

text
apt update
add-apt-repository --yes --update ppa:ansible/ansible
apt install ansible

*Note: Add sudo before each command if your user does not have root privileges.

cài đặt ansible

Check whether Ansible has been installed successfully using the command

text
 ansible --version

So, the installation was successful. From this result, you can obtain information about the environment and the paths to the configuration files of this automation tool.

Ansible Config

Ansible configurations are stored in the file /etc/ansible/ansible.cfg. Some important default settings include:

– forks: specifies the number of connections Ansible makes concurrently to remote servers; the default is 5. For example, if you have 10 remote servers, Ansible will establish 5 connections to 5 servers. After they complete, it will establish 5 more connections to the remaining 5 servers. You can increase this number to reduce execution time. However, opening too many connections simultaneously can affect the resources on the control server. Therefore, configure this parameter according to the available resources.

– host_key_checking: enabled by default (True). This means that when Ansible connects to a remote server over SSH, it checks the host key (usually stored in the file ~/.ssh/known_hosts). If the remote server's key does not match the information in that file, Ansible stops and warns the user.

Enabling this feature provides stronger security and helps prevent remote-server spoofing and man-in-the-middle attacks.

For systems that frequently update their SSH keys or for development environments, you can set it to False to disable this feature.

In addition, there are other parameters related to the default inventory and module declaration locations in Ansible, along with color options when running Ansible Ad Hoc commands or Ansible Playbooks.

Deployment model

Model

mô hình triển khai ansible

Hostname OS IP
Control Server – Ansilbe Ubuntu 192.168.33.10
Remote Server – Server A Ubuntu 192.168.33.20
Remote Server – Server B Ubuntu 192.168.33.30

Declaring the inventory

You need to declare information about the remote servers in the default file /etc/ansible/hosts. However, I will create a separate file for easier management.

Create an inventory-lab file using the syntax below to provide the SSH information that Ansible needs to connect to the remote servers.

In this article, I will cover only SSH password authentication; I will explain SSH key authentication in greater detail in a later article.

text
<name_server> ansible_ssh_host=<IP> ansible_ssh_user=<user> ansible_ssh_port=<port SSH> ansible_ssh_pass=<password SSH>
  • name_server: sets a name for the remote server to distinguish it from other servers
  • ansible_ssh_host: the IP address of the remote server
  • ansible_ssh_user: The user used to establish the SSH connection, typically the root user or a user with sudo privileges
  • ansible_ssh_port: If you use the default SSH port – port 22, you do not need to specify this parameter
  • ansible_ssh_pass: SSH password

Getting Started with Ansible Ad Hoc Commands

Ad hoc commands in Ansible are quick commands used to perform a one-off administrative task without writing a Playbook. They are particularly useful when you only need to run a temporary command or perform a quick check on remote hosts, such as testing connectivity, starting a service, or copying a file.

The general syntax of an ad hoc command is:

text
ansible [group] -m [module] -a "[arguments]" -i [inventory]
  • [group]: The group of servers or target hosts on which you want to execute the command.
  • -m [module]: The name of the Ansible module you want to use.
  • -a “[arguments]”: The arguments passed to the module.
  • -i [inventory]: Specifies the server inventory file.

To test connectivity to remote hosts, you can use Ansible's Ping module to ping the remote servers (similar to the Linux ping command for an IP address or domain).

text
ansible all -i inventory-lab -m ping
  • all: Tells Ansible to run the command on all remote servers defined in the inventory file, specifically the inventory-lab file created earlier.
  • -m ping: The Ansible ping module

As shown, after a “ping” is sent to the remote servers, they respond to the control server with “pong”, indicating that Ansible has connected successfully. This is also the first step in verifying the connection and Ansible configuration between the control server and the remote servers. The administrator can then perform other tasks as required.

Now, if we change the SSH password for server-B, the result will no longer return “pong” as before. Instead, it will display an error indicating that an SSH connection to server-B could not be established.

At this point, you have successfully installed Ansible and gained a general understanding of the tool. In the following articles, I will provide a more detailed explanation of Ansible's key components, such as inventory, modules,… and how to use them with Ansible ad hoc commands and Ansible Playbooks.