Bookmark this page

Configuring Ansible

Objectives

After completing this section, you should be able to:

  • Recognize valid locations for the Ansible configuration file.

  • Describe how to determine which Ansible configuration file will be used.

  • Customize the connection method, authentication details, privilege escalation, and performance.

The Ansible Configuration

Ansible settings are stored in its configuration file.

  • Ansible chooses which configuration file to use from several possible locations on the control node.

  • The Ansible installation provides the default configuration file /etc/ansible/ansible.cfg. Settings and their default values are described in this file using comments.

  • A sample configuration file that documents all settings with comments is also available on the web:

https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg

Note

Misspelled section headings, such as [default], are ignored. The correct heading is [defaults].

Which Configuration File?

Ansible uses the environment and the file system to determine which configuration file to use.

It searches these in this order and uses the first file it finds (it only uses one configuration file):

  1. The one specified by the ANSIBLE_CONFIG environment variable, if set

  2. The current working directory: ./ansible.cfg

  3. A hidden file in the user's home directory: ~/.ansible.cfg

  4. The system's default configuration file: /etc/ansible/ansible.cfg

Recommended Practice

You do not have to do things a particular way, but these work well in many contexts.

Use Version Control

Use a Version Control System (VCS) to manage Ansible Playbooks as projects.

Store Ansible Configuration with VCS Project

Store a copy of the ansible.cfg file at the top level of the project.

Configure the Hosts Inventory Location

In the [defaults] section, use inventory = filepath to specify the inventory file location.

Which Configuration File?

If you are uncertain about which configuration file is being used, run this command:

[user@host ~]$ ansible --version
ansible 2.5.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']!
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

Managing Settings

The Ansible configuration file uses the familiar INI format. Comments are prefixed by either the number (#) or semicolon (;).

Settings are defined using key-value pairs that fall under one of these groups:

  • defaults

  • privilege_escalation

  • paramiko_connection

  • ssh_connection

  • accelerate

  • Selinux

The sections that contain settings most likely to be useful in terms of customization are defaults and privilege_escalation.

Key Configuration Information

Key information that Ansible relies on to connect to hosts includes:

  • Where the inventory is that lists the managed hosts and host groups.

  • Which connection protocol to use to communicate with the managed hosts.

  • Whether the protocol uses its standard port or a non-standard one, and if not the standard one, which port to use.

  • Which identity to use to access managed hosts.

  • Whether privilege escalation is needed when using an unprivileged user to access a managed host.

  • Whether to prompt for an SSH password or sudo password to log in or gain privileges.

The Configuration File and Effective Settings

Customization of ansible.cfg establishes settings that apply when not otherwise overridden by being set elsewhere in ways that have higher precedence.

  • The ansible.cfg file establishes a reasonable set of default behaviors.

  • The recommended practice is to create an ansible.cfg file in a directory from which you run Ansible commands, or where playbooks are located. When an ansible.cfg file exists in the playbook directory, it is used instead of the configuration file in the default location (/etc/ansible/ansible.cfg).

Commonly Modified Settings

The table shown below shows commonly modified directives in ansible.cfg.

SettingDescription
inventoryThe location of the Ansible inventory.
remote_userThe remote user account used to establish connections to managed hosts.
ask_passPrompt for a password to use when connecting as the remote user.
becomeEnable or disable privilege escalation for operations on managed hosts.
become_methodThe privilege escalation method to use on managed hosts.
become_userThe user account to escalate privileges to on managed hosts.
become_ask_passDefines whether privilege escalation on managed hosts should prompt for a password.
gatheringSet to explicit to disable gathering by default.

Sample Custom Configuration Files

The following ansible.cfg configuration file defaults to always prompting for the password used to access devices.

[defaults]
inventory = inventory
ask_pass = True
host_key_checking = False
[persistent_connection]
# custom timeout values to accommodate slow 1000V VM
command_timeout = 180
connect_timeout = 100
connect_retry_timeout = 100

The following ansible.cfg configuration file uses a Vault password file, which must be suitably protected by file-system permissions.

[defaults]
inventory = inventory
ask_pass = True
host_key_checking = False

Configuring Authentication Details

By default, Ansible connects to managed hosts using SSH, and defaults to using the same user name on remote systems as the local user running Ansible commands. To use a different identity, set the remote_user parameter to the desired user name.

If the local user running Ansible has private SSH keys configured that allow them to authenticate as the remote user on the managed hosts, Ansible automatically logs in. If that is not the case, you can configure Ansible to prompt the local user for the password used by the remote user by setting the directive ask_pass = true.

Escalating Privilege Levels

Privilege escalation settings are configured in the [privilege_escalation] section.

To enable privilege escalation by default, set the directive become = true in the configuration file.

Other privilege escalation settings include become_method, which defaults to sudo, and become_user, which defaults to root. If become_method requires providing a password, set become_ask_pass = true in the configuration file.

Ansible 2.5 added support for become to be used to enter enable mode (Privileged EXEC mode) on network devices that support it.

You must set the host connection type to connection: network_cli to use become for privilege escalation on network devices. Ansible 2.5.3 supports become for privilege escalation on eos, ios, and nxos.

Connection Settings: Smart or Not?

Connection decisions: SSH or not? If SSH, smart or not?

Smart (Default)

Ansible determines the most efficient way to use SSH to connect to managed hosts. For instance, Ansible uses SSH multiplexing if it can be used.

Explicit SSH Connection Plug-in

Ansible can be configured to use a particular SSH connection plug-in.

Non-SSH Connection Plug-in

Ansible can be configured to use a particular non-SSH connection plug-in.

This is important for managed network devices or Microsoft Windows systems.

Using the Local Connection Method

The special connection type local is available for running ad hoc commands and plays locally on the Ansible control node.

When a localhost entry is not listed in your inventory file, Ansible defines localhost implicitly so that ad hoc commands and playbooks target the control node. Ansible defaults to the local connection type when using the implicit localhost definition.

The local connection type ignores the remote_user setting and runs commands directly on the local system. If privilege escalation is used, it runs sudo from the user account that ran the Ansible command, not remote_user.

Before Ansible 2.5, network modules used local connections to run tasks on the control node that modified remote network devices.

Connecting to Network Devices

The netconf and network_cli connection plug-ins connect to network devices. These were introduced NEW in version 2.5.

The NETWORK_CLI plug-in sends and receives network device CLI commands.

http://docs.ansible.com/ansible/2.5/plugins/connection/network_cli.html

$ ansible-doc -t connection network_cli

The NETCONF plug-in provides a persistent connection using the netconf protocol.

http://docs.ansible.com/ansible/2.5/plugins/connection/netconf.html

$ ansible-doc -t connection netconf

These new connection plug-ins allow network automation plays to be written in the same style as plays used with servers.

Using Netconf or Network_Cli

Which should I use?

The target platforms (the network devices you will be managing) must be supported by the connection method you use to manage them.

Network_Cli

This connection plug-in provides a connection to remote devices over the SSH and implements a CLI shell. It is typically used by network devices for sending and receiving CLI commands to network devices. This connection method supports a broad variety of network devices, including the IOS and VyOS devices in the Lab Environment.

Netconf

This connection plug-in provides a persistent connection to remote devices over the SSH NETCONF subsystem. It is typically used by network devices for sending and receiving RPC calls over NETCONF. This plug-in requires ncclient to be installed on the local Ansible controller. It does not currently support the network devices in the Lab Environment.

Connection Method and Play Performance

Ansible uses connection plug-ins to connect to managed hosts.

Use ansible-doc -t connection -l to see a list of available connection plug-ins. The important ones for network automation are network_cli and netconf. The netconf_cli connection plug-in currently supports the widest range of platforms. The netconf plug-in provides a connection to devices over the SSH NETCONF subsystem.

Different connection plug-ins have different performance characteristics.

How Ansible connects to hosts affects how long it takes to execute a play.

Plays typically run faster using plug-ins that support persistent connections compared to those that create and delete connections for each task.

More Play Performance Optimization

When downloading resources such as packages, use local mirrors or caches whenever possible.

The forks parameter determines how many hosts Ansible applies a play to in parallel. By default, Ansible sets the forks parameter conservatively to 5.

Whenever the number of hosts to which a play is being applied is smaller than the fork value, Ansible only spins up as many ansible-playbook processes as there are hosts in the target set.

Serializing How Hosts are Mapped to Plays

The value of the serial setting is used to specify how many hosts run all the way through a play before proceeding to the next set of that many hosts.

With serial: 5, for instance, Ansible runs five hosts all the way through the play, then runs the next five hosts all the way through the play, until it runs out of hosts targeted by the play.

If you have 100 routers, and want to only work on ten at a time, use serial: 10. The play is applied to ten hosts. When done with the first ten, the play is applied to the next ten hosts, all the way through the play, until it runs out of targeted hosts.

By default, serial is set to 0, which runs all targeted hosts through the play in parallel.

Forking Tasks for Hosts

With the forks setting, all the hosts in the play complete a task before moving to the next task in the play.

With forks: 5, for instance, Ansible talks to 5 hosts for each task at a time in parallel, and has all hosts in the play complete that task before moving to the next task in the play.

By default, forks is set to 5.

Combining Serial and Forks

With forks: M and serial: N, Ansible talks to M hosts at a time for each task, and runs them all the way through the play in groups of N.

Upon completing the play with the first N hosts, it picks N more hosts from the target and work through the play, doing each task across M hosts at a time in parallel.

Revision: do457-2.5-4693601