Skip to content

Instantly share code, notes, and snippets.

@nntrn
Last active August 18, 2021 07:10
Show Gist options
  • Save nntrn/24b19fbbf9bd7e196054537d6728d7da to your computer and use it in GitHub Desktop.
Save nntrn/24b19fbbf9bd7e196054537d6728d7da to your computer and use it in GitHub Desktop.
handy command line ansible snippets
# NOTES
# ansible [pattern] -m [module] -a "[module options]"
# add `--tree <foldername>` to store output in directory
# add `-i <inventory_file>` if inventory is not specified in ansible.cfg
# ==============================================================================
# GETTING FACTS
# ==============================================================================
# output ansible facts to out directory
mkdir -p out/{free,distribution}
ansible -m setup --tree out/ all
# filter facts
ansible all -m setup -a 'filter=distribution*' --tree out/distribution
ansible all -m command -a "free -m" --tree out/free
# add -o to display on one line
ansible all -m command -a "fuptime" -o
# Display facts from all hosts and store them at /tmp/facts
mkdir -p /tmp/facts
ansible all -m setup --tree /tmp/facts
# Display only facts regarding memory found by ansible on all hosts and output them.
ansible all -m setup -a 'filter=ansible_*_mb'
# Display only facts returned by facter.
ansible all -m setup -a 'filter=facter_*'
# Collect only facts returned by facter.
ansible all -m setup -a 'gather_subset=!all,!any,facter'
# Display only facts about certain interfaces.
ansible all -m setup -a 'filter=ansible_eth[0-2]'
# Restrict additional gathered facts to network and virtual (includes default minimum facts)
ansible all -m setup -a 'gather_subset=network,virtual'
# Collect only network and virtual (excludes default minimum facts)
ansible all -m setup -a 'gather_subset=!all,!any,network,virtual'
# Do not call puppet facter or ohai even if present.
ansible all -m setup -a 'gather_subset=!facter,!ohai'
# Only collect the default minimum amount of facts:
ansible all -m setup -a 'gather_subset=!all'
# Collect no facts, even the default minimum subset of facts:
ansible all -m setup -a 'gather_subset=!all,!min'
# Display facts from Windows hosts with custom facts stored in C:\custom_facts
ansible windows -m setup -a "fact_path='c:\custom_facts'"
# ==============================================================================
# RUN AS USER
# ==============================================================================
# If you do not like this behavior, pass in "-u username". If you want to run commands as a different user, it looks like this:
ansible all -a "/usr/bin/foo" -u username
# Often you'll not want to just do things from your user account. If you want to run commands through privilege escalation:
ansible all -a "/usr/bin/foo" -u username --become [--ask-become-pass]
# It is also possible to become a user other than root using --become-user:
ansible all -a "/usr/bin/foo" -u username --become --become-user otheruser [--ask-become-pass]
# ==============================================================================
# PING
# ==============================================================================
# Ping a server
ansible -m ping servername
# Ping a group of servers
ansible -m ping all
# ==============================================================================
# COPY
# ==============================================================================
# Transfer a file directly to many servers
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
# ==============================================================================
# FILE
# ==============================================================================
# The file module allows changing ownership and permissions on files.
# These same options can be passed directly to the copy module as well:
ansible all -m file -a "dest=/srv/foo/a.txt mode=600"
ansible all -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
# The file module can also create directories, similar to mkdir -p:
ansible all -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
# As well as delete directories (recursively) and delete files:
ansible all -m file -a "dest=/path/to/c state=absent"
# ==============================================================================
# PACKAGES
# ==============================================================================
# note: sub `yum` for `apt` for ubuntu
# Ensure a package is installed, but don’t update it:
ansible all -m yum -a "name=acme state=present"
# Ensure a package is installed to a specific version:
ansible all -m yum -a "name=acme-1.5 state=present"
# Ensure a package is at the latest version:
ansible all -m yum -a "name=acme state=latest"
# Ensure a package is not installed:
ansible all -m yum -a "name=acme state=absent"
# add -b for become
ansible all -m yum -a "name=httpd state=absent" -b
# ==============================================================================
# USER
# ==============================================================================
ansible all -m user -a 'name=annie_tran state=absent'
# ==============================================================================
# GIT
# ==============================================================================
# Deploy your webapp straight from git:
ansible all -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
# ==============================================================================
# SERVICES
# ==============================================================================
# Ensure a service is started on all servers:
ansible all -m service -a "name=httpd state=started"
# Alternatively, restart a service on all servers:
ansible all -m service -a "name=httpd state=restarted"
# Ensure a service is stopped:
ansible all -m service -a "name=httpd state=stopped"
# ==============================================================================
# SCRIPT
# ==============================================================================
# Note: the test.sh script must be available on the Ansible Controller machine
ansible all -m script test.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment