How do i get set_fact variable in another play ?

24 views
Skip to first unread message

Mohtashim S

unread,
Nov 20, 2019, 6:21:01 AM11/20/19
to Ansible Project
I have a playbook where i first [Play 1] add dynamic host to group "desthosts" 

In Play 2 I append a string variable "storerecords" with filenames. Sample playbook below:

In Play 3 I try to remove duplicate entries in "storerecords" variable where i get the error.

---
- name: Play 1
  hosts
: localhost
   
- addhost:
          name
: "{{ item }}"
         
group: desthosts
      with_items
:
         
- "{{ SourceFiles.split(',') }}"
 


- name: Play 2
  hosts
: desthosts
   
- set_fact:
        storerecords
: "{{ storerecords  | default('') + item + '\n' }}"
      with_fileglob
:       - "{{ playbook_dir }}/tmpfiles/*"




- name: Play 3
  hosts
: localhost
   
- local_action: lineinfile line="{{ storerecords }}" path="{{ playbook_dir }}/files/tricky.txt" create=yes

   
- set_fact:
        storerecords
: "{{ storerecords.split('\n') | unique | select | join('\n') }}"


Error running the playbook is:

TASK [lineinfile] *******************************************************************************************************************************************
fatal
: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'storerecords' is undefined\n\nThe error appears to be in '/app/scripts/deploy.yml': line 1277, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - local_action: lineinfile line=\"{{ storerecords }}\" path=\"{{ playbook_dir }}/files/tricky.txt\" create=yes\n     ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}


Can you please suggest how I can get the value of storerecords in from desthost to Play 3 localhost ?


Reply all
Reply to author
Forward
0 new messages