Ansible is a modern configuration management tool that facilitates the task of setting up and maintaining remote servers.
This cheat sheet-style guide provides a quick reference to commands and practices commonly used when working with Ansible. For an overview of Ansible and how to install and configure it, please check our guide on How to Install and Configure Ansible on Ubuntu 20.04.
How to Use This Guide:
highlighted textin this guide’s commands, keep in mind that this text should refer to hosts, usernames and IP addresses from your own inventory.
The following Ansible-specific terms are largely used throughout this guide:
If you’d like an example of these commands in practice, see our guide on How to Use Ansible to Automate Initial Server Setup on Ubuntu 20.04. You’ll need at least one remote server to use as a node.
To test that Ansible is able to connect and run commands and playbooks on your nodes, you can use the following command:
- ansible all -m ping
ping module will test if you have valid credentials for connecting to the nodes defined in your inventory file, in addition to testing if Ansible is able to run Python scripts on the remote server. A pong reply back means Ansible is ready to run commands and playbooks on that node.
By default, Ansible tries to connect to the nodes as your current system user, using its corresponding SSH keypair. To connect as a different user, append the command with the
-u flag and the name of the intended user:
- ansible all -m ping -u sammy
The same is valid for
- ansible-playbook myplaybook.yml -u sammy
If you’re using a custom SSH key to connect to the remote servers, you can provide it at execution time with the
- ansible all -m ping --private-key=~/.ssh/custom_id
This option is also valid for
- ansible-playbook myplaybook.yml --private-key=~/.ssh/custom_id
If you need to use password-based authentication in order to connect to the nodes, you need to append the option
--ask-pass to your Ansible command.
This will make Ansible prompt you for the password of the user on the remote server that you’re attempting to connect as:
- ansible all -m ping --ask-pass
This option is also valid for
- ansible-playbook myplaybook.yml --ask-pass
If the remote user needs to provide a password in order to run
sudo commands, you can include the option
--ask-become-pass to your Ansible command. This will prompt you to provide the remote user sudo password:
- ansible all -m ping --ask-become-pass
This option is also valid for
- ansible-playbook myplaybook.yml --ask-become-pass
The default inventory file is typically located at
/etc/ansible/hosts, but you can also use the
-i option to point to custom inventory files when running Ansible commands and playbooks. Ansible also supports inventory scripts for building dynamic inventory files, for when your inventory fluctuates, with servers being created and destroyed often. Custom inventory files are useful for setting up per-project inventories that can be included in version control systems such as Git:
- ansible all -m ping -i my_custom_inventory
The same option is valid for
- ansible-playbook myplaybook.yml -i my_custom_inventory
To execute a command on a node, use the
-a option followed by the command you want to run, in quotes.
This will execute
uname -a on all the nodes in your inventory:
- ansible all -a "uname -a"
It is also possible to run Ansible modules with the option
-m. The following command would install the package
server1 from your inventory:
- ansible server1 -m apt -a "name=vim"
Before making changes to your nodes, you can conduct a dry run to predict how the servers would be affected by your command. This can be done by including the
- ansible server1 -m apt -a "name=vim" --check
To run a playbook and execute all the tasks defined within it, use the
- ansible-playbook myplaybook.yml
To overwrite the default
hosts option in the playbook and limit execution to a certain group or host, include the option
-l in your command:
- ansible-playbook -l server1 myplaybook.yml
--list-tasks is used to list all tasks that would be executed by a play without making any changes to the remote servers:
- ansible-playbook myplaybook.yml --list-tasks
Similarly, it is possible to list all hosts that would be affected by a play, without running any tasks on the remote servers:
- ansible-playbook myplaybook.yml --list-hosts
You can use tags to limit the execution of a play. To list all tags available in a play, use the option
- ansible-playbook myplaybook.yml --list-tags
You can use the option
--start-at-task to define a new entry point for your playbook. Ansible will then skip anything that comes before the specified task, executing the remaining of the play from that point on. This option requires a valid task name as argument:
- ansible-playbook myplaybook.yml --start-at-task="Set Up Nginx"
To only execute tasks associated with specific tags, you can use the option
--tags. For instance, if you’d like to only execute tasks tagged as
mysql, you can use:
- ansible-playbook myplaybook.yml --tags=mysql,nginx
If you want to skip all tasks that are under specific tags, use
--skip-tags. The following command would execute
myplaybook.yml, skipping all tasks tagged as
- ansible-playbook myplaybook.yml --skip-tags=mysql
If your Ansible playbooks deal with sensitive data like passwords, API keys, and credentials, it is important to keep that data safe by using an encryption mechanism. Ansible provides
ansible-vault to encrypt files and variables.
Even though it is possible to encrypt any Ansible data file as well as binary files, it is more common to use
ansible-vault to encrypt variable files containing sensitive data. After encrypting a file with this tool, you’ll only be able to execute, edit or view its contents by providing the relevant password defined when you first encrypted the file.
You can create a new encrypted Ansible file with:
- ansible-vault create credentials.yml
This command will perform the following actions:
ansible-vaultwill save the file as encrypted data.
To encrypt an existing Ansible file, you can use the following syntax:
- ansible-vault encrypt credentials.yml
This will prompt you for a password that you’ll need to enter whenever you access the file
If you want to view the contents of a file that was previously encrypted with
ansible-vault and you don’t need to change its contents, you can use:
- ansible-vault view credentials.yml
This will prompt you to provide the password you selected when you first encrypted the file with
To edit the contents of a file that was previously encrypted with Ansible Vault, run:
- ansible-vault edit credentials.yml
This will prompt you to provide the password you chose when first encrypting the file
ansible-vault. After password validation, your default command-line editor will open with the unencrypted contents of the file, allowing you to make your changes. When finished, you can save and close the file as you would normally, and the updated contents will be saved as encrypted data.
If you wish to permanently revert a file that was previously encrypted with
ansible-vault to its unencrypted version, you can do so with this syntax:
- ansible-vault decrypt credentials.yml
This will prompt you to provide the same password used when first encrypting the file
ansible-vault. After password validation, the file contents will be saved to the disk as unencrypted data.
Ansible supports multiple vault passwords grouped by different vault IDs. This is useful if you want to have dedicated vault passwords for different environments, such as development, testing, and production environments.
To create a new encrypted file using a custom vault ID, include the
--vault-id option along with a label and the location where
ansible-vault can find the password for that vault. The label can be any identifier, and the location can either be
prompt, meaning that the command should prompt you to enter a password, or a valid path to a password file.
- ansible-vault create --vault-id dev@prompt credentials_dev.yml
This will create a new vault ID named dev that uses
prompt as password source. By combining this method with group variable files, you’ll be able to have separate ansible vaults for each application environment:
- ansible-vault create --vault-id prod@prompt credentials_prod.yml
We used dev and prod as vault IDs to demonstrate how you can create separate vaults per environment, but you can create as many vaults as you want, and you can use any identifier of your choice as vault ID.
Now to view, edit, or decrypt these files, you’ll need to provide the same vault ID and password source along with the
- ansible-vault edit credentials_dev.yml --vault-id dev@prompt
If you need to automate the process of provisioning servers with Ansible using a third-party tool, you’ll need a way to provide the vault password without being prompted for it. You can do that by using a password file with
A password file can be a plain text file or an executable script. If the file is an executable script, the output produced by this script will be used as the vault password. Otherwise, the raw contents of the file will be used as vault password.
To use a password file with
ansible-vault, you need to provide the path to a password file when running any of the vault commands:
- ansible-vault create --vault-id dev@path/to/passfile credentials_dev.yml
Ansible doesn’t make a distinction between content that was encrypted using
prompt or a password file as password source, as long as the input password is the same. In practical terms, this means it is OK to encrypt a file using
prompt and then later use a password file to store the same password used with the
prompt method. The opposite is also true: you can encrypt content using a password file and later use the
prompt method, providing the same password when prompted by Ansible.
For extended flexibility and security, instead of having your vault password stored in a plain text file, you can use a Python script to obtain the password from other sources. The official Ansible repository contains a few examples of vault scripts that you can use for reference when creating a custom script that suits the particular needs of your project.
Whenever you run a playbook that uses data previously encrypted via
ansible-vault, you’ll need to provide the vault password to your playbook command.
If you used default options and the
prompt password source when encrypting the data used in this playbook, you can use the option
--ask-vault-pass to make Ansible prompt you for the password:
- ansible-playbook myplaybook.yml --ask-vault-pass
If you used a password file instead of prompting for the password, you should use the option
- ansible-playbook myplaybook.yml --vault-password-file my_vault_password.py
If you’re using data encrypted under a vault ID, you’ll need to provide the same vault ID and password source you used when first encrypting the data:
- ansible-playbook myplaybook.yml --vault-id dev@prompt
If using a password file with your vault ID, you should provide the label followed by the full path to the password file as password source:
- ansible-playbook myplaybook.yml --vault-id dev@vault_password.py
If your play uses multiple vaults, you should provide a
--vault-id parameter for each of them, in no particular order:
- ansible-playbook myplaybook.yml --vault-id dev@vault_password.py --vault-id test@prompt --vault-id ci@prompt
If you run into errors while executing Ansible commands and playbooks, it’s a good idea to increase output verbosity in order to get more information about the problem. You can do that by including the
-v option to the command:
- ansible-playbook myplaybook.yml -v
If you need more detail, you can use
-vvv and this will increase verbosity of the output. If you’re unable to connect to the remote nodes via Ansible, use
-vvvv to get connection debugging information:
- ansible-playbook myplaybook.yml -vvvv
This guide covers some of the most common Ansible commands you may use when provisioning servers, such as how to execute remote commands on your nodes and how to run playbooks using a variety of custom settings.
There are other command variations and flags that you may find useful for your Ansible workflow. To get an overview of all available options, you can use the help command:
- ansible --help
If you want a more comprehensive view of Ansible and all its available commands and features, please refer to the official Ansible documentation.
If you want to see another practical example of Ansible, check out our guide on How to Use Ansible to Install and Set Up Docker on Ubuntu 20.04.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.