Ansible supports variables that can be used to store values that can be reused throughout files in an Ansible project. This can simplify the creation and maintenance of a project and reduce the number of errors.
Variables provide a convenient way to manage dynamic values for a given environment in your Ansible project. Examples of values that variables might contain include:

  • Users to create
  • Packages to install
  • Services to restart
  • Files to remove
  • Archives to retrieve from the internet

DEFINING VARIABLES

Variables can be defined in a variety of places in an Ansible. However, this can be simplified to three basic scope levels:

  • Global scope: Variables set from the command line or Ansible configuration.
  • Play scope: Variables set in the play and related structure.
  • Host scope: Variables set on host groups and individual hosts by the inventory, fact gathering or registered tasks

If the same variable name is defined at more than one level, the level with the highest precedence wins. A narrow scope takes precedence over a wider scope: variables defined by the inventory are overridden by variables defined by the playbook, which are overridden by variables defined on the command line.

TASK BY TRAINER:

  1. Create the playbook.yml playbook and define the following variables in the vars section.
VARIABLE DESCRIPTION
web_pkg Name of the package to install for the web server.
firewall_pkg Name of the firewall package.
web_service Name of the web service to manage.
firewall_service Name of the firewall service to manage.
python_pkg Name of the required package for the uri module.
rule Name of service to open.
  1. Create the tasks block and create the first taşk, which should use the yum module to make sure the latest versions of the required packages are installed.
  • web_pkg
  • firewall_pkg
  • python_pkg
  1. Create two tasks to make sure that the httpd and firewalld services are started and enabled.