# copy registered content to a file which is called from variable

**URL:** <https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331>\
**Category:** Ansible Project\
**Created:** [November 10, 2022, 2:16pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331 "2022-11-10T14:16:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 10, 2022, 2:16pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/1 "2022-11-10T14:16:07Z")

</div>

Hi,

I am trying to copy the contents from a register output to a file in the local server.  
However , I have challenges in re-calling the variable filename in the copy command.

Correct me what I am missing. in dest=“{{ vars[{{ comp\_file }}] }}” or “{{ compare\_out.stdout }}” in the last lines …

[vee@server]$ grep comp\_file vars/postcheck\_vars.yml  
comp\_file: /logs/{{ exec\_date }}/{{ inventory\_hostname }}-compare-{{{ exec\_date }}.txt

[vee@server]$ cat compare\_sanity.yml

- name: comparing file1 and file2  
local\_action: command /usr/bin/sdiff “{{ logs\_dir }}”{{ inventory\_hostname }}-Pre-{{ exec\_date }}.txt “{{ logs\_dir }}”{{ inventory\_hostname }}-Post-{{ exec\_date }}.txt  
register: compare\_out  
ignore\_errors: yes  
no\_log: True

- name: create output file  
local\_action:  
copy content=“{{ compare\_out.stdout }}” dest=“{{ vars[{{ comp\_file }}] }}”  
[vee@server]$

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 10, 2022, 4:54pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/2 "2022-11-10T16:54:39Z")

</div>

A couple of things. First,

> comp\_file: /logs/{{ exec\_date }}/{{ inventory\_hostname }}-compare-{{{ exec\_date }}.txt

has three left braces on “exec\_date”.

Second, mustaches (i.e. “{{” … “}}”) never nest. So

> copy content=“{{ compare\_out.stdout }}” dest=“{{ vars[{{ comp\_file }}] }}”

should be

> copy content=“{{ compare\_out.stdout }}” dest=“{{ vars[comp\_file] }}”

Well, at least that’s “dest=” without nested mustaches. Whether it’s right or not I don’t know, but it’s less wrong this way.

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 10, 2022, 6:26pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/3 "2022-11-10T18:26:40Z")

</div>

Thanks for pointing as i did notice that , and I have corrected the three left braces on “exec\_date”.

Second, mustaches (i.e. “{{” … “}}”) never nest \<\< Yes , I tried earlier that and even now after reverting back , still the same issue …  
Execution error for the play below.

fatal: [server1 → localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute u’/home/user1/ansible/patching/logs/2022-11-10/server1-compare-2022-11-10.txt’\n\nThe error appears to be in ‘/home/unixops/ansible/linux\_patching/compare\_sanity.yml’: line 7, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: create compare file using precheck and postcheck sanity checks\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 10, 2022, 6:43pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/4 "2022-11-10T18:43:38Z")

</div>

Attaching a screenshot to show the format still miss something

 ![IMG-20221111-WA0000.jpg](https://us1.discourse-cdn.com/ansible/original/2X/0/048d7084417bf20945597ba6400b7c0a3e9978f7.jpeg)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 10, 2022, 8:04pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/5 "2022-11-10T20:04:05Z")

</div>

Wait a second. You’re saying “{{ vars[comp\_file] }}”, but

comp\_file=“/home/user1/ansible/patching/logs/2022-11-10/server1-compare-2022-11-10.txt”

So you’re trying to pull a value from the “vars” dict with the key of  
“/home/user1/ansible/patching/logs/2022-11-10/server1-compare-2022-11-10.txt”.

Whatever “vars” is, it doesn’t have any data with that name.

Perhaps you mean this:

dest=“{{ comp\_file }}”

Whatever. In any case, if I were you, I’d throw in a debug step and print out the values of “compare\_out.stdout” and, er, whatever you think “dest” is supposed to be (“vars”? but I think you just mean “comp\_file”).

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 10, 2022, 9:43pm UTC](https://forum.ansible.com/t/copy-registered-content-to-a-file-which-is-called-from-variable/37331/6 "2022-11-10T21:43:05Z")

</div>

Whatever “vars” is, it doesn’t have any data with that name - exactly… it will be a new file to be created by the copy’s “dest .”

compare\_out.stdout is having the output of hundreds of lines to be printed parallely . I have disabled the printing of its output (to reduce screen space) by  
register: compare\_out  
no\_log: true
