Frequently Asked Questions

Here are some commonly asked questions and their answers.

How can I set the PATH or any other environment variable for a task or entire playbook?

Setting environment variables can be done with the environment keyword. It can be used at the task or other levels in the play:

  1. environment:
  2. PATH: "{{ ansible_env.PATH }}:/thingy/bin"
  3. SOME: value

Note

starting in 2.0.1 the setup task from gatherfacts also inherits the environment directive from the play, you might need to use the |default_ filter to avoid errors if setting this at play level.

How do I handle different machines needing different user accounts or ports to log in with?

Setting inventory variables in the inventory file is the easiest way.

For instance, suppose these hosts have different usernames and ports:

  1. [webservers]
  2. asdf.example.com ansible_port=5000 ansible_user=alice
  3. jkl.example.com ansible_port=5001 ansible_user=bob

You can also dictate the connection type to be used, if you want:

  1. [testcluster]
  2. localhost ansible_connection=local
  3. /path/to/chroot1 ansible_connection=chroot
  4. foo.example.com ansible_connection=paramiko

You may also wish to keep these in group variables instead, or file them in a group_vars/<groupname> file.See the rest of the documentation for more information about how to organize variables.

How do I get ansible to reuse connections, enable Kerberized SSH, or have Ansible pay attention to my local SSH config file?

Switch your default connection type in the configuration file to ‘ssh’, or use ‘-c ssh’ to useNative OpenSSH for connections instead of the python paramiko library. In Ansible 1.2.1 and later, ‘ssh’ will be usedby default if OpenSSH is new enough to support ControlPersist as an option.

Paramiko is great for starting out, but the OpenSSH type offers many advanced options. You will want to run Ansiblefrom a machine new enough to support ControlPersist, if you are using this connection type. You can still manageolder clients. If you are using RHEL 6, CentOS 6, SLES 10 or SLES 11 the version of OpenSSH is still a bit old, soconsider managing from a Fedora or openSUSE client even though you are managing older nodes, or just use paramiko.

We keep paramiko as the default as if you are first installing Ansible on an EL box, it offers a better experiencefor new users.

How do I configure a jump host to access servers that I have no direct access to?

You can set a ProxyCommand in theansible_ssh_common_args inventory variable. Any arguments specified inthis variable are added to the sftp/scp/ssh command line when connectingto the relevant host(s). Consider the following inventory group:

  1. [gatewayed]
  2. foo ansible_host=192.0.2.1
  3. bar ansible_host=192.0.2.2

You can create group_vars/gatewayed.yml with the following contents:

  1. ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected]"'

Ansible will append these arguments to the command line when trying toconnect to any hosts in the group gatewayed. (These arguments are usedin addition to any ssh_args from ansible.cfg, so you do not need torepeat global ControlPersist settings in ansible_ssh_common_args.)

Note that ssh -W is available only with OpenSSH 5.4 or later. Witholder versions, it’s necessary to execute nc %h:%p or some equivalentcommand on the bastion host.

With earlier versions of Ansible, it was necessary to configure asuitable ProxyCommand for one or more hosts in ~/.ssh/config,or globally by setting ssh_args in ansible.cfg.

How do I get Ansible to notice a dead target in a timely manner?

You can add -o ServerAliveInterval=NumberOfSeconds in ssh_args from ansible.cfg. Without this option, SSH and therefore Ansible will wait until the TCP connection times out. Another solution is to add ServerAliveInterval into your global SSH configuration. A good value for ServerAliveInterval is up to you to decide; keep in mind that ServerAliveCountMax=3 is the SSH default so any value you set will be tripled before terminating the SSH session.

How do I speed up management inside EC2?

Don’t try to manage a fleet of EC2 machines from your laptop. Connect to a management node inside EC2 firstand run Ansible from there.

How do I handle python not having a Python interpreter at /usr/bin/python on a remote machine?

While you can write Ansible modules in any language, most Ansible modules are written in Python,including the ones central to letting Ansible work.

By default, Ansible assumes it can find a /usr/bin/python on your remote system that iseither Python2, version 2.6 or higher or Python3, 3.5 or higher.

Setting the inventory variable ansible_python_interpreter on any host will tell Ansible toauto-replace the Python interpreter with that value instead. Thus, you can point to any Python youwant on the system if /usr/bin/python on your system does not point to a compatiblePython interpreter.

