Ansible Commands Cheat Sheet



How to use ansible

Ansible Cheat Sheet. Posted on July 6, 2016 July 4, 2019 by admin. Execute arbitrary commands. Ansible -a command ansible all -a 'ifconfig -a'. Ping specific node ansible -i hosts nycweb01.prod.local -m ping Ping with wildcard ansible -i hosts 'nycweb.' -m ping Ping all nodes with SSH user 'root' ansible -i hosts all -m ping -u root run a command ansible -i hosts dev -a 'uname -a' check Yum packages ansible -i hosts dev -m yum check if Docker rpm is installed ansible -i hosts web01.nyc. Arbitrary commands can be run against hosts or groups of hosts at the command line, one at a time. In order to list the contents of the home directory for the ansible user, for example:ansible GROUPNAME -a “ls -al /home/ansible”. Ansible AD HOC Command Examples – Ansible Cheat Sheet Updated on: February 1, 2021 Sarav AK Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact swiss army knife when you want to do a quick task across multiple machines.

Welcome to the Ansible style guide!To create clear, concise, consistent, useful materials on docs.ansible.com, follow these guidelines:

We want the Ansible documentation to be:

  • clear

  • direct

  • conversational

  • easy to translate

We want reading the docs to feel like having an experienced, friendly colleagueexplain how Ansible works.

This cheat-sheet illustrates a few rules that help achieve the “Ansible tone”:

Rule

Good example

Bad example

Use active voice

You can run a task by

A task can be run by

Use the present tense

This command creates a

This command will create a

Address the reader

As you expand your inventory

When the number of managed nodes grows

Use standard English

Return to this page

Hop back to this page

Use American English

The color of the output

The colour of the output

Headers should be written in sentence case. For example, this section’s title isHeadercase, not HeaderCase or HEADERCASE.

Latin words and phrases like e.g. or etc.are easily understood by English speakers.They may be harder to understand for others and are also tricky for automated translation.

Ansible Commands Cheat Sheet Download

Use the following English terms in place of Latin terms or abbreviations:

Latin

English

i.e

in other words

e.g.

for example

etc

and so on

via

by/ through

vs./versus

rather than/against

The Ansible documentation is written in reStructuredText and processed by Sphinx.We follow these technical or mechanical guidelines on all rST pages:

Section headers in reStructuredTextcan use a variety of notations.Sphinx will ‘learn on the fly’ when creating a hierarchy of headers.To make our documents easy to read and to edit, we follow a standard set of header notations.We use:

  • ### with overline, for parts:

  • *** with overline, for chapters:

  • for sections:

  • --- for subsections:

  • ^^^ for sub-subsections:

  • '' for paragraphs:

Anchors (also called labels) and linkswork together to help users find related content.Local tables of contents also help users navigate quickly to the information they need.All internal links should use the :ref: syntax.Every page should have at least one anchor to support internal :ref: links.Long pages, or pages with multiple levels of headers, can also include a local TOC.

  • Include at least one anchor on every page

  • Place the main anchor above the main header

  • If the file has a unique title, use that for the main page anchor:

  • You may also add anchors elsewhere on the page

  • All internal links must use :ref: syntax. These links both point to the anchor defined above:

The second example adds custom text for the link.

Ansible Commands Cheat Sheet

Ansible 2.10 and later require the extended Fully Qualified Collection Name (FQCN) as part of the links:

For example:

displays as ansible.builtin.first_found lookup plugin.

Modules require different suffixes from other plugins:

  • Module links use this extended FQCN module name with _module for the anchor.

  • Plugin links use this extended FQCN plugin name with the plugin type (_connection for example).

Note

ansible.builtin is the FQCN for modules included in ansible.base. Documentation links are the only place you prepend ansible_collections to the FQCN. This is used by the documentation build scripts to correctly fetch documentation from collections on Ansible Galaxy.

The page you’re reading includes a local TOC.If you include a local TOC:

  • place it below, not above, the main heading and (optionally) introductory text

  • use the :local: directive so the page’s main header is not included

  • do not include a title

The syntax is:

These pages offer more help with grammatical, stylistic, and technical rules for documentation.

See also

Contributing to the Ansible Documentation

How to contribute to the Ansible documentation

Testing the documentation locally

How to build the Ansible documentation

irc.freenode.net

#ansible-docs IRC chat channel

Linux

Packages:

  • git
  • python
  • python-devel
  • python-pip
  • openssl
  • ansible

Linux

Be sure to install epel-release first and then update your caches (if CentOS). On Ubuntu/Debian distributions, you may install from the default repositories. Assuming CentOS, as in our course, do the following:

sudo yum install git python python-devel python-pip openssl ansible

User Accounts

Create a user called ansible (example) on the server you intend to use Ansible to run playbooks from AND each of the Ansible nodes you intend to run playbooks on. Set the user as a sudo-capable user and include the NOPASSWD: ALLdirective in /etc/sudoers.

Create an SSH key with ssh-keygen on the Ansible server. Exchange that key using ssh-copy-id on each of the nodes you are running playbooks on. This allows the playbook to run with escalated privileges as needed.

Configuration Files

  • /etc/ansible/ansible.cfg
    • Primary Ansible configuration file (agentless, daemon-less configuration, read on each ansible command run)
    • Uncomment “inventory” field
    • Uncomment “become user” field
  • /etc/ansible/hosts
    • Copy original to /etc/ansible/hosts.original
    • Create one or more sections with group names, sample below
2
4
6
8
10
12
14
16
18
20
22
- hosts: hostsToRunAgainst
become: yes
connection: ssh
vars:
var2: value
- name: Some description of what we are doing
name: httpd
notify:
handlers:
service:
state: restarted

ansible-playbook

  • Calling a playbook ansible-playbook /path/to/playbook.yaml

Inventory

  • /etc/ansible/hosts
    • Defines nodes/groups of nodes to operate against
  • $ANSIBLE_HOSTS
    • Shell variable containing one or more ansible hosts