Ansible Basics

Start Locally

Start by doing normal actions on the computer.

Say 'hello' to yourself:

1ansible --module-name=ping localhost

Ansible takes a lot of information about each machine during setup:

1TMP=$(mktemp)
2ansible --module-name=setup localhost  | tee $TMP
3less !$

If you have jq, you can pull out info:

1sed -i 's/.*SUCC.*/{/' $TMP
2jq '.ansible_facts.ansible_distribution' < $TMP

Upgrade through the package manager.

packager=apt (or pacman or xbps,...)

1packager="$( jq -r '.ansible_facts.ansible_pkg_mgr' < $TMP )"
2ansible --module-name=${packager} --args "upgrade=yes" localhost

This fails because you have not 'become root'. So, 'become'!

1ansible --become -m ${packager} -a "upgrade=true" localhost

Passwords

Typing the password is dull. You might shift it to the command line:

ansible-playbook t.yaml -i hosts.yaml -e "ansible_become_password=${password}"

...this is also dull.

If you have a password store, like pass, you can put that in a script:

1echo "#!/bin/sh
2pass $HOSTNAME" > pass.sh
3
4chmod u+x !$
5
6ansible --become --module-name=pacman --args "upgrade=true" localhost

Other Hosts

Find something you can ssh into. Ansible will use your /etc/hosts file, and ~/.ssh/config.

Make a Hosts File

You can use the .ini format:

1echo '[phones]
2192.168.0.20' > hosts

But everything uses yaml nowadays, so may as well be consistent:

1all:
2  children:
3    phones:
4      children:
5        pine:
6          ansible_host: 192.168.0.20

Check the inventory in yaml format:

1ansible-inventory --list -y -i
1ansible-vault view sec.yml --vault-pass-file pass.sh

Install `espeak', then make the computer say something:

1ansible --module-name=say --args "msg='testing'" localhost