# Read a file content as a variable

**URL:** <https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615>\
**Category:** Ansible Project\
**Created:** [October 5, 2013, 4:19am UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615 "2013-10-05T04:19:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yapeng\_Wu](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yapeng\_Wu](https://forum.ansible.com/u/Yapeng_Wu)\
**Post date:** [October 5, 2013, 4:19am UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/1 "2013-10-05T04:19:08Z")

</div>

Hello, I am new to Ansible.

I am trying to figure out if I could do the following step:

1. run command A; A will generate a file:B
2. read content B
3. copy a template file C to another location
4. change content C with content B read from step 2.

I know how to do 1 and 3 in ansible. But I don’t know if I could do 2 and 4?

Thanks,  
Yapeng

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [October 5, 2013, 7:22pm UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/2 "2013-10-05T19:22:42Z")

</div>

you can use the command modules to read the file using what ever current method you are currently using to read the file, otherwise you can check which file module is closer to the functionality you need for example is it to be read by a version control system, then it is git, or is it to be read to be processed then template or read to be copied then it is fetch or copy. you need to let us know the original problem and describe it well, your description is very generic and a bit vague

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 5, 2013, 8:05pm UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/3 "2013-10-05T20:05:23Z")

</div>

1. To read a file back to the original server you can use the “fetch” module. If you just want the contents in a variable you can use “slurp” instead.

2. Sounds like here you just want to use the “copy” module with either a filename (if fetch) or content= (if slurp).

---

<div class="post-metadata">

**Author:** ![Yapeng\_Wu](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yapeng\_Wu](https://forum.ansible.com/u/Yapeng_Wu)\
**Post date:** [October 6, 2013, 2:00am UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/4 "2013-10-06T02:00:31Z")

</div>

Thank you, Walid and Michael,

Let me elaborate more in detail:

1. run a command: A, A will generate file B, B content is simple, just a URI, i.e. [http://xxxx.yyy.zzz/aaa](http://xxxx.yyy.zzz/aaa)
2. I want to read B and save the content as a variable D.
3. update file C in another location. C is a configuration file, there is a field: “uri”, I want to update this file C as a template and field “uri” is updated with variable D, i.e. “uri = {{ D }}”

I will read more “fetch” and “slurp”, but from my brief reading, I think I could not save the content of fetch file as variable then pass back to template in the same YAML file.

Sorry I am new learner, I asked a lot of dumb questions.

Yapeng

---

<div class="post-metadata">

**Author:** ![Yapeng\_Wu](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yapeng\_Wu](https://forum.ansible.com/u/Yapeng_Wu)\
**Post date:** [October 6, 2013, 3:12am UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/5 "2013-10-06T03:12:40Z")

</div>

Thanks guys, I got answer from IRC channel.

Using command “cat” and “register” solves my puzzle.

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [October 6, 2013, 4:21am UTC](https://forum.ansible.com/t/read-a-file-content-as-a-variable/13615/6 "2013-10-06T04:21:57Z")

</div>

That is what I like about Ansible, if you do not know how to do it the Ansible way, try to use the command modules and do it the same way you are doing now, you can add register, ignore\_error, and created flag for better handling and relationship with other surrounding tasks
