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
On Thu, Jul 11, 2019 at 8:56 AM Sudheer S <[email protected]> wrote:
>
> On 10/07/19 8:32 PM, Kunalsing Thakur wrote:
> > 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.
> >
> >
> 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
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/9d0260ae-ee54-179c-2da6-cdf0fd565843%40techchorus.net
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
[image: --]
kunalsing thakur
[image: https://]about.me/kunalsingthakur
<https://about.me/kunalsingthakur?promo=email_sig&utm_source=email_sig&utm_medium=email_sig&utm_campaign=external_links>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CANErU6m%2BafGv%3DvJE1jySX1ROfj6No%3Dr3499K9wkipacizZi9Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.