From Playbook to Production: Projects, Templates, Jobs, and Workflows in Practice
58%
Surveys: Turning Templates Into Forms
Surveys prompt users for additional information when launching a job template. You can use them to ask for things like the name of the project, the environment, the application, and other information needed to run the job. Surveys are useful for making job templates more flexible and reusable.
To understand how surveys function, let's create a new playbook that installs a package on a host. Create a new folder named InstallPackage under $PROJECTS_PATH and add a new playbook named install_package.yml with the following content:
# Create the project directory
mkdir -p $PROJECTS_PATH/InstallPackage
# Create the playbook
cat << 'EOF' > $PROJECTS_PATH/InstallPackage/install_package.yml
---
- name: Install package and optionally restart service
hosts: all
# Ensures that tasks are run with elevated privileges
become: yes
tasks:
- name: Install a package using YUM
ansible.builtin.yum:
name: "{{ package }}"
state: present
# This task runs if OS => the RH family
when: ansible_os_family == "RedHat"
- name: Install a package using APT
ansible.builtin.apt:
name: "{{ package }}"
state: present
# This task runs if OS => the Debian family
when: ansible_os_family == "Debian"
- name: Restart a service
ansible.builtin.systemd:
name: "{{ service }}"
state: restarted
when: restart_service is defined and restart_service|bool
# This task will only execute if `restart_service`
# is defined and true.
# It uses the `systemd` module to restart
#the specified service.
EOF
AWX in Action
Ansible Orchestration at ScaleEnroll now to unlock all content and receive all future updates for free.
