Playbook Modules
Ansible consist of many pre-built modules that helps us to create playbooks. For example,
set_fact
pause
prompt
wait_for
assemble
add_host
group_by
fetch
set_fact
This module allows us to add or change facts during the execution.
For example if we consider the below playbook,
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: ubuntu3,centos3
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Set a fact
set_fact:
our_fact: Ansible Rocks!
- name: Show custom fact
debug:
msg: "{{ our_fact }}"
# Three dots indicate the end of a YAML document
...In the first task we are setting a fact with the name out_fact and in the second task we print it's value using the debug module.
In addition to that, we can set multiple facts in the same iteration of the set_fact module. For example, let's modify our playbook in such a way,
Then, let's assume that we have to set facts conditionally, for example, based on the environment. In such cases, we can do this by using the when key word along with the set_fact module,
pause module allows us to temporarily stop the playbook execution for a specified time period. It is also possible to interrput the pause by clieck control+C keys. For example,
Running this playbook will output the following:
In a situation where we have to take a manual action, we can use the pause module with prompt option. This will allow us to show a message to the user and ask to perform an action. For example,
Running this playbook will result in the following output,
In some cases when we want to wait for certian operations to be completed, we could use the wait_for module rather than using pause. For example, in a situation where we have to wait until a port becomes available.
assemble module allows us to concatenate multiple files into one. For example, imagin a situation where we keep a comman ssh config file along with additional host specific configurations in separate files. Now that when we want to connect with those, we can use the assemble module to create a single ssh config file.
For example, let's start by creating a couple of ssh config files in conf.d directory like below,
conf.d/defaults:
conf.d/centos1:
assemble_playbook.yaml :
In above playbook, it takes all content inside config.d directory and create a single config file with the name sshd_config. Which would contain a merged set of entries like below,
add_host is another module where we can add hosts to our inventory dynamically. This useful when we create hosts in our playbook. Playbook example for this looks like below,
Above playbook will add centos1 to adhoc_group1 and adhoc_group2 and then ping all instances in adhoc_group1.
Similarly group_by module associates / group items based on what's been advised. For example, below playbook group each host by OS distribution and then ping all centos instances in the custom group.
Finally, fetch module allows us to get files from the remote machine. For example,
Last updated
Was this helpful?