Ansible - Playbooks: Sample Playbook
âļī¸
Ships & Commanders Example
đĸđī¸
Another example of a playbook is the ships & commanders
example. In this example, we have a group of ships
and a group of commanders
. Both groups have a setup
and logs
task. The setup
task creates a file for its current host. The log
tasks reads the file, prints the contents to the console, and writes events to the file. Both groups also have a unique event
task that they run to be logged in their respective files. Everything is ran in main.yml
.
-
run example
đ ansible-zero-to-pro â ansible-playbook main.yml -i inventory.txt
Playbooks âī¸
-
setting up
# ships-setup.yml - name: ships setup hosts: ships tags: ['ship', 'setup'] vars: log_dir: "/target_logs" file_name: "-ship.txt" tasks: - name: create ship file command: touch chdir= creates=
# commander-setup.yml - name: commanders setup hosts: commanders tags: ['commander', 'setup'] vars: log_dir: "/target_logs" file_name: "-commander.txt" tasks: - name: create commander file command: touch chdir= creates=
# logs-setup.yml - name: dir setup hosts: all tags: ['setup', 'dir'] vars: play_name: "" log_dir: "/target_logs" tasks: - name: create target dir command: mkdir chdir=/ creates=
-
logs
# read-ships-logs.yml - name: read ship logs hosts: ships tags: ['ship', 'log', 'read'] vars: play_name: "" log_dir: "/target_logs" file_name: "-ship.txt" tasks: - name: reading ... command: "cat /" register: result - ansible.builtin.debug: msg: ""
# write-ships-logs.yml - name: 'writing ship logs...' vars: log_dir: "/target_logs" file_name: "-ship.txt" event: "**DEFAULT**" event_result: "**DEFAULT**" log: "{'inventory_hostname': '', 'host-name': '', 'date': '', 'time': '', 'event': '', event_result: }" lineinfile: line: "" path: "/" tags: [ship, log, write]
# read-commander-logs.yml - name: commander read logs hosts: commanders tags: [commander, log, read] vars: play_name: "" log_dir: "/target_logs" file_name: "-commander.txt" tasks: - name: reading ... command: "cat /" register: result - ansible.builtin.debug: msg: ""
# write-commander-logs.yml - name: 'writing commander logs...' vars: log_dir: "/target_logs" file_name: "-commander.txt" event: "**DEFAULT**" event_result: "**DEFAULT**" log: "{'inventory_hostname': '', 'host-name': '', 'date': '', 'time': '', 'event': '', event_result: }" lineinfile: line: "" path: "/" tags: [commander, log, write]
-
random tasks
# commander-command.yml - name: commander command tester hosts: commanders tags: ['commander', 'command'] vars: play_name: '' tool: 'nslookup' parameter: 'google.com' tasks: - name: ' test...' command: ' ' register: result ignore_errors: true - debug: msg: ' â failed...' when: result is failed - debug: msg: ' đ status changed...' when: result is changed - debug: msg: ' âī¸ was skipped...' when: result is skipped - debug: msg: ' âī¸ succeeded...' when: result is success - name: 'searching for package' shell: 'dnf provides -q' register: result2 ignore_errors: True when: result is failed - command: "echo " register: command_package when: result2 is success and result is failed - ansible.builtin.debug: msg: ' đĻ found , attempting to install...' when: result2 is success and result is failed - command: 'dnf -y install ' when: result2 is success and result is failed - command: ' ' register: result - include_tasks: write-commander-logs.yml vars: event: ' ' event_result: "''"
# ships-pokemon-command.yml - name: random pokemon hosts: ships tags: ['ship', 'command'] vars: play_name: "" tool: "curl" url: "https://retro-pokemon-game-api-k6cgale4bq-uc.a.run.app/pokemon" tasks: - set_fact: num: "" when: num is not defined - name: 'getting a random pokemon...' command: " /" register: result ignore_errors: true - debug: msg: " â failed..." when: result is failed - debug: msg: " đ status changed..." when: result is changed - debug: msg: " âī¸ was skipped..." when: result is skipped - debug: msg: " âī¸ succeeded..." when: result is success - include_tasks: write-ships-logs.yml vars: event: " /" event_result: ""
-
main
# main.yml - name: dir setup run_once: true import_playbook: logs-setup.yml - name: commander setup run_once: true import_playbook: commander-setup.yml - name: ships setup run_once: true import_playbook: ships-setup.yml - name: commander command import_playbook: commander-command.yml vars: tool: "ls" parameter: "-la" - name: ships get random pokemon import_playbook: ships-pokemon-command.yml - name: read commander logs import_playbook: read-commander-logs.yml - name: read ships logs import_playbook: read-ships-logs.yml