Tested with Ansible 15 (ansible-core 2.22), and most commands work on older versions too. Ansible runs on Linux and macOS. On Windows, run it in WSL. Inventory, group and host names are examples, so swap in your own. With an inventory set in ansible.cfg, you can leave out -i.

Install and set up #

  • Install Ansible with pipxLinux, macOS
    pipx install --include-deps ansible
  • Install Ansible with pipLinux, macOS
    python3 -m pip install --user ansible
  • Upgrade Ansible installed with pipxLinux, macOS
    pipx upgrade --include-injected ansible
  • Show the Ansible version, Python and config file in useLinux, macOS
    ansible --version
  • Make an ansible.cfg with every setting listed and commented outLinux, macOS
    ansible-config init --disabled > ansible.cfg
  • Show the settings you've changed from the defaultsLinux, macOS
    ansible-config dump --only-changed
  • Show the config file Ansible is readingLinux, macOS
    ansible-config view
  • List every setting with its environment variableLinux, macOS
    ansible-config list
  • List the built-in modulesLinux, macOS
    ansible-doc --list ansible.builtin
  • Show the docs and examples for a moduleLinux, macOS
    ansible-doc ansible.builtin.copy
  • Show a short task snippet for a moduleLinux, macOS
    ansible-doc --snippet ansible.builtin.copy
  • List the modules in a collectionLinux, macOS
    ansible-doc --list community.general
  • List plugins of one type, such as lookups or filtersLinux, macOS
    ansible-doc --type lookup --list

Inventory and host patterns #

  • Show the inventory as JSON, with groups and variablesLinux, macOS
    ansible-inventory -i inventory.ini --list
  • Show the inventory as YAMLLinux, macOS
    ansible-inventory -i inventory.ini --list --yaml
  • Show the inventory as a tree of groupsLinux, macOS
    ansible-inventory -i inventory.ini --graph
  • Show a group's tree, with each host's variablesLinux, macOS
    ansible-inventory -i inventory.ini --graph --vars webservers
  • Show every variable for one hostLinux, macOS
    ansible-inventory -i inventory.ini --host web1
  • Convert an INI inventory to YAMLLinux, macOS
    ansible-inventory -i inventory.ini --list --yaml --output inventory.yml
  • List the hosts a pattern matchesLinux, macOS
    ansible webservers -i inventory.ini --list-hosts
  • Target hosts in two groupsLinux, macOS
    ansible 'webservers:dbservers' -i inventory.ini --list-hosts
  • Target hosts in both groupsLinux, macOS
    ansible 'webservers:&prod' -i inventory.ini --list-hosts
  • Target a group but leave some hosts outLinux, macOS
    ansible 'all:!dbservers' -i inventory.ini --list-hosts
  • Target hosts whose names match a patternLinux, macOS
    ansible 'web*' -i inventory.ini --list-hosts
  • Run against hosts with no inventory file (note the trailing comma)Linux, macOS
    ansible all -i web1, -m ansible.builtin.ping
  • Use a folder of inventory filesLinux, macOS
    ansible all -i inventory/ --list-hosts
  • List the inventory plugins you have, such as aws_ec2 or proxmoxLinux, macOS
    ansible-doc --type inventory --list
  • Use a dynamic inventory from a cloudLinux, macOS
    ansible-inventory -i aws_ec2.yml --graph

