unable to copy ansible vriable into file

Error:

TASK [lineinfile] ***************************************************************************************************************************
fatal: [192.168.1.158]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute u’0’\n\nThe error appears to be in ‘/home/cloud/nidhi/ccepbook/nidhi-playbook.yml’: line 31, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - local_action: lineinfile line="{{ result.results[item].container.Config.Hostname }}" dest=/tmp/abhay.csv\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}

  • local_action: lineinfile line=“{{ result.results[item].container.Config.Hostname }}” dest=/tmp/abhay.csv
    with_sequence: start=0 end=“{{ count|int }}”

here count is from 0 to 2

The error is telling you exactly what the problem is. You need to work out what might be causing that problem.

In this case, a list object does not have an attribute called (in Unicode) ‘0’.

Your loop uses values from a sequence, and the first in the sequence is ‘0’. This is used to dereference the variable result.results (as in, result.results[item]…).

I’m guessing that result.results is an empty list, OR maybe you should cast item to be an integer:

{{ result.results[item |int].container.Config.Hostname }}

One option would be to output result in a debug statement before you try to use it, so you can look at its structure and then dereference it properly.

BTW this is guesswork. I haven’t read any doco or tested any of the above.

Regards, K.

Thanks its work.