Accessing file content via ansible

24 views
Skip to first unread message

Kunalsing Thakur

unread,
Jul 10, 2019, 11:02:17 AM7/10/19
to Ansible Project
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.

Kunalsing Thakur

unread,
Jul 10, 2019, 10:03:21 PM7/10/19
to Ansible Project
Hello ansible team can anyone suggest?

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0c5af08c-b9b6-41eb-856a-45bc662c4cf0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sudheer S

unread,
Jul 10, 2019, 11:26:47 PM7/10/19
to ansible...@googlegroups.com
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.

-

Sudheer S

Kunalsing Thakur

unread,
Jul 11, 2019, 8:43:15 AM7/11/19
to Ansible Project
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.
---

- hosts: all
  gather_facts: false
  connection: local
  vars:
    versions:
      httpd: 2.8
      apache: 2.4
  tasks:

  - name: Ansible Dictionary variable Example
    debug:
      msg: "rpmname is {{ item.key}} and value is {{item.value}}"
    with_dict: "{{ versions }}"


[test@4c594b5b8054 playbooks]$ ansible-playbook -vv test.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: test.yaml *********************************************************************************************
1 plays in test.yaml

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

TASK [Ansible Dictionary variable Example] **********************************************************************
task path: /etc/ansible/playbooks/test.yaml:10
ok: [127.0.0.1] => (item={'value': 2.4, 'key': u'apache'}) => {
    "msg": "rpmname is apache and value is 2.4"
}
ok: [127.0.0.1] => (item={'value': 2.8, 'key': u'httpd'}) => {
    "msg": "rpmname is httpd and value is 2.8"
}
META: ran handlers
META: ran handlers

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




--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Reply all
Reply to author
Forward
0 new messages