Skip to main content

Installing and Configuring Ansible

1. Installing Ansible

Example: Install Ansible using pip

pip install ansible
Installation Method Description Notes
pip pip install ansible Installs latest stable version
package manager (Ubuntu/Debian) apt update && apt install ansible Uses system package manager
package manager (CentOS/RHEL) yum install ansible Uses yum or dnf depending on version
Homebrew (macOS) brew install ansible Installs via Homebrew
Note: Ensure Python 3.8 or higher is installed before installing Ansible.

2. Verifying Installation

Command Description Output
ansible --version Displays the installed Ansible version and configuration details
ansible [core 2.16.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections
  python version = 3.10.12 (main, Nov 20 2024, 13:25:08) [GCC 11.4.0]
  jinja version = 3.1.3
  libyaml = True
ansible --help Shows the main help menu with available options and commands ansible [options] [inventory] [pattern]
ansible-doc --list Lists all available modules in the installed Ansible collection
ansible.builtin.apt
ansible.builtin.copy
ansible.builtin.debug
ansible.builtin.file
ansible.builtin.get_url
ansible.builtin.group
ansible.builtin.host_group
ansible.builtin.lineinfile
ansible.builtin.ping
ansible.builtin.shell
ansible.builtin.template
ansible.builtin.user

Example: Check Ansible version

$ ansible --version
ansible [core 2.16.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections
  python version = 3.10.12 (main, Nov 20 2024, 13:25:08) [GCC 11.4.0]
  jinja version = 3.1.3
  libyaml = True
Note: Ensure that the version displayed is compatible with your playbooks and roles.

3. Understanding ansible.cfg

Option Values Description
inventory hosts.ini Path to the inventory file
library /usr/share/ansible Path to the module library
remote_tmp /tmp/.ansible/tmp Directory for temporary files on remote hosts
local_tmp /tmp/.ansible/tmp Directory for temporary files on the local host
log_path None Path to the log file
host_key_checking True Enable host key checking for SSH connections
gathering implicit Control how facts are gathered (implicit, explicit, smart)
fact_caching memory Cache facts in memory (or file, redis, etc.)
fact_caching_timeout 86400 Time in seconds before cached facts expire

Example: Basic ansible.cfg

[defaults]
inventory = hosts.ini
library = /usr/share/ansible
remote_tmp = /tmp/.ansible/tmp
local_tmp = /tmp/.ansible/tmp
host_key_checking = False
gathering = smart
fact_caching = memory
fact_caching_timeout = 86400

[ssh_connection]
pipelining = True

4. Configuring Default Settings

Setting Default Value Description
ANSIBLE_CONFIG ansible.cfg Path to the configuration file
ANSIBLE_HOME ~/.ansible Directory for storing logs and cache
ANSIBLE_LIBRARY ~/.ansible/plugins Path to custom modules
ANSIBLE_PLAYBOOK_DIR . Directory where playbooks are stored
ANSIBLE_ROLES_PATH ./roles Path to roles directory
ANSIBLE_INVENTORY /etc/ansible/hosts Path to inventory file
ANSIBLE_HOST_KEY_CHECKING true Enables host key checking for SSH connections
ANSIBLE_TIMEOUT 10 Timeout in seconds for SSH connections
ANSIBLE_REMOTE_USER root Default user for remote connections
ANSIBLE_PRIVATE_KEY_FILE None Path to private key for SSH authentication

Example: Setting Default User in ansible.cfg

[defaults]
remote_user = ansible_user
host_key_checking = false
timeout = 30

5. Setting Up SSH Keys

Ansible uses SSH to communicate with managed nodes. Properly configuring SSH keys is essential for secure and automated interactions.

Step Description Action
Generate SSH Key Pair Creates a public and private key for authentication. ssh-keygen -t rsa -b 4096 -C "ansible@yourdomain.com"
Copy Public Key to Target Hosts Ensures Ansible can authenticate without password prompts. ssh-copy-id user@target-host
Configure SSH Permissions Maintains security by restricting access to private keys. chmod 600 ~/.ssh/id_rsa
Note: Ensure the private key is securely stored and not committed to version control.

Example: SSH Key Setup Script

#!/bin/bash
# Generate SSH key if not exists
if [ ! -f ~/.ssh/id_rsa ]; then
  ssh-keygen -t rsa -b 4096 -C "ansible@yourdomain.com" -N ""
fi

# Copy public key to target host
ssh-copy-id user@target-host

6. Configuring SSH Connection

Option Values Notes
ansible_connection ssh Default connection type for Ansible
ansible_ssh_host hostname.example.com Target host to connect to
ansible_ssh_user admin User to connect as via SSH
ansible_ssh_port 22 SSH port to connect on
ansible_ssh_private_key_file ~/.ssh/id_rsa Path to private key file for authentication
ansible_ssh_extra_args -o StrictHostKeyChecking=no Extra arguments to pass to SSH client

Example: SSH Configuration in Inventory

[webservers]
web1 ansible_ssh_host=192.168.1.10 ansible_ssh_user=admin ansible_ssh_private_key_file=~/.ssh/id_rsa
web2 ansible_ssh_host=192.168.1.11 ansible_ssh_user=admin ansible_ssh_private_key_file=~/.ssh/id_rsa
Note: Ensure SSH keys are properly set up and permissions are correct for key-based authentication.
Warning: Using -o StrictHostKeyChecking=no can expose you to man-in-the-middle attacks in production environments.

7. Disabling Host Key Checking

By default, Ansible verifies the SSH host key of managed nodes to ensure secure communication. In some environments, such as development or testing setups with dynamically provisioned hosts, this verification can cause failures.

Option Values Notes
host_key_checking false Disables SSH host key checking for all connections

Example: Disabling host key checking in ansible.cfg

[defaults]
host_key_checking = False
Note: Disabling host key checking reduces security by making the system vulnerable to man-in-the-middle attacks. Only disable it in trusted environments.

8. Setting Default Inventory

Option Values Notes
inventory hosts.ini Default inventory file if not specified
inventory hosts.yaml YAML format inventory file
inventory hosts.json JSON format inventory file

Example: Default Inventory File (hosts.ini)

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com
db2.example.com

Example: YAML Inventory File (hosts.yaml)

all:
  hosts:
    web1.example.com:
    web2.example.com:
  children:
    webservers:
      hosts:
        web1.example.com:
        web2.example.com:
    databases:
      hosts:
        db1.example.com:
        db2.example.com:

9. Configuring Log Path

Configuration Option Default Value Description
log_path None Path to the log file where Ansible will write logs. If not set, logs are written to standard output.

Example: Setting Log Path in ansible.cfg

[defaults]
log_path = /var/log/ansible/ansible.log
Note: Ensure the specified log path is writable by the user running Ansible.

10. Understanding Configuration Precedence

Configuration Source Priority Description
Command-line options 1 Highest priority, overrides all others
Inventory file variables 2 Defined per host or group in inventory
Playbook variables 3 Defined in playbooks or roles
Host facts 4 Collected from managed hosts
Group vars 5 Defined in group_vars/ directories
Host vars 6 Defined in host_vars/ directories
Ansible configuration file 7 Defined in ansible.cfg or ~/.ansible.cfg
Defaults 8 Default values from Ansible core
Note: Configuration precedence can be overridden using ansible-playbook --extra-vars or by setting ANSIBLE_CONFIG environment variable.

Was this article helpful?