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: localhostspecifies that the playbook tasks will be executed on the localhost (the control machine where Ansible is being run).Line 3: The
gather_facts: falseindicates 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.
Line6–7: The
taskssection begins, and the first task is defined with the nameGet system uptime.Line 8: The
ansible.builtin.shell: uptimeis an Ansible task that runs theuptimecommand on the control machine using theshellmodule (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
uptimecommand in a variable calledmy_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_factis 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_dictionaryand sets its value to the result of the expression on the right-hand side.The expressionmy_register_var.stdoutaccesses thestdoutattribute of themy_register_varvariable, which contains the output of theuptimecommand.Line 15: The third task is defined with the name
Display system uptime.Line 16:
debugis an Ansible module that displays debug information during playbook execution.Line 17: The
msg: "System Uptime: {{ my_register_var.stdout }}"prints the value of thestdoutattribute from themy_register_varvariable, 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 themy_dictionaryvariable.Line 22: The
loop: "{{ my_dictionary | dict2items }}"iterates through the key-value pairs in themy_dictionaryvariable using thedict2itemsfilter.
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