Some platforms may only have Python 3 installed by default. If it is not installed as/usr/bin/python, you will need to configure the path to the interpreter viaansible_python_interpreter. Although most core modules will work with Python 3, there may be somespecial purpose ones which do not or you may encounter a bug in an edge case. As a temporaryworkaround you can install Python 2 on the managed host and configure Ansible to use that Python viaansible_python_interpreter. If there’s no mention in the module’s documentation that the modulerequires Python 2, you can also report a bug on our bug tracker so that the incompatibility can be fixed in a future release.

Do not replace the shebang lines of your python modules. Ansible will do this for you automatically at deploy time.

Also, this works for ANY interpreter, i.e ruby: ansible_ruby_interpreter, perl: ansible_perl_interpreter, etc,so you can use this for custom modules written in any scripting language and control the interpreter location.

Keep in mind that if you put env in your module shebang line (#!/usr/bin/env <other>),this facility will be ignored so you will be at the mercy of the remote $PATH.

How do I handle the package dependencies required by Ansible package dependencies during Ansible installation ?

While installing Ansible, sometimes you may encounter errors such as No package ‘libffi’ found or fatal error: Python.h: No such file or directory_These errors are generally caused by the missing packages which are dependencies of the packages required by Ansible.For example, _libffi package is dependency of pynacl and paramiko (Ansible -> paramiko -> pynacl -> libffi).

In order to solve these kinds of dependency issue, you may need to install required packages using the OS native package managers (e.g., yum, dnf or apt) or as mentioned in the package installation guide.

Please refer the documentation of the respective package for such dependencies and their installation methods.

Common Platform Issues

What customer platforms does Red Hat support?

A number of them! For a definitive list please see this Knowledge Base article.

Running in a virtualenv

You can install Ansible into a virtualenv on the controller quite simply:

  1. $ virtualenv ansible
  2. $ source ./ansible/bin/activate
  3. $ pip install ansible

If you want to run under Python 3 instead of Python 2 you may want to change that slightly:

  1. $ virtualenv -p python3 ansible
  2. $ source ./ansible/bin/activate
  3. $ pip install ansible

If you need to use any libraries which are not available via pip (for instance, SELinux Pythonbindings on systems such as Red Hat Enterprise Linux or Fedora that have SELinux enabled) then youneed to install them into the virtualenv. There are two methods:

  • When you create the virtualenv, specify —system-site-packages to make use of any librariesinstalled in the system’s Python:
  1. $ virtualenv ansible --system-site-packages
  • Copy those files in manually from the system. For instance, for SELinux bindings you might do:
  1. $ virtualenv ansible --system-site-packages
  2. $ cp -r -v /usr/lib64/python3.*/site-packages/selinux/ ./py3-ansible/lib64/python3.*/site-packages/
  3. $ cp -v /usr/lib64/python3.*/site-packages/*selinux*.so ./py3-ansible/lib64/python3.*/site-packages/

Running on BSD

See also

Ansible and BSD

Running on Solaris

By default, Solaris 10 and earlier run a non-POSIX shell which does not correctly expand the defaulttmp directory Ansible uses ( ~/.ansible/tmp). If you see module failures on Solaris machines, thisis likely the problem. There are several workarounds:

  • You can set remote_tmp to a path that will expand correctly with the shell you are using (see the plugin documentation for C shell, fish shell, and Powershell). Forexample, in the ansible config file you can set:
  1. remote_tmp=$HOME/.ansible/tmp

In Ansible 2.5 and later, you can also set it per-host in inventory like this:

  1. solaris1 ansible_remote_tmp=$HOME/.ansible/tmp
  • You can set ansible_shell_executable to the path to a POSIX compatible shell. Forinstance, many Solaris hosts have a POSIX shell located at /usr/xpg4/bin/sh so you can setthis in inventory like so:
  1. solaris1 ansible_shell_executable=/usr/xpg4/bin/sh

(bash, ksh, and zsh should also be POSIX compatible if you have any of those installed).

Running on z/OS

There are a few common errors that one might run into when trying to execute Ansible on z/OS as a target.

  • Version 2.7.6 of python for z/OS will not work with Ansible because it represents strings internally as EBCDIC.

To get around this limitation, download and install a later version of python for z/OS (2.7.13 or 3.6.1) that represents strings internally as ASCII. Version 2.7.13 is verified to work.

  • When pipelining = False in /etc/ansible/ansible.cfg then Ansible modules are transferred in binary mode via sftp however execution of python fails with

Error

SyntaxError: Non-UTF-8 code starting with ‘\x83’ in file /a/user1/.ansible/tmp/ansible-tmp-1548232945.35-274513842609025/AnsiballZ_stat.py on line 1, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

To fix it set pipelining = True in /etc/ansible/ansible.cfg.

  • Python interpret cannot be found in default location /usr/bin/python on target host.

Error

/usr/bin/python: EDC5129I No such file or directory

To fix this set the path to the python installation in your inventory like so:

  1. zos1 ansible_python_interpreter=/usr/lpp/python/python-2017-04-12-py27/python27/bin/python
  • Start of python fails with The module libpython2.7.so was not found.

Error

EE3501S The module libpython2.7.so was not found.

On z/OS, you must execute python from gnu bash. If gnu bash is installed at /usr/lpp/bash, you can fix this in your inventory by specifying an ansible_shell_executable:

  1. zos1 ansible_shell_executable=/usr/lpp/bash/bin/bash

What is the best way to make content reusable/redistributable?

If you have not done so already, read all about “Roles” in the playbooks documentation. This helps you make playbook contentself-contained, and works well with things like git submodules for sharing content with others.

If some of these plugin types look strange to you, see the API documentation for more details about ways Ansible can be extended.

Where does the configuration file live and what can I configure in it?

See Configuring Ansible.

How do I disable cowsay?

If cowsay is installed, Ansible takes it upon itself to make your day happier when running playbooks. If you decidethat you would like to work in a professional cow-free environment, you can either uninstall cowsay, set nocows=1 in ansible.cfg, or set the ANSIBLE_NOCOWS environment variable:

  1. export ANSIBLE_NOCOWS=1

How do I see a list of all of the ansible_ variables?

Ansible by default gathers “facts” about the machines under management, and these facts can be accessed in Playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the “setup” module as an ad-hoc action:

  1. ansible -m setup hostname

This will print out a dictionary of all of the facts that are available for that particular host. You might want to pipe the output to a pager.This does NOT include inventory variables or internal ‘magic’ variables. See the next question if you need more than just ‘facts’.

How do I see all the inventory variables defined for my host?

By running the following command, you can see inventory variables for a host:

  1. ansible-inventory --list --yaml

How do I see all the variables specific to my host?

To see all host specific variables, which might include facts and other sources:

  1. ansible -m debug -a "var=hostvars['hostname']" localhost

Unless you are using a fact cache, you normally need to use a play that gathers facts first, for facts included in the task above.

How do I loop over a list of hosts in a group, inside of a template?

A pretty common pattern is to iterate over a list of hosts inside of a host group, perhaps to populate a template configurationfile with a list of servers. To do this, you can just access the “$groups” dictionary in your template, like this:

  1. {% for host in groups['db_servers'] %}
  2. {{ host }}
  3. {% endfor %}

If you need to access facts about these hosts, for instance, the IP address of each hostname, you need to make sure that the facts have been populated. For example, make sure you have a play that talks to db_servers:

  1. - hosts: db_servers
  2. tasks:
  3. - debug: msg="doesn't matter what you do, just that they were talked to previously."

Then you can use the facts inside your template, like this:

  1. {% for host in groups['db_servers'] %}
  2. {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
  3. {% endfor %}

How do I access a variable name programmatically?

An example may come up where we need to get the ipv4 address of an arbitrary interface, where the interface to be used may be suppliedvia a role parameter or other input. Variable names can be built by adding strings together, like so:

  1. {{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }}

The trick about going through hostvars is necessary because it’s a dictionary of the entire namespace of variables. ‘inventory_hostname’is a magic variable that indicates the current host you are looping over in the host loop.

Also see dynamic_variables.

How do I access a group variable?

Technically, you don’t, Ansible does not really use groups directly. Groups are label for host selection and a way to bulk assign variables, they are not a first class entity, Ansible only cares about Hosts and Tasks.

That said, you could just access the variable by selecting a host that is part of that group, see first_host_in_a_group below for an example.

How do I access a variable of the first host in a group?

What happens if we want the ip address of the first webserver in the webservers group? Well, we can do that too. Note that if weare using dynamic inventory, which host is the ‘first’ may not be consistent, so you wouldn’t want to do this unless your inventoryis static and predictable. (If you are using Red Hat Ansible Tower, it will use database order, so this isn’t a problem even if you are using cloudbased inventory scripts).

Anyway, here’s the trick:

  1. {{ hostvars[groups['webservers'][0]]['ansible_eth0']['ipv4']['address'] }}

Notice how we’re pulling out the hostname of the first machine of the webservers group. If you are doing this in a template, youcould use the Jinja2 ‘#set’ directive to simplify this, or in a playbook, you could also use set_fact:

  1. - set_fact: headnode={{ groups[['webservers'][0]] }}
  2.  
  3. - debug: msg={{ hostvars[headnode].ansible_eth0.ipv4.address }}

Notice how we interchanged the bracket syntax for dots – that can be done anywhere.

How do I copy files recursively onto a target host?

The “copy” module has a recursive parameter. However, take a look at the “synchronize” module if you want to do something more efficient for a large number of files. The “synchronize” module wraps rsync. See the module index for info on both of these modules.

How do I access shell environment variables?

If you just need to access existing variables ON THE CONTROLLER, use the ‘env’ lookup plugin.For example, to access the value of the HOME environment variable on the management machine:

  1. ---
  2. # ...
  3. vars:
  4. local_home: "{{ lookup('env','HOME') }}"

For environment variables on the TARGET machines, they are available via facts in the ‘ansible_env’ variable:

  1. {{ ansible_env.SOME_VARIABLE }}

If you need to set environment variables for TASK execution, see Setting the Environment (and Working With Proxies) in the Advanced Playbooks section.There are several ways to set environment variables on your target machines. You can use the template, replace, or lineinfile modules to introduce environment variables into files.The exact files to edit vary depending on your OS and distribution and local configuration.

How do I generate encrypted passwords for the user module?

Ansible ad-hoc command is the easiest option:

  1. ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"

The mkpasswd utility that is available on most Linux systems is also a great option:

  1. mkpasswd --method=sha-512

If this utility is not installed on your system (e.g. you are using macOS) then you can still easilygenerate these passwords using Python. First, ensure that the Passlibpassword hashing library is installed:

  1. pip install passlib

Once the library is ready, SHA512 password values can then be generated as follows:

  1. python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"

Use the integrated Hashing filters to generate a hashed version of a password.You shouldn’t put plaintext passwords in your playbook or host_vars; instead, use Using Vault in playbooks to encrypt sensitive data.

In OpenBSD, a similar option is available in the base system called encrypt(1):

  1. encrypt

Ansible allows dot notation and array notation for variables. Which notation should I use?

The dot notation comes from Jinja and works fine for variables without specialcharacters. If your variable contains dots (.), colons (:), or dashes (-), ifa key begins and ends with two underscores, or if a key uses any of the knownpublic attributes, it is safer to use the array notation. See Using Variablesfor a list of the known public attributes.

  1. item[0]['checksum:md5']
  2. item['section']['2.1']
  3. item['region']['Mid-Atlantic']
  4. It is {{ temperature['Celsius']['-3'] }} outside.

Also array notation allows for dynamic variable composition, see dynamic_variables.

Another problem with ‘dot notation’ is that some keys can cause problems because they collide with attributes and methods of python dictionaries.

  1. item.update # this breaks if item is a dictionary, as 'update()' is a python method for dictionaries
  2. item['update'] # this works

When is it unsafe to bulk-set task arguments from a variable?

You can set all of a task’s arguments from a dictionary-typed variable. Thistechnique can be useful in some dynamic execution scenarios. However, itintroduces a security risk. We do not recommend it, so Ansible issues awarning when you do something like this:

  1. #...
  2. vars:
  3. usermod_args:
  4. name: testuser
  5. state: present
  6. update_password: always
  7. tasks:
  8. - user: '{{ usermod_args }}'

This particular example is safe. However, constructing tasks like this isrisky because the parameters and values passed to usermod_args couldbe overwritten by malicious values in the host facts on a compromisedtarget machine. To mitigate this risk:

  • set bulk variables at a level of precedence greater than host facts in the order of precedence found in Variable precedence: Where should I put a variable? (the example above is safe because play vars take precedence over facts)
  • disable the INJECT_FACTS_AS_VARS configuration setting to prevent fact values from colliding with variables (this will also disable the original warning)

Can I get training on Ansible?

Yes! See our services page for information on our services and training offerings. Email info@ansible.com for further details.

We also offer free web-based training classes on a regular basis. See our webinar page for more info on upcoming webinars.

Is there a web interface / REST API / etc?

Yes! Ansible, Inc makes a great product that makes Ansible even more powerful and easy to use. See Red Hat Ansible Tower.

How do I submit a change to the documentation?

Great question! Documentation for Ansible is kept in the main project git repository, and complete instructions for contributing can be found in the docs README viewable on GitHub. Thanks!

How do I keep secret data in my playbook?

If you would like to keep secret data in your Ansible content and still share it publicly or keep things in source control, see Using Vault in playbooks.

If you have a task that you don’t want to show the results or command given to it when using -v (verbose) mode, the following task or playbook attribute can be useful:

  1. - name: secret task
  2. shell: /usr/bin/do_something --value={{ secret_value }}
  3. no_log: True

This can be used to keep verbose output but hide sensitive information from others who would otherwise like to be able to see the output.

The no_log attribute can also apply to an entire play:

  1. - hosts: all
  2. no_log: True

Though this will make the play somewhat difficult to debug. It’s recommended that thisbe applied to single tasks only, once a playbook is completed. Note that the use of theno_log attribute does not prevent data from being shown when debugging Ansible itself viathe ANSIBLE_DEBUG environment variable.

When should I use {{ }}? Also, how to interpolate variables or dynamic variable names

A steadfast rule is ‘always use {{ }} except when when:’.Conditionals are always run through Jinja2 as to resolve the expression,so when:, failed_when: and changed_when: are always templated and you should avoid adding {{ }}.

In most other cases you should always use the brackets, even if previously you could use variables without specifying (like loop or with_ clauses), as this made it hard to distinguish between an undefined variable and a string.

Another rule is ‘moustaches don’t stack’. We often see this:

  1. {{ somevar_{{other_var}} }}

The above DOES NOT WORK as you expect, if you need to use a dynamic variable use the following as appropriate:

  1. {{ hostvars[inventory_hostname]['somevar_' + other_var] }}

For ‘non host vars’ you can use the vars lookup plugin:

  1. {{ lookup('vars', 'somevar_' + other_var) }}

Why don’t you ship in X format?

In most cases it has to do with maintainability. There are many ways to ship software and we do not have the resources to release Ansible on every platform.In some cases there are technical issues. For example, our dependencies are not present on Python Wheels.

How do I get the original ansible_host when I delegate a task?

As the documentation states, connection variables are taken from the delegate_to host so ansible_host is overwritten,but you can still access the original via hostvars:

  1. original_host: "{{ hostvars[inventory_hostname]['ansible_host'] }}"

This works for all overridden connection variables, like ansible_user, ansible_port, etc.

How do I fix ‘protocol error: filename does not match request’ when fetching a file?

Newer releases of OpenSSH have a bug in the SCP client that can trigger this error on the Ansible controller when using SCP as the file transfer mechanism:

  1. failed to transfer file to /tmp/ansible/file.txt\r\nprotocol error: filename does not match request

In these releases, SCP tries to validate that the path of the file to fetch matches the requested path.The validationfails if the remote filename requires quotes to escape spaces or non-ascii characters in its path. To avoid this error:

    • Use SFTP instead of SCP by setting scp_if_ssh to smart (which tries SFTP first) or to False. You can do this in one of four ways:
      • Rely on the default setting, which is smart - this works if scp_if_ssh is not explicitly set anywhere
      • Set a host variable or group variable in inventory: ansible_scp_if_ssh: False
      • Set an environment variable on your control node: export ANSIBLE_SCP_IF_SSH=False
      • Pass an environment variable when you run Ansible: ANSIBLE_SCP_IF_SSH=smart ansible-playbook
      • Modify your ansible.cfg file: add scp_if_ssh=False to the [ssh_connection] section
    • If you must use SCP, set the -T arg to tell the SCP client to ignore path validation. You can do this in one of three ways:
      • Set a host variable or group variable: ansible_scp_extra_args=-T,
      • Export or pass an environment variable: ANSIBLE_SCP_EXTRA_ARGS=-T
      • Modify your ansible.cfg file: add scp_extra_args=-T to the [ssh_connection] section

Note

If you see an invalid argument error when using -T, then your SCP client is not performing filename validation and will not trigger this error.

I don’t see my question here

Please see the section below for a link to IRC and the Google Group, where you can ask your question there.

See also