mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
419 lines
17 KiB
ReStructuredText
419 lines
17 KiB
ReStructuredText
12 years ago
|
Frequently Asked Questions
|
||
|
==========================
|
||
|
|
||
8 years ago
|
Here are some commonly asked questions and their answers.
|
||
12 years ago
|
|
||
9 years ago
|
|
||
10 years ago
|
.. _set_environment:
|
||
11 years ago
|
|
||
10 years ago
|
How can I set the PATH or any other environment variable for a task or entire playbook?
|
||
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
Setting environment variables can be done with the `environment` keyword. It can be used at the task or the play level::
|
||
10 years ago
|
|
||
|
environment:
|
||
10 years ago
|
PATH: "{{ ansible_env.PATH }}:/thingy/bin"
|
||
10 years ago
|
SOME: value
|
||
10 years ago
|
|
||
9 years ago
|
.. note:: starting in 2.0.1 the setup task from gather_facts 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.
|
||
10 years ago
|
|
||
|
|
||
12 years ago
|
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.
|
||
|
|
||
9 years ago
|
.. include:: ../rst_common/ansible_ssh_changes_note.rst
|
||
9 years ago
|
|
||
8 years ago
|
For instance, suppose these hosts have different usernames and ports:
|
||
|
|
||
|
.. code-block:: ini
|
||
12 years ago
|
|
||
|
[webservers]
|
||
9 years ago
|
asdf.example.com ansible_port=5000 ansible_user=alice
|
||
|
jkl.example.com ansible_port=5001 ansible_user=bob
|
||
12 years ago
|
|
||
8 years ago
|
You can also dictate the connection type to be used, if you want:
|
||
|
|
||
|
.. code-block:: ini
|
||
12 years ago
|
|
||
|
[testcluster]
|
||
|
localhost ansible_connection=local
|
||
|
/path/to/chroot1 ansible_connection=chroot
|
||
8 years ago
|
foo.example.com ansible_connection=paramiko
|
||
12 years ago
|
|
||
9 years ago
|
You may also wish to keep these in group variables instead, or file them in a group_vars/<groupname> file.
|
||
12 years ago
|
See the rest of the documentation for more information about how to organize variables.
|
||
|
|
||
11 years ago
|
.. _use_ssh:
|
||
12 years ago
|
|
||
|
How do I get ansible to reuse connections, enable Kerberized SSH, or have Ansible pay attention to my local SSH config file?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
11 years ago
|
Switch your default connection type in the configuration file to 'ssh', or use '-c ssh' to use
|
||
12 years ago
|
Native OpenSSH for connections instead of the python paramiko library. In Ansible 1.2.1 and later, 'ssh' will be used
|
||
|
by default if OpenSSH is new enough to support ControlPersist as an option.
|
||
12 years ago
|
|
||
|
Paramiko is great for starting out, but the OpenSSH type offers many advanced options. You will want to run Ansible
|
||
|
from a machine new enough to support ControlPersist, if you are using this connection type. You can still manage
|
||
7 years ago
|
older clients. If you are using RHEL 6, CentOS 6, SLES 10 or SLES 11 the version of OpenSSH is still a bit old, so
|
||
12 years ago
|
consider managing from a Fedora or openSUSE client even though you are managing older nodes, or just use paramiko.
|
||
12 years ago
|
|
||
|
We keep paramiko as the default as if you are first installing Ansible on an EL box, it offers a better experience
|
||
|
for new users.
|
||
|
|
||
9 years ago
|
.. _use_ssh_jump_hosts:
|
||
|
|
||
|
How do I configure a jump host to access servers that I have no direct access to?
|
||
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
7 years ago
|
You can set a `ProxyCommand` in the
|
||
9 years ago
|
`ansible_ssh_common_args` inventory variable. Any arguments specified in
|
||
|
this variable are added to the sftp/scp/ssh command line when connecting
|
||
8 years ago
|
to the relevant host(s). Consider the following inventory group:
|
||
|
|
||
|
.. code-block:: ini
|
||
9 years ago
|
|
||
|
[gatewayed]
|
||
9 years ago
|
foo ansible_host=192.0.2.1
|
||
|
bar ansible_host=192.0.2.2
|
||
9 years ago
|
|
||
9 years ago
|
You can create `group_vars/gatewayed.yml` with the following contents::
|
||
9 years ago
|
|
||
9 years ago
|
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user@gateway.example.com"'
|
||
9 years ago
|
|
||
9 years ago
|
Ansible will append these arguments to the command line when trying to
|
||
|
connect to any hosts in the group `gatewayed`. (These arguments are used
|
||
|
in addition to any `ssh_args` from `ansible.cfg`, so you do not need to
|
||
|
repeat global `ControlPersist` settings in `ansible_ssh_common_args`.)
|
||
9 years ago
|
|
||
|
Note that `ssh -W` is available only with OpenSSH 5.4 or later. With
|
||
|
older versions, it's necessary to execute `nc %h:%p` or some equivalent
|
||
|
command on the bastion host.
|
||
|
|
||
|
With earlier versions of Ansible, it was necessary to configure a
|
||
|
suitable `ProxyCommand` for one or more hosts in `~/.ssh/config`,
|
||
|
or globally by setting `ssh_args` in `ansible.cfg`.
|
||
|
|
||
11 years ago
|
.. _ec2_cloud_performance:
|
||
|
|
||
12 years ago
|
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 first
|
||
|
and run Ansible from there.
|
||
|
|
||
11 years ago
|
.. _python_interpreters:
|
||
|
|
||
12 years ago
|
How do I handle python pathing not having a Python 2.X in /usr/bin/python on a remote machine?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
|
While you can write ansible modules in any language, most ansible modules are written in Python, and some of these
|
||
|
are important core ones.
|
||
|
|
||
8 years ago
|
By default, Ansible assumes it can find a /usr/bin/python on your remote system that is a 2.X version of Python, specifically
|
||
8 years ago
|
2.6 or higher.
|
||
12 years ago
|
|
||
8 years ago
|
Setting the inventory variable 'ansible_python_interpreter' on any host will allow Ansible to auto-replace the interpreter
|
||
12 years ago
|
used when executing python modules. Thus, you can point to any python you want on the system if /usr/bin/python on your
|
||
7 years ago
|
system does not point to a Python 2.X interpreter.
|
||
12 years ago
|
|
||
|
Some Linux operating systems, such as Arch, may only have Python 3 installed by default. This is not sufficient and you will
|
||
8 years ago
|
get syntax errors trying to run modules with Python 3. Python 3 is essentially not the same language as Python 2. Python 3
|
||
|
support is being worked on but some Ansible modules are not yet ported to run under Python 3.0. This is not a problem though
|
||
|
as you can just install Python 2 also on a managed host.
|
||
12 years ago
|
|
||
|
Do not replace the shebang lines of your python modules. Ansible will do this for you automatically at deploy time.
|
||
|
|
||
11 years ago
|
.. _use_roles:
|
||
|
|
||
12 years ago
|
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 content
|
||
11 years ago
|
self-contained, and works well with things like git submodules for sharing content with others.
|
||
12 years ago
|
|
||
|
If some of these plugin types look strange to you, see the API documentation for more details about ways Ansible can be extended.
|
||
|
|
||
11 years ago
|
.. _configuration_file:
|
||
|
|
||
12 years ago
|
Where does the configuration file live and what can I configure in it?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
|
|
||
7 years ago
|
See :doc:`../installation_guide/intro_configuration`.
|
||
12 years ago
|
|
||
11 years ago
|
.. _who_would_ever_want_to_disable_cowsay_but_ok_here_is_how:
|
||
12 years ago
|
|
||
|
How do I disable cowsay?
|
||
|
++++++++++++++++++++++++
|
||
|
|
||
|
If cowsay is installed, Ansible takes it upon itself to make your day happier when running playbooks. If you decide
|
||
7 years ago
|
that you would like to work in a professional cow-free environment, you can either uninstall cowsay, or set the :envvar:`ANSIBLE_NOCOWS` environment variable:
|
||
12 years ago
|
|
||
8 years ago
|
.. code-block:: shell-session
|
||
|
|
||
12 years ago
|
export ANSIBLE_NOCOWS=1
|
||
|
|
||
11 years ago
|
.. _browse_facts:
|
||
|
|
||
12 years ago
|
How do I see a list of all of the ansible\_ variables?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
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:
|
||
|
|
||
|
.. code-block:: shell-session
|
||
12 years ago
|
|
||
|
ansible -m setup hostname
|
||
|
|
||
9 years ago
|
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.
|
||
|
|
||
|
.. _browse_inventory_vars:
|
||
9 years ago
|
|
||
9 years ago
|
How do I see all the inventory vars defined for my host?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
By running the following command, you can see vars resulting from what you've defined in the inventory:
|
||
8 years ago
|
|
||
|
.. code-block:: shell-session
|
||
9 years ago
|
|
||
|
ansible -m debug -a "var=hostvars['hostname']" localhost
|
||
12 years ago
|
|
||
11 years ago
|
.. _host_loops:
|
||
|
|
||
12 years ago
|
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 configuration
|
||
9 years ago
|
file with a list of servers. To do this, you can just access the "$groups" dictionary in your template, like this:
|
||
|
|
||
9 years ago
|
.. code-block:: jinja
|
||
12 years ago
|
|
||
|
{% for host in groups['db_servers'] %}
|
||
|
{{ host }}
|
||
|
{% 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::
|
||
|
|
||
|
- hosts: db_servers
|
||
|
tasks:
|
||
9 years ago
|
- debug: msg="doesn't matter what you do, just that they were talked to previously."
|
||
12 years ago
|
|
||
8 years ago
|
Then you can use the facts inside your template, like this:
|
||
|
|
||
|
.. code-block:: jinja
|
||
12 years ago
|
|
||
|
{% for host in groups['db_servers'] %}
|
||
|
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
|
||
|
{% endfor %}
|
||
|
|
||
11 years ago
|
.. _programatic_access_to_a_variable:
|
||
11 years ago
|
|
||
11 years ago
|
How do I access a variable name programmatically?
|
||
|
+++++++++++++++++++++++++++++++++++++++++++++++++
|
||
11 years ago
|
|
||
|
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 supplied
|
||
8 years ago
|
via a role parameter or other input. Variable names can be built by adding strings together, like so:
|
||
|
|
||
|
.. code-block:: jinja
|
||
11 years ago
|
|
||
|
{{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }}
|
||
|
|
||
11 years ago
|
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.
|
||
11 years ago
|
|
||
11 years ago
|
.. _first_host_in_a_group:
|
||
11 years ago
|
|
||
|
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 we
|
||
|
are using dynamic inventory, which host is the 'first' may not be consistent, so you wouldn't want to do this unless your inventory
|
||
7 years ago
|
is static and predictable. (If you are using :doc:`../reference_appendices/tower`, it will use database order, so this isn't a problem even if you are using cloud
|
||
11 years ago
|
based inventory scripts).
|
||
|
|
||
8 years ago
|
Anyway, here's the trick:
|
||
|
|
||
|
.. code-block:: jinja
|
||
11 years ago
|
|
||
|
{{ 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, you
|
||
9 years ago
|
could use the Jinja2 '#set' directive to simplify this, or in a playbook, you could also use set_fact::
|
||
11 years ago
|
|
||
|
- set_fact: headnode={{ groups[['webservers'][0]] }}
|
||
9 years ago
|
|
||
11 years ago
|
- debug: msg={{ hostvars[headnode].ansible_eth0.ipv4.address }}
|
||
|
|
||
|
Notice how we interchanged the bracket syntax for dots -- that can be done anywhere.
|
||
|
|
||
11 years ago
|
.. _file_recursion:
|
||
|
|
||
12 years ago
|
How do I copy files recursively onto a target host?
|
||
|
+++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
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.
|
||
12 years ago
|
|
||
11 years ago
|
.. _shell_env:
|
||
|
|
||
12 years ago
|
How do I access shell environment variables?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
|
If you just need to access existing variables, use the 'env' lookup plugin. For example, to access the value of the HOME
|
||
8 years ago
|
environment variable on the management machine::
|
||
12 years ago
|
|
||
|
---
|
||
|
# ...
|
||
|
vars:
|
||
|
local_home: "{{ lookup('env','HOME') }}"
|
||
|
|
||
|
If you need to set environment variables, see the Advanced Playbooks section about environments.
|
||
|
|
||
7 years ago
|
Remote environment variables are available via facts in the 'ansible_env' variable:
|
||
8 years ago
|
|
||
|
.. code-block:: jinja
|
||
11 years ago
|
|
||
|
{{ ansible_env.SOME_VARIABLE }}
|
||
|
|
||
11 years ago
|
.. _user_passwords:
|
||
|
|
||
|
How do I generate crypted passwords for the user module?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
The mkpasswd utility that is available on most Linux systems is a great option:
|
||
|
|
||
|
.. code-block:: shell-session
|
||
11 years ago
|
|
||
8 years ago
|
mkpasswd --method=sha-512
|
||
11 years ago
|
|
||
|
If this utility is not installed on your system (e.g. you are using OS X) then you can still easily
|
||
8 years ago
|
generate these passwords using Python. First, ensure that the `Passlib <https://bitbucket.org/ecollins/passlib/wiki/Home>`_
|
||
8 years ago
|
password hashing library is installed:
|
||
|
|
||
|
.. code-block:: shell-session
|
||
11 years ago
|
|
||
|
pip install passlib
|
||
|
|
||
8 years ago
|
Once the library is ready, SHA512 password values can then be generated as follows:
|
||
|
|
||
|
.. code-block:: shell-session
|
||
11 years ago
|
|
||
7 years ago
|
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
|
||
11 years ago
|
|
||
8 years ago
|
Use the integrated :ref:`hash_filters` to generate a hashed version of a password.
|
||
7 years ago
|
You shouldn't put plaintext passwords in your playbook or host_vars; instead, use :doc:`../user_guide/playbooks_vault` to encrypt sensitive data.
|
||
8 years ago
|
|
||
11 years ago
|
.. _commercial_support:
|
||
|
|
||
7 years ago
|
Ansible supports dot notation and array notation for variables. Which notation should I use?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
|
The dot notation comes from Jinja and works fine for variables without special
|
||
|
characters. If your variable contains dots (.), colons (:), or dashes (-) it is
|
||
|
safer to use the array notation for variables.
|
||
|
|
||
|
.. code-block:: jinja
|
||
|
|
||
|
item[0]['checksum:md5']
|
||
|
item['section']['2.1']
|
||
|
item['region']['Mid-Atlantic']
|
||
|
It is {{ temperature['Celsius']['-3'] }} outside.
|
||
|
|
||
8 years ago
|
Can I get training on Ansible?
|
||
|
++++++++++++++++++++++++++++++
|
||
12 years ago
|
|
||
8 years ago
|
Yes! See our `services page <https://www.ansible.com/consulting>`_ for information on our services and training offerings. Email `info@ansible.com <mailto:info@ansible.com>`_ for further details.
|
||
9 years ago
|
|
||
8 years ago
|
We also offer free web-based training classes on a regular basis. See our `webinar page <https://www.ansible.com/webinars-training>`_ for more info on upcoming webinars.
|
||
12 years ago
|
|
||
11 years ago
|
.. _web_interface:
|
||
|
|
||
|
Is there a web interface / REST API / etc?
|
||
|
++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
11 years ago
|
Yes! Ansible, Inc makes a great product that makes Ansible even more powerful
|
||
7 years ago
|
and easy to use. See :doc:`../reference_appendices/tower`.
|
||
11 years ago
|
|
||
|
.. _docs_contributions:
|
||
|
|
||
12 years ago
|
How do I submit a change to the documentation?
|
||
|
++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
8 years ago
|
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 <https://github.com/ansible/ansible/blob/devel/docs/docsite/README.md>`_. Thanks!
|
||
12 years ago
|
|
||
11 years ago
|
.. _keep_secret_data:
|
||
|
|
||
|
How do I keep secret data in my playbook?
|
||
|
+++++++++++++++++++++++++++++++++++++++++
|
||
|
|
||
7 years ago
|
If you would like to keep secret data in your Ansible content and still share it publicly or keep things in source control, see :doc:`../user_guide/playbooks_vault`.
|
||
11 years ago
|
|
||
7 years ago
|
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::
|
||
10 years ago
|
|
||
|
- name: secret task
|
||
|
shell: /usr/bin/do_something --value={{ secret_value }}
|
||
|
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::
|
||
|
|
||
|
- hosts: all
|
||
|
no_log: True
|
||
|
|
||
|
Though this will make the play somewhat difficult to debug. It's recommended that this
|
||
9 years ago
|
be applied to single tasks only, once a playbook is completed. Note that the use of the
|
||
|
no_log attribute does not prevent data from being shown when debugging Ansible itself via
|
||
7 years ago
|
the :envvar:`ANSIBLE_DEBUG` environment variable.
|
||
9 years ago
|
|
||
|
|
||
|
.. _when_to_use_brackets:
|
||
|
.. _dynamic_variables:
|
||
|
.. _interpolate_variables:
|
||
|
|
||
9 years ago
|
When should I use {{ }}? Also, how to interpolate variables or dynamic variable names
|
||
9 years ago
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
9 years ago
|
|
||
7 years ago
|
A steadfast rule is 'always use ``{{ }}`` except when ``when:``'.
|
||
9 years ago
|
Conditionals are always run through Jinja2 as to resolve the expression,
|
||
7 years ago
|
so ``when:``, ``failed_when:`` and ``changed_when:`` are always templated and you should avoid adding ``{{ }}``.
|
||
9 years ago
|
|
||
7 years ago
|
In most other cases you should always use the brackets, even if previously you could use variables without specifying (like ``loop`` or ``with_`` clauses),
|
||
9 years ago
|
as this made it hard to distinguish between an undefined variable and a string.
|
||
|
|
||
8 years ago
|
Another rule is 'moustaches don't stack'. We often see this:
|
||
|
|
||
|
.. code-block:: jinja
|
||
9 years ago
|
|
||
9 years ago
|
{{ somevar_{{other_var}} }}
|
||
9 years ago
|
|
||
8 years ago
|
The above DOES NOT WORK, if you need to use a dynamic variable use the hostvars or vars dictionary as appropriate:
|
||
|
|
||
|
.. code-block:: jinja
|
||
9 years ago
|
|
||
9 years ago
|
{{ hostvars[inventory_hostname]['somevar_' + other_var] }}
|
||
10 years ago
|
|
||
8 years ago
|
Why don't you ship in X format?
|
||
|
+++++++++++++++++++++++++++++++
|
||
|
|
||
|
Several reasons, in most cases it has to do with maintainability, there are tons of ways to ship software and it is a herculean task to try to support them all.
|
||
8 years ago
|
In other cases there are technical issues, for example, for python wheels, our dependencies are not present so there is little to no gain.
|
||
8 years ago
|
|
||
7 years ago
|
.. _i_dont_see_my_question:
|
||
8 years ago
|
|
||
12 years ago
|
I don't see my question here
|
||
|
++++++++++++++++++++++++++++
|
||
|
|
||
11 years ago
|
Please see the section below for a link to IRC and the Google Group, where you can ask your question there.
|
||
12 years ago
|
|
||
11 years ago
|
.. seealso::
|
||
|
|
||
7 years ago
|
:doc:`../user_guide/playbooks`
|
||
11 years ago
|
An introduction to playbooks
|
||
7 years ago
|
:doc:`../user_guide/playbooks_best_practices`
|
||
11 years ago
|
Best practices advice
|
||
11 years ago
|
`User Mailing List <http://groups.google.com/group/ansible-project>`_
|
||
11 years ago
|
Have a question? Stop by the google group!
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|