Ansible Module set fact: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 10: Line 10:
=Overview=
=Overview=


This module sets new variables. The variables are set on a host-by-host basis just like facts discovered by the setup module and will be available to subsequent plays during an ansible-playbook run.
This module sets new [[Ansible_Concepts#Fact|facts]] (variables). The variables are set on a host-by-host basis just like facts discovered by the setup module and will be available to subsequent plays during an ansible-playbook run.


Set cacheable to yes to save variables across executions using a fact cache. Variables created with set_fact have different precedence depending on whether they are or are not cached. Per the standard Ansible variable precedence rules, many other types of variables have a higher priority, so this value may be overridden.
Set cacheable to yes to save variables across executions using a fact cache. Variables created with set_fact have different precedence depending on whether they are or are not cached. Per the standard Ansible variable precedence rules, many other types of variables have a higher priority, so this value may be overridden.
Line 18: Line 18:
   set_fact:
   set_fact:
     bashrc_d: "{{ ansible_env.HOME }}/.bashrc.d"
     bashrc_d: "{{ ansible_env.HOME }}/.bashrc.d"
</syntaxhighlight>
=Setting Command Output as a Fact=
{{Internal|Ansible_Module_command#Setting_Command_Output_as_a_Fact|Setting Command Output as a Fact}}
=Setting a Fact by Processing the Content of a Variable=
<syntaxhighlight lang='yaml'>
- name: IDEA applications
  set_fact:
    idea_apps: "{{ intellij_apps | to_json | from_json | json_query('files[?contains(path, `/apps/IDEA-`) == `true`]') | list }}"
</syntaxhighlight>
=Setting the Fact Only if the Variable Does Not Exist=
<syntaxhighlight lang='yaml'>
- name:
  when: some_var is not defined
  set_fact:
    some_var: "some value"
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 21:55, 6 July 2021

External

Internal

Overview

This module sets new facts (variables). The variables are set on a host-by-host basis just like facts discovered by the setup module and will be available to subsequent plays during an ansible-playbook run.

Set cacheable to yes to save variables across executions using a fact cache. Variables created with set_fact have different precedence depending on whether they are or are not cached. Per the standard Ansible variable precedence rules, many other types of variables have a higher priority, so this value may be overridden.

- name: Set bashrc.d directory path
  set_fact:
    bashrc_d: "{{ ansible_env.HOME }}/.bashrc.d"

Setting Command Output as a Fact

Setting Command Output as a Fact

Setting a Fact by Processing the Content of a Variable

- name: IDEA applications
  set_fact:
    idea_apps: "{{ intellij_apps | to_json | from_json | json_query('files[?contains(path, `/apps/IDEA-`) == `true`]') | list }}"

Setting the Fact Only if the Variable Does Not Exist

- name:
  when: some_var is not defined
  set_fact:
    some_var: "some value"