Re: [ansible-project] how do we read a set_fact variable of a dynamic host into different play localhost ?

2019-11-24 Thread Vladimir Botka
On Sun, 24 Nov 2019 04:01:16 -0800 (PST)
Shifa Shaikh  wrote:

> - name: Play 2
>   hosts: desthosts
>   tasks: 
> - set_fact:
> storerec: "{{storerec | default('') + 'Hello ' + inventory_hostname + 
> '\n'}}"
>   with_fileglob:
> - "{{ playbook_dir }}/tmpfiles/*"
> 
> - name: Play 3
>   hosts: localhost
>   tasks:
>- local_action: lineinfile line="{{ hostvars['dest_nodes']['storerec'] }}" 
> path="{{ playbook_dir }}/files/tricky.txt" create=yes
> [...]
> fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an 
> undefined variable. The error was: \"hostvars['desthosts']\" is 
> undefined\n

"To get the value of storerecords from desthost" use the *extract* filter. For
example

storerecords: "{{ groups['desthosts'] |
  map('extract', hostvars, 'storerec') |
  list }}"

Cheers,

-vlado

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20191124152359.754a4209%40gmail.com.


pgpdrs9yf0Gtx.pgp
Description: OpenPGP digital signature


[ansible-project] how do we read a set_fact variable of a dynamic host into different play localhost ?

2019-11-24 Thread Shifa Shaikh


Here is my requirement:

   - 
   
   I have a playbook where I add the dynamic host to group "desthosts" in 
   [Play 1]
   - 
   
   In [Play 2] desthosts group I append a set_fact variable "storerecords".
   - 
   
   In [Play 3] I try to read set_fact "storerecords" of Play 2 however, I get 
   undefined 'storerecords' variable error.
   
Below is my playbook.

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

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

- name: Play 3
  hosts: localhost
  tasks:
   - local_action: lineinfile line="{{ hostvars['dest_nodes']['storerec'] }}" 
path="{{ playbook_dir }}/files/tricky.txt" create=yes

Playbook run command:

ansible-playbook test.yml -e SourceHost="10.9.9.56,10.9.9.63,10.9.9.33" 

Error running the playbook is:
TASK [lineinfile] 

 
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an 
undefined variable. The error was: \"hostvars['desthosts']\" is 
undefined\n\nThe error appears to be in '/app/test.yml': line 28, column 7, 
but may\nbe elsewhere in the file depending on the exact syntax 
problem.\n\nThe offending line appears to be:\n\n tasks:\n - local_action: 
lineinfile line=\"{{ hostvars['desthosts']['storerec'] }}\" 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 from 
desthost to Play 3?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/147edb97-1c14-4059-bf35-3dca35861c1a%40googlegroups.com.