# Accessing file content via ansible

**URL:** <https://forum.ansible.com/t/accessing-file-content-via-ansible/30745>\
**Category:** Ansible Project\
**Created:** [July 10, 2019, 3:02pm UTC](https://forum.ansible.com/t/accessing-file-content-via-ansible/30745 "2019-07-10T15:02:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [July 10, 2019, 3:02pm UTC](https://forum.ansible.com/t/accessing-file-content-via-ansible/30745/1 "2019-07-10T15:02:16Z")

</div>

file.txt

rpmname: version  
rpmname: version

Playbook:-

- hosts: all  
gather\_facts: false

tasks:

- name: store the value  
shell: cat /etc/ansible/playbooks/file.txt  
register: versions
- name: Ansible Dictionary variable Example  
debug:  
msg: “rpmname is {{ item.key}} and value is {{item.value}}”

i want to access multiple key values in ansible -playbook to perform operation. looks like something is missing to access that values.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [July 11, 2019, 2:02am UTC](https://forum.ansible.com/t/accessing-file-content-via-ansible/30745/2 "2019-07-11T02:02:50Z")

</div>

Hello ansible team can anyone suggest?

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [July 11, 2019, 3:26am UTC](https://forum.ansible.com/t/accessing-file-content-via-ansible/30745/3 "2019-07-11T03:26:35Z")

</div>

Where did 'item' come from? Usually, when you use a task in a loop, you can access the loop item via the variable 'item'. I don't see a loop in your playbook.

In your example, look at versions.stdout for file contents. Or use the Slurp module to read file contents.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [July 11, 2019, 12:42pm UTC](https://forum.ansible.com/t/accessing-file-content-via-ansible/30745/4 "2019-07-11T12:42:55Z")

</div>

Hi Sudheer,

Thats the problem now how should we iterate items.

[test@4c594b5b8054 playbooks]$ ansible-playbook -vv test3.yaml  
ansible-playbook 2.8.1  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/home/test/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/site-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]  
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: test3.yaml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in test3.yaml

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers

TASK [store the value] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/playbooks/test3.yaml:5  
changed: [127.0.0.1] =\> {“ansible\_facts”: {“discovered\_interpreter\_python”: “/usr/bin/python”}, “changed”: true, “cmd”: “cat /etc/ansible/playbooks/file.txt”, “delta”: “0:00:01.326287”, “end”: “2019-07-11 12:33:51.894849”, “rc”: 0, “start”: “2019-07-11 12:33:50.568562”, “stderr”: “”, “stderr\_lines”: , “stdout”: “httpd: 2.4\napache: 2.8”, “stdout\_lines”: [“httpd: 2.4”, “apache: 2.8”]}

TASK [Ansible Dictionary variable Example] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/playbooks/test3.yaml:8  
fatal: [127.0.0.1]: FAILED! =\> {“msg”: “with\_dict expects a dict”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
127.0.0.1 : ok=1 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

in this example its required the content should be dictionary format.

But if you take vars in anisbleplaybook itself it will work because vars is in dict format.
