why lineinfile not able to run with item.path

Code==>

  • name: insert logggin file in log4j protperties

lineinfile:

path: ‘{{ item.path }}’

insertafter: ‘log4j.appender.kafkaAppender.layout.ConversionPattern=*’

line: ‘{{ item.line }}’

with_items:

  • { path: ‘/etc/kafka/confluent-6.2.10/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxFileSize=512MB’ }

  • { path: ‘/etc/kafka/confluent-7.2.5/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxFileSize=512MB’ }

  • { path: ‘/etc/kafka/confluent-6.2.10/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxBackupIndex=10’ }

  • { path: ‘/etc/kafka/confluent-7.2.5/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxBackupIndex=10’ }

ERROR==>>>

item={‘path’: ‘/etc/kafka/confluent-7.2.5/zookeeper/log4j.properties’, ‘line’: ‘log4j.appender.kafkaAppender.MaxBackupIndex=10’}) => {“ansible_loop_var”: “item”, “changed”: false, “item”: {“line”: “log4j.appender.kafkaAppender.MaxBackupIndex=10”, “path”: “/etc/kafka/confluent-7.2.5/zookeeper/log4j.properties”}, “msg”: “Destination { item.path }} does not exist !”, “rc”: 257}

It seems that you have a typo since the error message reads ‘Destination { item.path }} does not exist !’ which is missing a starting curling bracket.

Your playbook works fine though.

You need proper indentation. The module name and “with_items” must align with the task name. The module args would then outdent by two space to also reflect proper indentation. I used a “debug” vs “lineinfile” to display the item.path and item.line vars and it worked just fine once indentation was corrected.

  • name: insert logggin file in log4j protperties
    lineinfile:
    path: ‘{{ item.path }}’
    insertafter: ‘log4j.appender.kafkaAppender.layout.ConversionPattern=*’
    line: ‘{{ item.line }}’
    with_items:
  • { path: ‘/etc/kafka/confluent-6.2.10/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxFileSize=512MB’ }
  • { path: ‘/etc/kafka/confluent-7.2.5/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxFileSize=512MB’ }
  • { path: ‘/etc/kafka/confluent-6.2.10/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxBackupIndex=10’ }
  • { path: ‘/etc/kafka/confluent-7.2.5/kafka/log4j.properties’, line: ‘log4j.appender.kafkaAppender.MaxBackupIndex=10’ }

Walter

Exaclty. The error is misleading as you can see my code its straightforward {{ before item.pah. Anyways i m trying what MR. Walter has suggested. Lets c. Thanks by the way for your response.

Hello MR. Walter,

Thanks a lot. My code and understanding was fine but as you pointed out i somehow couldn’t see space in front. And as we know ansible is not good for error prompt i mislead it.

Thanks again. it worked by add 2 spaces in with_items.

By the way any good tool or editor you can recommend which auto corrects or helps with this??

Also could you please tell me what did u do for troubleshooting is it only , debug or something else so that i ll try to do next time.

Documentation in various sites have a problem with with_items , they all start the item list from the w of the with_items , the indentation is almost incorrect in all majority of the knowledge sites for the list of items with variables.

Please feel free to improve the error reporting if you feel it is necessary. :slight_smile:

Except rol sites(that many don’t have access) I don’t think other sites that google hit take the indentation into account for the examples…it’s up to us…

It is always upto us.