Ad hoc commands #

  • Check Ansible can reach and log in to every hostLinux, macOS
    ansible all -i inventory.ini -m ansible.builtin.ping
  • Run a command on a groupLinux, macOS
    ansible webservers -i inventory.ini -a "uptime"
  • Run a command with pipes or redirects (shell module)Linux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.shell -a "ps aux | grep nginx"
  • Run a command as root with sudoLinux, macOS
    ansible webservers -i inventory.ini -a "whoami" --become
  • Run as root and be asked for the sudo passwordLinux, macOS
    ansible webservers -i inventory.ini -a "whoami" --become --ask-become-pass
  • Log in as a particular userLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.ping -u deploy
  • Log in with a particular SSH keyLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.ping --private-key ~/.ssh/id_ed25519
  • Log in with a password instead of a key (needs sshpass)Linux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.ping --ask-pass
  • Run on more hosts at onceLinux, macOS
    ansible all -i inventory.ini -a "uptime" --forks 20
  • Show one line of output per hostLinux, macOS
    ansible all -i inventory.ini -a "uptime" --one-line
  • Run on just this machine, without SSHLinux, macOS
    ansible localhost -m ansible.builtin.setup -a "filter=ansible_distribution*"
  • Gather all facts about a hostLinux, macOS
    ansible web1 -i inventory.ini -m ansible.builtin.setup
  • Gather some facts with a filterLinux, macOS
    ansible all -i inventory.ini -m ansible.builtin.setup -a "filter=ansible_distribution*"
  • Save each host's facts to its own fileLinux, macOS
    ansible all -i inventory.ini -m ansible.builtin.setup --tree facts
  • Install a packageLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.package -a "name=nginx state=present" --become
  • Update every package on Debian or UbuntuLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.apt -a "update_cache=true upgrade=dist" --become

    Warning: Upgrades every package at once. Try it on one host first, or add --check to see what would change.

  • Update every package on RHEL, Rocky or FedoraLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.dnf -a "name=* state=latest" --become

    Warning: Upgrades every package at once. Try it on one host first.

  • Restart a serviceLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.service -a "name=nginx state=restarted" --become
  • Make sure a service is running and starts at bootLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.service -a "name=nginx state=started enabled=true" --become
  • Copy a file to hostsLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.copy -a "src=files/motd dest=/etc/motd mode=0644" --become
  • Fetch a file from hosts, into a folder per hostLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.fetch -a "src=/var/log/syslog dest=fetched/"
  • Make a folderLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.file -a "path=/etc/motd state=directory mode=0755" --become
  • Delete a file or folderLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.file -a "path=/etc/motd state=absent" --become

    Warning: Deletes the path and everything in it on every matching host.

  • Add a userLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.user -a "name=alice groups=sudo append=true" --become
  • Add an SSH public key for a userLinux, macOS
    ansible webservers -i inventory.ini -m ansible.posix.authorized_key -a "user=deploy key='$(cat ~/.ssh/id_ed25519.pub)'" --become
  • Reboot hosts and wait for them to come backLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.reboot --become

    Warning: Reboots every matching host. Add --forks 1 to do them one at a time.

  • Show disk space on every hostLinux, macOS
    ansible all -i inventory.ini -a "df -h"
  • Show a variable for each hostLinux, macOS
    ansible all -i inventory.ini -m ansible.builtin.debug -a "var=ansible_host"
  • See what an ad hoc command would change, without changing itLinux, macOS
    ansible webservers -i inventory.ini -m ansible.builtin.package -a "name=nginx state=present" --become --check --diff

Running playbooks #

  • Run a playbookLinux, macOS
    ansible-playbook -i inventory.ini site.yml
  • Check a playbook's syntaxLinux, macOS
    ansible-playbook -i inventory.ini site.yml --syntax-check
  • Do a dry run that shows what would changeLinux, macOS
    ansible-playbook -i inventory.ini site.yml --check --diff
  • Show the changes in files as it runsLinux, macOS
    ansible-playbook -i inventory.ini site.yml --diff
  • Run on some hosts onlyLinux, macOS
    ansible-playbook -i inventory.ini site.yml --limit web1
  • Run on a list of hosts from a fileLinux, macOS
    ansible-playbook -i inventory.ini site.yml --limit @hosts.txt
  • Rerun only the hosts that failed last timeLinux, macOS
    ansible-playbook -i inventory.ini site.yml --limit @site.retry
  • Run only tasks with a tagLinux, macOS
    ansible-playbook -i inventory.ini site.yml --tags nginx
  • Run everything except tasks with a tagLinux, macOS
    ansible-playbook -i inventory.ini site.yml --skip-tags nginx
  • List the tasks a playbook would runLinux, macOS
    ansible-playbook -i inventory.ini site.yml --list-tasks
  • List the tags in a playbookLinux, macOS
    ansible-playbook -i inventory.ini site.yml --list-tags
  • List the hosts a playbook would run onLinux, macOS
    ansible-playbook -i inventory.ini site.yml --list-hosts
  • Start part-way through, at a named taskLinux, macOS
    ansible-playbook -i inventory.ini site.yml --start-at-task "Install nginx"
  • Step through a playbook, confirming each taskLinux, macOS
    ansible-playbook -i inventory.ini site.yml --step
  • Set a variable from the command lineLinux, macOS
    ansible-playbook -i inventory.ini site.yml --extra-vars "app_version=1.4.2"
  • Load variables from a fileLinux, macOS
    ansible-playbook -i inventory.ini site.yml --extra-vars @vars/prod.yml
  • Set variables as JSONLinux, macOS
    ansible-playbook -i inventory.ini site.yml --extra-vars '{"app_version": "1.4.2", "debug": false}'
  • Run a playbook as root, asking for the sudo passwordLinux, macOS
    ansible-playbook -i inventory.ini site.yml --become --ask-become-pass
  • Run on at most 5 hosts at a timeLinux, macOS
    ansible-playbook -i inventory.ini site.yml --forks 5
  • Run handlers even if a task failsLinux, macOS
    ansible-playbook -i inventory.ini site.yml --force-handlers
  • Clear cached facts before runningLinux, macOS
    ansible-playbook -i inventory.ini site.yml --flush-cache
  • Show more output, up to -vvvv for SSH detailLinux, macOS
    ansible-playbook -i inventory.ini site.yml -vvv
  • Show how long each task takesLinux, macOS
    ANSIBLE_CALLBACKS_ENABLED=ansible.posix.profile_tasks ansible-playbook -i inventory.ini site.yml
  • Show task results as readable YAMLLinux, macOS
    ANSIBLE_CALLBACK_RESULT_FORMAT=yaml ansible-playbook -i inventory.ini site.yml
  • Run several playbooks in orderLinux, macOS
    ansible-playbook -i inventory.ini site.yml deploy.yml

