ansible cheat sheet


Command
ansible-playbook file.yamlRun an Ansible playbook called file.yaml

Authentication options
–user, -u <username>Log in as username
–private-key, –key-file <key>Log in using SSH key (usually in ~/.ssh)
–ssh-extra-argsPass extra command options to SSH
–vault-id <id>Use vault identity ID
–vault-password-file <key>Use vault password file key
–ask-vault-passPrompt for a vault password
–becomeEscalate privileges
–ask-become-passPrompt for a password for become
–become-methodEscalate privilege using a specific method
ansible-doc –-type foo –listList choices for become, connection, and other Ansible options

Control options
–syntax-checkVerify syntax of a playbook, but do not run it
–list-hostsShow hosts listed in a playbook
–list-tasksShow tasks defined in a playbook
–start-at-task <task_name>Run playbook starting at task name
–checkRun the playbook but don’t make changes
–diffShow diffs for what changes are made
–module-pathPrepend colon-separated path to default path
–connection <method>Connect over method

Playbook and YAML
Parameter: valueA YAML mapping entry is a key and a value
– fooA YAML sequence entry is an itemized list
– bar
– baz
Distro:A mapping entry can contain a sequence
– Fedora
– RHEL
– Debian
– Slackware
OS:Sequence items can contain mappings
– Linux: Fedora
– BSD: NetBSD

Playbook structure
YAML files start with three dashes
– name: “My play”Use the name mapping to name your play
hosts: allIndent, and define which hosts the play runs on. List target hosts in etc/ansible/hosts
tasks:Open a tasks mapping, which will contain a sequence
– name:“My task” Give the task a name with the name mapping
some_module:Import a module as a new mapping containing a sequence of parameters. Find required and optional parameters in the module’s documentation.
path: ‘/example/’Parameters are usually mappings using the command option as the key and an argument as the value.
– name: “My other task”A play may contain more than one task
other_module:A task usually imports a module
foo: ‘bar’