<p>I need to get node name and IP address of each node and then create dictionary object.  I am able to get Kubernetes node list using below command   </p>

<pre><code> - hosts: k8s

  tasks:
  - name: get cluster nodes 
    shell: "kubectl get nodes -o wide --no-headers | awk '{ print $1 ,$7}'"
    register: nodes

  - debug: var=nodes

  - set_fact:
      node_data: {}  

  - name: display node name
    debug:
      msg: "name is {{item.split(' ').0}}"
    with_items: "{{nodes.stdout_lines}}"

  - set_fact:
      node_data: "{{ node_data | combine ( item.split(' ').0 : { 'name': item.split(' ').0 , 'ip' : item.split(' ').1 }, recursive=True) }}"
    with_items: "{{ nodes.stdout_lines }}"

  - debug: var=node_data
</code></pre>

<p>I got below error:</p>

<blockquote>
  <p>FAILED! => {"msg": "template error while templating string: expected
  token ',', got ':'. String: {{ node_data | combine ( item.split(' ').0
  : { 'name':item.split(' ').0 , 'ip': item.split(' ').1 },
  recursive=True) }}"}</p>
</blockquote>

<p>Output of kubectl command given below</p>

<pre><code>kubectl get nodes -o wide --no-headers | awk '{ print $1 ,$7}'
</code></pre>

<p>is as follows</p>

<pre><code>&gt; ip-192-168-17-93.ec2.internal 55.175.171.80
&gt; ip-192-168-29-91.ec2.internal 3.23.224.95
&gt; ip-192-168-83-37.ec2.internal 54.196.19.195
&gt; ip-192-168-62-241.ec2.internal 107.23.129.142
</code></pre>

<p>How to get the nodename and ip address into dictionary object in ansible?</p>

Samselvaprabu
<p>The first argument to the <code>combine</code> filter must be a dictionary. You're calling:</p>

<pre><code>  - set_fact:
      node_data: "{{ node_data | combine ( item.split(' ').0 : { 'name': item.split(' ').0 , 'ip' : item.split(' ').1 }, recursive=True) }}"
    with_items: "{{ nodes.stdout_lines }}"
</code></pre>

<p>You need to make that:</p>

<pre><code>  - set_fact:
      node_data: "{{ node_data | combine ({item.split(' ').0 : { 'name': item.split(' ').0 , 'ip' : item.split(' ').1 }}, recursive=True) }}"
    with_items: "{{ nodes.stdout_lines }}"
</code></pre>

<p>Note the new <code>{...}</code> around your first argument to <code>combine</code>. You might want to consider reformatting this task for clarity, which might make this sort of issue more obvious:</p>

<pre><code>    - set_fact:
        node_data: &gt;-
          {{ node_data | combine ({
            item.split(' ').0: {
              'name': item.split(' ').0,
              'ip': item.split(' ').1
            },
          }, recursive=True) }}
      with_items: "{{ nodes.stdout_lines }}"
</code></pre>

<hr>

<p>You could even make it a little more clear by moving the calls to <code>item.split</code> into a <code>vars</code> section, like this:</p>

<pre><code>    - set_fact:
        node_data: &gt;-
          {{ node_data | combine ({
            name: {
              'name': name,
              'ip': ip
            },
          }, recursive=True) }}
      vars:
        name: "{{ item.split(' ').0 }}"
        ip: "{{ item.split(' ').1 }}"
      with_items: "{{ nodes.stdout_lines }}"
</code></pre>
