ansible assign a fact value to a variable

3k views Asked by At

I am new to ansible, I am writing a small playbook where it has to collect the fact value from the destination host and use that as a variable within the play. Can someone help me how to do that.

---
- hosts: all
  gather_facts: True
  become: true
  become_method: sudo
  become_user: root
  vars:
    BUILD_PATH: /opt/services/dev


  pre_tasks:
   - setup:
      filter: ansible_env
   - set_fact:
       tag: "{{ ansible_env.DATA_AGGREGATOR_ENV }}"
   - debug: var=ENV

  tasks:
    - name: Copy to Build to DATA
      shell: cp -pr {{ BUILD_PATH }} {{ ENV }}

Note: DATA_AGGREGATOR_ENV is environmental variable defined in all servers and the value vary from one server to other.

1

There are 1 answers

4
ceving On

You set the variable tag, but you use the variable ENV.

You have to set the ENV variable, if you want to use the ENV variable.

- set_fact:
    ENV: "{{ ansible_env.DATA_AGG_ENV }}"

Or you have to use the tag variable, if you have set the tag variable.

    shell: cp -pr {{ BUILD_PATH }} {{ tag }}

btw: the filter line lacks a space.