how to store value of ansible fact variable in another variable

8,740

You can use the set_fact module.

Here is an example....

- name: -> Apply common configuration to {{ target }} nodes
  hosts: "{{ target }}"
  gather_facts: True
  user: root

  pre_tasks:
   - setup:
      filter: ansible_local
   - set_fact:
       tag: "{{ ansible_local.sj.inventory.as_tag }}"
   - debug:
       var: tag
Share:
8,740

Related videos on Youtube

Huzefa
Author by

Huzefa

Updated on September 18, 2022

Comments

  • Huzefa
    Huzefa over 1 year

    I wanted to store value of ansible fact variable in some other variable, and also wanted to check is that fact variable is empty or not

    Below is my code where i can print the value of ansible_local fact variable but i wanted that value to store in another variable say xyz. And also wanted to check that ansible_local.sj.inventory.as_tag variable value is empty or not

    - name: -> Apply common configuration to {{ target }} nodes
      hosts: "{{ target }}"
      gather_facts: True
    
      user: root
    
      pre_tasks:
       - setup:
          filter: ansible_local
    
     tasks:
       - action: debug msg="{{ ansible_local.sj.inventory.as_tag }}"
    

    Sample O/P:

    TASK: [debug msg="{{ansible_local.sj.inventory.as_tag}}"]   
        ***********
    Monday 09 May 2016  09:48:49 -0700 (0:00:01.375)       0:00:02.785  
      ************
    ok: [abcserver] => {
      "msg": "abcd-123"