Creating Playbooks
Ansible allows us to carry out more ad-hoc tasks on servers within our inventory as well. For example, if we wanted to create a playbook that carried out a set of tasks, we could do so in a similar format to how we would create / define an Ansible Role - using YAML syntax and and defining the same or very similar set of parameters within one file.
---
- hosts: bookstack
become: yes
tasks:
- name: Backup Bookstack container files
command: tar -cvzf bookstack-backup.tar.gz /home/admin/bookstack
- name: Fetch backup files from remote host
command: scp -P 2222 -i /home/kapak/.ssh/id_rsa /home/admin/bookstack-backup.tar.gz admin@sub.domain.com:/home/admin/backups/bookstack/
Here, we use scp
instead of Ansible's Fetch module to save memory on the small host that runs the BookStack you are viewing. When fetching large files, memory errors can be encountered so here we have worked around the module using an alternative method for transferring our files.
Ad-Hoc Commands
To run commands on a server or a group of servers within the /etc/ansible/hosts
file, run any of the below
ansible -m ping hostname
ansible -m ping 134.23.4.5
ansible -a "sudo ls /" hostname
ansible -a "sudo ls /" 134.23.4.5
ansible -a "free -h" hostname
ansible -a "free -h" 134.23.4.5