Ansible Configuration Settings

Let's dive into the common settings that can be configured in the ansible.cfg file.

  1. Hosts File Location (inventory):

    • The inventory setting specifies the location of your hosts file. This file lists the hosts (servers or devices) that Ansible will manage.

    • Example:

      [defaults]
      inventory = /etc/ansible/hosts
    • In this example, Ansible will use the standard /etc/ansible/hosts file as the inventory.

  2. Roles Path (roles_path):

    • The roles_path setting defines where Ansible looks for roles. Roles are reusable playbooks that encapsulate specific functionality.

    • Example:

      [defaults]
      roles_path = /path/to/your/custom_roles
    • Here, Ansible will search for roles in the specified directory.

  3. Parallel Processes (forks):

    • The forks setting determines the number of parallel processes Ansible uses when executing tasks.

    • Example:

      [defaults]
      forks = 10
    • In this case, Ansible will run up to 10 tasks concurrently.

  4. Privilege Escalation (become):

    • Privilege escalation allows Ansible to execute tasks with elevated permissions (e.g., using sudo).

    • Example:

      [privilege_escalation]
      become = True
    • Now Ansible will use privilege escalation when needed.

  5. SSH Arguments (ssh_args):

    • The ssh_args setting allows you to add additional SSH arguments.

    • Example:

      [ssh_connection]
      ssh_args = -o ForwardAgent=yes
    • Here, Ansible will forward the SSH agent for better authentication.

  6. Colorized Output (color):

    • The color setting controls whether Ansible displays colorized output.

    • Example:

      [defaults]
      color = auto
    • Ansible will automatically enable color if the terminal supports it.

You can customize your ansible.cfg based on your environment and requirements. Let's create a practical example of a custom ansible.cfg file that you might use in a production environment:

# ansible.cfg

# Set the path to your inventory file (hosts file)
inventory = /etc/ansible/hosts

# Configure logging options
log_path = /var/log/ansible.log
log_file = verbose

# Set the default SSH user
remote_user = myuser

# Enable colorized output
color = auto

# Specify the location of custom modules
library = /usr/local/share/ansible

# Fine-tune performance-related settings
forks = 10
timeout = 30

# Disable host key checking for SSH connections (use with caution)
host_key_checking = False

# Set the default number of retries for failed tasks
retry_files_enabled = False
max_retries = 3

# Enable or disable specific features as needed
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache

# Additional settings specific to your environment
# ...

# End of ansible.cfg

Here's a breakdown of the key settings:

  1. inventory: Points to your inventory file (usually /etc/ansible/hosts).

  2. log_path and log_file: Specify where Ansible logs should be stored.

  3. remote_user: Set the default SSH user for connections.

  4. color: Enables colorized output for readability.

  5. library: Defines the location of custom modules.

  6. forks and timeout: Control performance-related settings.

  7. host_key_checking: Disables host key checking (use with caution).

  8. retry_files_enabled and max_retries: Configure task retries.

  9. [defaults] section: Additional global settings.

Last updated