| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2013, Dag Wieers (@dagwieers) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: set_fact |
| 12 |
short_description: Set host variable(s) and fact(s). |
| 13 |
version_added: "1.2" |
| 14 |
description: |
| 15 |
- This action allows setting variables associated to the current host. |
| 16 |
- These variables will be available to subsequent plays during an ansible-playbook run via the host they were set on. |
| 17 |
- Set O(cacheable) to V(true) to save variables across executions using a fact cache. |
| 18 |
Variables will keep the set_fact precedence for the current run, but will used 'cached fact' precedence for subsequent ones. |
| 19 |
- Per the standard Ansible variable precedence rules, other types of variables have a higher priority, so this value may be overridden. |
| 20 |
options: |
| 21 |
key_value: |
| 22 |
description: |
| 23 |
- "The M(ansible.builtin.set_fact) module takes C(key=value) pairs or C(key: value) (YAML notation) as variables to set in the playbook scope. |
| 24 |
The 'key' is the resulting variable name and the value is, of course, the value of said variable." |
| 25 |
- You can create multiple variables at once, by supplying multiple pairs, but do NOT mix notations. |
| 26 |
required: true |
| 27 |
cacheable: |
| 28 |
description: |
| 29 |
- This boolean converts the variable into an actual 'fact' which will also be added to the fact cache. |
| 30 |
It does not enable fact caching across runs, it just means it will work with it if already enabled. |
| 31 |
- Normally this module creates 'host level variables' and has much higher precedence, this option changes the nature and precedence |
| 32 |
(by 7 steps) of the variable created. |
| 33 |
U(https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable) |
| 34 |
- "This actually creates 2 copies of the variable, a normal 'set_fact' host variable with high precedence and |
| 35 |
a lower 'ansible_fact' one that is available for persistence via the facts cache plugin. |
| 36 |
This creates a possibly confusing interaction with C(meta: clear_facts) as it will remove the 'ansible_fact' but not the host variable." |
| 37 |
type: bool |
| 38 |
default: no |
| 39 |
version_added: "2.4" |
| 40 |
extends_documentation_fragment: |
| 41 |
- action_common_attributes |
| 42 |
- action_common_attributes.conn |
| 43 |
- action_common_attributes.flow |
| 44 |
- action_core |
| 45 |
attributes: |
| 46 |
action: |
| 47 |
details: While the action plugin does do some of the work it relies on the core engine to actually create the variables, that part cannot be overridden |
| 48 |
support: partial |
| 49 |
bypass_host_loop: |
| 50 |
support: none |
| 51 |
bypass_task_loop: |
| 52 |
support: none |
| 53 |
check_mode: |
| 54 |
support: full |
| 55 |
core: |
| 56 |
details: While parts of this action are implemented in core, other parts are still available as normal plugins and can be partially overridden |
| 57 |
support: partial |
| 58 |
delegation: |
| 59 |
details: |
| 60 |
- while variable assignment can be delegated to a different host the execution context is always the current inventory_hostname |
| 61 |
- connection variables, if set at all, would reflect the host it would target, even if we are not connecting at all in this case |
| 62 |
support: partial |
| 63 |
diff_mode: |
| 64 |
support: none |
| 65 |
notes: |
| 66 |
- Because of the nature of tasks, set_fact will produce 'static' values for a variable. |
| 67 |
Unlike normal 'lazy' variables, the value gets evaluated and templated on assignment. |
| 68 |
- Some boolean values (yes, no, true, false) will always be converted to boolean type, |
| 69 |
This is done so the C(var=value) booleans, |
| 70 |
otherwise it would only be able to create strings, but it also prevents using those values to create YAML strings. |
| 71 |
Using the setting will restrict k=v to strings, but will allow you to specify string or boolean in YAML. |
| 72 |
- "To create lists/arrays or dictionary/hashes use YAML notation C(var: [val1, val2])." |
| 73 |
- Since 'cacheable' is now a module param, 'cacheable' is no longer a valid fact name. |
| 74 |
seealso: |
| 75 |
- module: ansible.builtin.include_vars |
| 76 |
- ref: ansible_variable_precedence |
| 77 |
description: More information related to variable precedence and which type of variable wins over others. |
| 78 |
author: |
| 79 |
- Dag Wieers (@dagwieers) |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: Setting host facts using key=value pairs, this format can only create strings or booleans |
| 84 |
ansible.builtin.set_fact: one_fact="something" other_fact="{{ local_var }}" |
| 85 |
|
| 86 |
- name: Setting host facts using complex arguments |
| 87 |
ansible.builtin.set_fact: |
| 88 |
one_fact: something |
| 89 |
other_fact: "{{ local_var * 2 }}" |
| 90 |
another_fact: "{{ some_registered_var.results | map(attribute='ansible_facts.some_fact') | list }}" |
| 91 |
|
| 92 |
- name: Setting facts so that they will be persisted in the fact cache |
| 93 |
ansible.builtin.set_fact: |
| 94 |
one_fact: something |
| 95 |
other_fact: "{{ local_var * 2 }}" |
| 96 |
cacheable: yes |
| 97 |
|
| 98 |
- name: Creating list and dictionary variables |
| 99 |
ansible.builtin.set_fact: |
| 100 |
one_dict: |
| 101 |
something: here |
| 102 |
other: there |
| 103 |
one_list: |
| 104 |
- a |
| 105 |
- b |
| 106 |
- c |
| 107 |
# As of Ansible 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no') |
| 108 |
# to proper boolean values when using the key=value syntax, however it is still |
| 109 |
# recommended that booleans be set using the complex argument style: |
| 110 |
- name: Setting booleans using complex argument style |
| 111 |
ansible.builtin.set_fact: |
| 112 |
one_fact: yes |
| 113 |
other_fact: no |
| 114 |
|
| 115 |
- name: Creating list and dictionary variables using 'shorthand' YAML |
| 116 |
ansible.builtin.set_fact: |
| 117 |
two_dict: {'something': here2, 'other': somewhere} |
| 118 |
two_list: [1,2,3] |
| 119 |
""" |