Vault and secrets #

  • Make a new encrypted fileLinux, macOS
    ansible-vault create group_vars/all/vault.yml
  • Edit an encrypted fileLinux, macOS
    ansible-vault edit group_vars/all/vault.yml
  • Show an encrypted file's contentsLinux, macOS
    ansible-vault view group_vars/all/vault.yml

    Warning: Prints the secrets in plain text.

  • Encrypt an existing fileLinux, macOS
    ansible-vault encrypt group_vars/all/vault.yml
  • Decrypt a file back to plain textLinux, macOS
    ansible-vault decrypt group_vars/all/vault.yml

    Warning: Leaves the secrets in plain text on disk. Use view or edit instead if you only need to read or change them.

  • Change a vault file's passwordLinux, macOS
    ansible-vault rekey group_vars/all/vault.yml
  • Encrypt one value to paste into a variables fileLinux, macOS
    ansible-vault encrypt_string --stdin-name app_version
  • Run a playbook and be asked for the vault passwordLinux, macOS
    ansible-playbook -i inventory.ini site.yml --ask-vault-pass
  • Run a playbook with the vault password in a fileLinux, macOS
    ansible-playbook -i inventory.ini site.yml --vault-password-file ~/.vault_pass

    Warning: Keep the password file out of Git, and readable only by you (chmod 600).

  • Use a vault password file for every command in this terminalLinux, macOS
    export ANSIBLE_VAULT_PASSWORD_FILE=~/.vault_pass
  • Encrypt a file with a labelled vault ID, such as one per environmentLinux, macOS
    ansible-vault encrypt --vault-id prod@prompt group_vars/all/vault.yml
  • Run with more than one vault IDLinux, macOS
    ansible-playbook -i inventory.ini site.yml --vault-id dev@prompt --vault-id prod@~/.vault_pass

Collections and roles (Galaxy) #

  • Install a collectionLinux, macOS
    ansible-galaxy collection install community.general
  • Install a collection at a versionLinux, macOS
    ansible-galaxy collection install community.general:==10.0.0
  • Upgrade a collection to the newest versionLinux, macOS
    ansible-galaxy collection install community.general --upgrade
  • Install the collections and roles in a requirements fileLinux, macOS
    ansible-galaxy install -r requirements.yml
  • Install collections into the project folderLinux, macOS
    ansible-galaxy collection install -r requirements.yml -p ./collections
  • List installed collections and their versionsLinux, macOS
    ansible-galaxy collection list
  • Show where one collection is installedLinux, macOS
    ansible-galaxy collection list community.general
  • Check installed collections haven't been changedLinux, macOS
    ansible-galaxy collection verify community.general
  • Make a new role with the standard foldersLinux, macOS
    ansible-galaxy role init webserver
  • Make a new collection with the standard foldersLinux, macOS
    ansible-galaxy collection init gerard.homelab
  • Install a role from GalaxyLinux, macOS
    ansible-galaxy role install geerlingguy.docker
  • List installed rolesLinux, macOS
    ansible-galaxy role list
  • Remove a roleLinux, macOS
    ansible-galaxy role remove geerlingguy.docker
  • Build a collection into a tarball to shareLinux, macOS
    ansible-galaxy collection build

Linting and testing #

  • Lint a projectLinux, macOS
    ansible-lint
  • Lint one playbookLinux, macOS
    ansible-lint site.yml
  • Fix the lint problems it can fix by itselfLinux, macOS
    ansible-lint --fix

    Warning: Rewrites your YAML files. Commit first so you can see and undo the changes.

  • Lint against a stricter profileLinux, macOS
    ansible-lint --profile production
  • List the rules ansible-lint checksLinux, macOS
    ansible-lint --list-rules
  • Check YAML formattingLinux, macOS
    yamllint .
  • Make a Molecule test scenario in a role or collectionLinux, macOS
    molecule init scenario default
  • Run the full Molecule test: create, converge, verify, destroyLinux, macOS
    molecule test
  • Apply the role to the test instance and keep it runningLinux, macOS
    molecule converge
  • Run the role again to check nothing changes the second timeLinux, macOS
    molecule idempotence
  • Open a shell on the test instanceLinux, macOS
    molecule login
  • Delete the test instanceLinux, macOS
    molecule destroy

