Skip to content

Ansible Ad-Hoc Cheatsheet⚓︎


Open Port Checking⚓︎

Bash
1
2
3
4
5
ansible -m raw -a "portconnect server.company.com 443" client.company.com
ansible -m wait_for -a "host=server.company.com port=443 state=started timeout=3" client.company.com
ansible -m wait_for -a "host=server.company.com port=80 state=started timeout=3" inv_group -o
ansible -m wait_for -a "host=server.company.com port=443 state=started timeout=3" client.company.com,inv_group -o
ansible -m wait_for -a "host=server.company.com port=443 state=started timeout=3" client.company.com -o

Upgrading and Installing Packages⚓︎

Bash
1
2
3
4
5
# Only upgrade a package if it is installed
ansible -m yum -a "state=latest update_only=yes name=firefox" --become client.company.com,inv_group

# install a package to the latest version
ansible -m yum -a "state=latest uname=firefox" --become client.company.com,inv_group

Modify an INI File⚓︎

Bash
1
ansible -m ini_file -a 'path=/etc/dnf/dnf.conf section=main option=installonly_limit value=2 no_extra_spaces=yes' inv_group --become

Modify Lines in a File⚓︎

Bash
1
2
ansible -m lineinfile -a "path=/etc/auto.direct create=no regex='^/NAS/ -rw .*' state=absent" -o --become inv_group
ansible -m lineinfile -a "path=/etc/ntp.conf regex='^server ' line='pool time.company.com iburst'" --become inv_group

Count OS Versions⚓︎

Bash
1
2
ansible -m raw -a "cat /etc/release" inv_solaris | grep Solaris | sort | uniq -c
ansible -m raw -a "cat /etc/redhat-release" inv_group | grep release | sort | uniq -c

Copy a File to Systems⚓︎

Bash
1
2
ansible -m copy -a "src=/tmp/app.zip dest=/home/user/app.zip owner=root" svr[1:5]vm[1:4].company.com --become
ansible -m copy -a "src=login_motd.txt dest=/etc/motd mode=0644 owner=root group=root " inv_group --become

Restart a Service⚓︎

Bash
1
ansible -m service -a "name=ntpd state=restarted" -u root --ask-pass inv_group

Create a Directory⚓︎

Bash
1
ansible -m file -a "path=/app/bin state=directory owner=username group=staff" --become -o inv_group

Run a Custom Role⚓︎

Bash
1
ansible -m include_role -a "role=av_config" inv_group