2

I am referring this question: Using Ansible set_fact to create a dictionary from register results

   - debug:                                         
       msg:                                         
        - "proc: '{{ item.ansible_facts.process }}'"
        - "own: '{{ item.ansible_facts.owner }}'"   
        - "stat: '{{ item.ansible_facts.state }}'"  
      with_items: '{{ taglr.results }}'              

   - set_fact:
        _services: " {{ _services|default([]) + [ {'proc': item.ansible_facts.process, 'own': item.ansible_facts.owner ,'stat': item.ansible_facts.state}  ]  }}"
     with_items: '{{ taglr.results }}'

the taglr.results contents are as follows

taglr: {
"results": [
        {
            "_ansible_item_label": "O3_RBT", 
            "_ansible_no_log": false, 
            "ansible_loop_var": "item", 
            "changed": true, 
            "cmd": "clustat | grep O3_RBT | grep -v Status | awk '{print $1\"--\"$2\"--\"$3}' | awk -F ':' '{print $2}' | sed 's/(\\|)//g'", 
            "delta": "0:00:00.039447", 
            "end": "2020-10-06 13:13:48.782631", 
            "failed": false, 
            "invocation": {
                "module_args": {
                    "_raw_params": "clustat | grep O3_RBT | grep -v Status | awk '{print $1\"--\"$2\"--\"$3}' | awk -F ':' '{print $2}' | sed 's/(\\|)//g'", 
                    "_uses_shell": true, 
                    "argv": null, 
                    "chdir": null, 
                    "creates": null, 
                    "executable": null, 
                    "removes": null, 
                    "stdin": null, 
                    "stdin_add_newline": true, 
                    "strip_empty_ends": true, 
                    "warn": true
                }
            }, 
            "item": "O3_RBT", 
            "rc": 0, 
            "start": "2020-10-06 13:13:48.743184", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "O3_RBT--rbt_daemon--disabled", 
            "stdout_lines": [
                "O3_RBT--rbt_daemon--disabled"
            ]
        }, 
        {
            "_ansible_item_label": "RBTTOMCAT", 
            "_ansible_no_log": false, 
            "ansible_loop_var": "item", 
            "changed": true, 
            "cmd": "clustat | grep RBTTOMCAT | grep -v Status | awk '{print $1\"--\"$2\"--\"$3}' | awk -F ':' '{print $2}' | sed 's/(\\|)//g'", 
            "delta": "0:00:00.032282", 
            "end": "2020-10-06 13:13:49.260344", 
            "failed": false, 
            "invocation": {
                "module_args": {
                    "_raw_params": "clustat | grep RBTTOMCAT | grep -v Status | awk '{print $1\"--\"$2\"--\"$3}' | awk -F ':' '{print $2}' | sed 's/(\\|)//g'", 
                    "_uses_shell": true, 
                    "argv": null, 
                    "chdir": null, 
                    "creates": null, 
                    "executable": null, 
                    "removes": null, 
                    "stdin": null, 
                    "stdin_add_newline": true, 
                    "strip_empty_ends": true, 
                    "warn": true
                }
            }, 
            "item": "RBTTOMCAT", 
            "rc": 0, 
            "start": "2020-10-06 13:13:49.228062", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "RBTTOMCAT--rbt_daemon--started", 
            "stdout_lines": [
                "RBTTOMCAT--rbt_daemon--started"
            ]
        }
    ]
}

The debug task works as expected:

ok: [localhost] => {
    "msg": [
        "proc: 'O3_RBT'",
        "own: 'rbt_daemon'",
        "stat: 'disabled'"
    ]
}
ok: [localhost] => {
    "msg": [
        "proc: 'RBTTOMCAT'",
        "own: 'rbt_daemon'",
        "stat: 'started'"
    ]
}

But set_fact is throwing this error:

fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ( {{ _services|default([]) + [ {'proc': item.ansible_facts.process, 'own': item.ansible_facts.owner ,'stat': item.ansible_facts.state}  ]
  }}): coercing to Unicode: need string or buffer, list found"}

Whereas example, given in Using Ansible set_fact to create a dictionary from register results, works perfectly fine.

Ansible_version: ansible 2.9.10

Python_version: 2.7.5

Can anyone suggest where are the issue and any links to follow?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
neatsdude
  • 21
  • 3
  • 3
    because you do have a space in front of your `set_fact`, this ends up confusing Ansible with a space character in the variable: `_services: " {{ _services|default([]) (...)` just remove this extra space: `_services: "{{ _services|default([]) (...)` to see if it fixes it for you – β.εηοιτ.βε Oct 06 '20 at 11:07
  • 1
    Hey @β.εηοιτ.βε... It worked. It is the learning, will keep in mind next time. Many Thanks. – neatsdude Oct 06 '20 at 12:06

0 Answers0