Useful Ansible Commands
Jeff Geerling has lots of great resources on both Ansible and Kuberenetes. Here are some Ansible notes, I got mostly from watching his videos.
Create / delete a user:
ansible app -b -m user -a "name=johndoe group=admin createhome=yes"
ansible app -b -m user -a "name=johndoe state=absent remove=yes"
Moving files back and forth:
ansible pis -m copy -a "src=/etc/hosts dest=/tmp/hosts"
ansible pis -b -m fetch -a "src=/etc/hosts dest=/tmp"
For more heavy duty file management, check out the synchronize and rsync modules.
Create directories with, set permissionrs, create links, and delete files
ansible multi -m file -a "dest=/tmp/test mode=644 state=directory"
ansible multi -m file -a "src=/src/file dest=/dest/symlink state=link"
ansible multi -m file -a "dest=/tmp/test state=absent"
Manage crontab
ansible pis -b -m cron -a "name='daily-cron-all-servers' minute=2 hour=4 job='/path/to/daily-script.sh'"
Unspecified times are treated as *
. The -b
flag is needed to become root.
Deploy Git repo (See ansible.builtin.git module – Deploy software (or files) from git checkouts — Ansible Documentation
ansible pis -b -m git -a "repo=git://example.com/path/to/repo.git \
dest=/opt/myapp update=yes version=1.2.4"
Reboot machines
$ ansible -i inventory pi_kubernetes_cluster -m reboot -u adm -b
Installing via a download script
Sometimes, the recommended package installation procedure is to download an installation script. e.g.
curl -sfL https://get.k3s.io | sh -
To make this idempotent, and to avoid re-running the script every time the play is run, try this approach:
- name: Download k3s binary arm64
get_url:
url: https://get.k3s.io
dest: /tmp/install.sh
owner: root
group: root
mode: 0744
- name: Run Install script
command: /tmp/install.sh