Windows hosts #

  • Check Ansible can reach Windows hostsLinux, macOS
    ansible windows -i inventory.ini -m ansible.windows.win_ping
  • Run a PowerShell command on Windows hostsLinux, macOS
    ansible windows -i inventory.ini -m ansible.windows.win_powershell -a "script='Get-Service | Where-Object Status -eq Running'"
  • Restart a Windows serviceLinux, macOS
    ansible windows -i inventory.ini -m ansible.windows.win_service -a "name=Spooler state=restarted"
  • Install Windows updatesLinux, macOS
    ansible windows -i inventory.ini -m ansible.windows.win_updates -a "category_names=SecurityUpdates reboot=true"

    Warning: Reboots the hosts if an update needs it.

  • Copy a file to Windows hostsLinux, macOS
    ansible windows -i inventory.ini -m ansible.windows.win_copy -a "src=files/motd dest=C:\\Temp\\motd"

Network devices #

  • Run show commands on Cisco IOS devicesLinux, macOS
    ansible switches -i inventory.ini -m cisco.ios.ios_command -a "commands='show version'"
  • Gather facts from Cisco IOS devicesLinux, macOS
    ansible switches -i inventory.ini -m cisco.ios.ios_facts
  • Back up Cisco IOS running configsLinux, macOS
    ansible switches -i inventory.ini -m cisco.ios.ios_config -a "backup=true"
  • Run show commands on Cisco NX-OS devicesLinux, macOS
    ansible switches -i inventory.ini -m cisco.nxos.nxos_command -a "commands='show version'"
  • Run a command on any supported network OSLinux, macOS
    ansible switches -i inventory.ini -m ansible.netcommon.cli_command -a "command='show version'"
  • Connect to devices without setting the connection in the inventoryLinux, macOS
    ansible switches -i inventory.ini -c ansible.netcommon.network_cli -e ansible_network_os=cisco.ios.ios -m cisco.ios.ios_command -a "commands='show clock'" -u deploy --ask-pass
  • Install the Cisco and network collectionsLinux, macOS
    ansible-galaxy collection install cisco.ios cisco.nxos ansible.netcommon

Other tools #

  • Pull a playbook from Git and run it on this machineLinux, macOS
    ansible-pull -U https://github.com/example/ansible-config.git site.yml
  • Open an interactive console for running tasks on hostsLinux, macOS
    ansible-console -i inventory.ini webservers
  • Run a playbook in an execution environment with ansible-navigatorLinux, macOS
    ansible-navigator run site.yml -i inventory.ini --mode stdout
  • Browse a playbook run's results with ansible-navigatorLinux, macOS
    ansible-navigator replay site-artifact.json
  • Build an execution environment image with ansible-builderLinux, macOS
    ansible-builder build --tag my-ee:1.0

Troubleshooting #

  • Show every SSH connection detailLinux, macOS
    ansible web1 -i inventory.ini -m ansible.builtin.ping -vvvv
  • Print a variable's value for each hostLinux, macOS
    ansible all -i inventory.ini -m ansible.builtin.debug -a "msg={{ ansible_host }}"
  • Test a Jinja expression against a hostLinux, macOS
    ansible web1 -i inventory.ini -m ansible.builtin.debug -a "msg={{ inventory_hostname | upper }}"
  • Show which hosts are in a group, from inside AnsibleLinux, macOS
    ansible localhost -i inventory.ini -m ansible.builtin.debug -a "msg={{ groups['webservers'] }}"
  • Skip SSH host key checks, for brand-new lab machinesLinux, macOS
    ANSIBLE_HOST_KEY_CHECKING=False ansible all -i inventory.ini -m ansible.builtin.ping

    Warning: Accepts any host key, so you can't tell if you're talking to the wrong machine. Use it only on a lab or test network.

  • Keep the files Ansible copies to hosts, to debug a moduleLinux, macOS
    ANSIBLE_KEEP_REMOTE_FILES=1 ansible web1 -i inventory.ini -m ansible.builtin.ping -vvv
  • Show the Python interpreter Ansible found on a hostLinux, macOS
    ansible web1 -i inventory.ini -m ansible.builtin.setup -a "filter=ansible_python*"
  • Use a particular Python on a hostLinux, macOS
    ansible web1 -i inventory.ini -m ansible.builtin.ping -e ansible_python_interpreter=/usr/bin/python3
  • Use an ansible.cfg from another folderLinux, macOS
    ANSIBLE_CONFIG=~/lab/ansible.cfg ansible-playbook -i inventory.ini site.yml
  • Clear the fact cacheLinux, macOS
    rm -rf ~/.ansible/fact_cache