Ansible Variables

Variables could be defined in #202204272036 and #202204272043. However, it is recommended to put variables, primarily credentials that are essential for connection such as username and password, into Inventory.

Note: Variables stored in #202204300820 are called facts.

To access a variable or fact in Playbook, use the filter syntax or Jinja syntax {{ variable }}. The variable could be stored as YAML file inside directories named host_vars or group_vars for the Playbook to access. The variables for hosts will take precedence over variables for groups since they are more specific.

Control Flow

For conditionals, we could use the #202204300820 when. One can use comparison operators such as ==, !=, >, >=, < and <=, or in keyword to check whether the value is found in a list. It has some limitation for potential large amount of tasks. For this, it is advised to use handler or separate the tasks into a #202204272036 and then included later. The usage is shown as below:

...
  tasks:
  - name: Install apache
    apt:
      name: apache2
      state: latest
    when: ansible_distribution == 'Debian' or
          ansible_distribution == 'Ubuntu'
...

register module could be used in order to store the return status of a task in order to be used in a later stage. A better approach in control flow could be by using handlers. The following shows the example use case:

...
  tasks:
  - name: Ensure httpd package is present
    yum:
      name: httpd
      state: latest
    register: httpd_results
  
  - name: Restart httpd
    service:
      name: httpd
      state: restart
    when: httpd_results.changed
...

Module notify is used to notify the handlers when there is a change of state of the task. Use the module handlers to define handler. Its format is quite similar to #the tasks module. For example:

...
  tasks:
  - name: Ensure httpd package is present
    yum:
      name: httpd
      state: latest
    notify: restart_httpd
  
  handlers:
  - name: restart_httpd
    service:
      name: httpd
      state: restart
...

We could loop over a list of variables or items using the module loop. This could become handy when there is a task should be repeated with multiple variables. That being said, using loop is unfavourable due to the performance hit. A lot of times, #202204300820 has better built-in than loop. The following code shows how to use loop:

...
  tasks:
  - name: Ensure user is present
    user:
      name: "{{ item }}"
      state: present
    loop:
      - dev_user
      - qa_user
      - prod_user
...

Note: item can store an object, meaning it could be a JSON list instead of just value.

Links to this page
  • Automation Controller

    We can put up a survey for end user to configure 202205042134# values for the Job.

  • Ansible Templates

    Templates use Jinja2 templating to generate files based on defined #202205042134. Use the #202204300820 template inside #202204272036. The following shows its usage:

  • Ansible Roles

    Role is a disaggregated #202204272036 which is reusable and #distributable (could be seen as a collection). It has a directory structure which includes default variables, handlers and others#, metadata (dependencies which could be fetched by 202205051149#), tasks, 202205051607#, and tests, shown as below:

    There are two directories that store defined 202205042134# : defaults and vars. While defaults set the default value for a variable, vars takes higher precedence if there is a duplication since variables defined in here are more specific.

  • Ansible Plugins

    Filter Plugin is a kind of Ansible Plugins that could be used to change the output format for 202205042134# of a task to JSON, YAML or CSV. It uses Jinja2 filter for example {{ variable | trim }}.

  • Ansible Playbooks

    Playbooks are YAML files that are used for #202204272021 configuration. Structurally, it contains plays, 202205042134#, 202204300820#, and 202204300825#. They could be run by the command line ansible-playbook or #202204300954. It is shown as below:

  • Ansible Modules

    The 202205042134# defined within a module is call a fact. Run ansible all -m {module} to check all the facts that has been defined in the module specified. Since facts are variables, we could use them in #202204272036 using the Jinja syntax {{ }}.

  • Ansible Inventory

    Note: Although Inventory could be used to store 202205042134#, it is advised to only define them if they are necessary for device connection such as user password and mean of communication.

    Note: Using keyword vars after the group name define group variables which are only able to be used for that group. However, if the name before the keyword is all, the variables could be used by every group or host in the Inventory.

#automation