Ansible set_fact to create a dictionary from register result

The set_fact module in Ansible is used to set variables on a host-by-host basis. These variables are available to subsequent plays during an Ansible playbook run. The set_fact module takes key: value (YAML notation) pairs as variables to set in the playbook scope.

Using set_fact in Ansible, you can create a dictionary from the results stored in a register variable. Register variables allow you to store the output of a task and then manipulate that data later in your playbook. Here's how you can create a dictionary using set_fact:

Assuming you have a task that registers the results in a variable called my_register_var, the following playbook demonstrates how to convert that register variable into a dictionary using set_fact:

---
- hosts: localhost
  gather_facts: false
  connection: local

  tasks:
    - name: Get system uptime
      ansible.builtin.shell: uptime
      register: my_register_var

    - name: Convert register variable to a dictionary
      set_fact:
        my_dictionary: "{{ my_register_var.stdout | regex_findall('\\w+=\\d+') | map('split', '=') | items2dict }}"

    - name: Display system uptime
      debug:
        msg: "System Uptime: {{ my_register_var.stdout }}"
        
    - name: Example task using the created dictionary
      debug:
        msg: "Key: {{ item.key }}, Value: {{ item.value }}"
      loop: "{{ my_dictionary | dict2items }}"

Code explanation

  • Line 1: The playbook starts with three dashes (---), which is the standard YAML indicator for a new document.

  • Line 2: The hosts: localhost specifies that the playbook tasks will be executed on the localhost (the control machine where Ansible is being run).

  • Line 3: The gather_facts: false indicates that Ansible won't gather facts about the target hosts. Gathering facts refers to collecting information about the target hosts (like OS, network interfaces, etc.)

  • Line 4: Here we specify that the playbook tasks should be run directly on the control machine (localhost) without attempting an SSH connection.

  • Line67: The tasks section begins, and the first task is defined with the name Get system uptime.

  • Line 8: The ansible.builtin.shell: uptime is an Ansible task that runs the uptime command on the control machine using the shell module (which is part of the built-in Ansible modules). This command retrieves the system's uptime.

  • Line 9: Here we store the output of the uptime command in a variable called my_register_var. This allows us to access the command's output later in the playbook.

  • Line 11: Here the second task is defined with the name Convert register variable to a dictionary.

  • Line 12: set_fact is an Ansible module that allows us to create new facts (variables) and assign values to them.

  • Line 13: Here, we use an expression that creates a new variable called my_dictionary and sets its value to the result of the expression on the right-hand side.The expression my_register_var.stdout accesses the stdout attribute of the my_register_var variable, which contains the output of the uptime command.

  • Line 15: The third task is defined with the name Display system uptime.

  • Line 16: debug is an Ansible module that displays debug information during playbook execution.

  • Line 17: The msg: "System Uptime: {{ my_register_var.stdout }}" prints the value of the stdout attribute from the my_register_var variable, which contains the system's uptime.

  • Line 19: The fourth task is defined with the name Example task using the created dictionary.

  • Line 21: The msg: "Key: {{ item.key }}, Value: {{ item.value }}" displays the key and value of each item in the my_dictionary variable.

  • Line 22: The loop: "{{ my_dictionary | dict2items }}" iterates through the key-value pairs in the my_dictionary variable using the dict2items filter.

Conclusion

The provided Ansible playbook is configured to execute tasks localhost without SSH connections to remote hosts. It captures the output of the uptime command in my_register_var and converts the relevant key-value pairs into a dictionary stored in my_dictionary. The playbook then displays the system uptime and presents the key-value pairs through the debug module for local analysis.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved