Ansible - Registered Variable Usage In Loop -


i have git update 2 different items (mango , apple) , in next step display each individual checkout's latest versions.

- name: checkout mango , apple   git:     repo: ssh://user@gerrit.adap.tv:26545/{{ item }}.git     dest: "{{ scriptdir }}/{{ item }}"     key_file: "{{ rsa_key }}"     accept_hostkey: yes     update: yes   with_items:     - mango     - apple   register: checkoutversion   - name: display checkoutversion   debug: msg="checkout version {{ item.name }}"   with_items:     - checkoutversion.after 

code output :-

task: [makepkg | checkout mango , apple ] **********************************************  ok: [127.0.0.1] => (item=mango) => {"after": "d3aa6131ec1a2e73f69ee150", "before": "d3aa6131ec1a2e73f69ee150", "changed": false, "item": "mango"}  ok: [127.0.0.1] => (item=primeribs) => {"after": "f9bb03466f248a9eb92c9656", "before": "f9bb03466f248a9eb92c9656", "changed": false, "item": "apple"}  task: [makepkg | display checkoutversion] ********************************* fatal: [127.0.0.1] => 1 or more undefined variables: 'str object' has no attribute 'item'  fatal: hosts have failed -- aborting 

please suggest how can print each individual item's latest version.

i find best thing in case write small test case see how ansible behaves. example:

- hosts: localhost   gather_facts: false   tasks:     - command: /bin/echo {{ item }}      register: foo       with_items:         - 1         - 2         - 3      - debug: var=foo 

the output of above playbook shows how ansible stores results of loop in variable foo:

ok: [localhost] => {     "foo": {         "changed": true,         "msg": "all items completed",         "results": [             {                 "changed": true,                 "cmd": [                     "/bin/echo",                     "one"                 ],                 "delta": "0:00:00.027182",                 "end": "2015-08-19 13:13:25.216657",                 "invocation": {                     "module_args": "/bin/echo one",                     "module_name": "command"                 },                 "item": "one",                 "rc": 0,                 "start": "2015-08-19 13:13:25.189475",                 "stderr": "",                 "stdout": "one"             },             {                 "changed": true,                 "cmd": [                     "/bin/echo",                     "two"                 ],                 "delta": "0:00:00.006270",                 "end": "2015-08-19 13:13:25.509316",                 "invocation": {                     "module_args": "/bin/echo two",                     "module_name": "command"                 },                 "item": "two",                 "rc": 0,                 "start": "2015-08-19 13:13:25.503046",                 "stderr": "",                 "stdout": "two"             },             {                 "changed": true,                 "cmd": [                     "/bin/echo",                     "three"                 ],                 "delta": "0:00:00.006347",                 "end": "2015-08-19 13:13:25.763675",                 "invocation": {                     "module_args": "/bin/echo three",                     "module_name": "command"                 },                 "item": "three",                 "rc": 0,                 "start": "2015-08-19 13:13:25.757328",                 "stderr": "",                 "stdout": "three"             }         ]     } } 

so actual results provided in list foo.results. if change debug task can iterate through these results 1 one:

  - debug: var=item     with_items: foo.results 

this returns following:

ok: [localhost] => (item={u'stdout': u'one', u'changed': true, u'end': u'2015-08-19 13:17:39.884374', 'item': 'one', u'cmd': [u'/bin/echo', u'one'], u'rc': 0, u'start': u'2015-08-19 13:17:39.878585', u'stderr': u'', u'delta': u'0:00:00.005789', 'invocation': {'module_name': u'command', 'module_args': u'/bin/echo one'}}) => {     "item": {         "changed": true,         "cmd": [             "/bin/echo",             "one"         ],         "delta": "0:00:00.005789",         "end": "2015-08-19 13:17:39.884374",         "invocation": {             "module_args": "/bin/echo one",             "module_name": "command"         },         "item": "one",         "rc": 0,         "start": "2015-08-19 13:17:39.878585",         "stderr": "",         "stdout": "one"     } } ok: [localhost] => (item={u'stdout': u'two', u'changed': true, u'end': u'2015-08-19 13:17:40.137575', 'item': 'two', u'cmd': [u'/bin/echo', u'two'], u'rc': 0, u'start': u'2015-08-19 13:17:40.131803', u'stderr': u'', u'delta': u'0:00:00.005772', 'invocation': {'module_name': u'command', 'module_args': u'/bin/echo two'}}) => {     "item": {         "changed": true,         "cmd": [             "/bin/echo",             "two"         ],         "delta": "0:00:00.005772",         "end": "2015-08-19 13:17:40.137575",         "invocation": {             "module_args": "/bin/echo two",             "module_name": "command"         },         "item": "two",         "rc": 0,         "start": "2015-08-19 13:17:40.131803",         "stderr": "",         "stdout": "two"     } } ok: [localhost] => (item={u'stdout': u'three', u'changed': true, u'end': u'2015-08-19 13:17:40.368420', 'item': 'three', u'cmd': [u'/bin/echo', u'three'], u'rc': 0, u'start': u'2015-08-19 13:17:40.362533', u'stderr': u'', u'delta': u'0:00:00.005887', 'invocation': {'module_name': u'command', 'module_args': u'/bin/echo three'}}) => {     "item": {         "changed": true,         "cmd": [             "/bin/echo",             "three"         ],         "delta": "0:00:00.005887",         "end": "2015-08-19 13:17:40.368420",         "invocation": {             "module_args": "/bin/echo three",             "module_name": "command"         },         "item": "three",         "rc": 0,         "start": "2015-08-19 13:17:40.362533",         "stderr": "",         "stdout": "three"     } } 

so suggest first change debug task initially:

- name: display checkoutversion   debug: var=checkoutversion 

use see sort of output git module provides, expand on want.

given output did provide above, final debug statement you'll want along these lines:

- name: display checkoutversion   debug: msg="checkout version {{ item.after }}"   with_items: checkoutversion.results 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -