# Template with to\_ini in a loop: 'str' object has no attribute 'items'

**URL:** <https://forum.ansible.com/t/template-with-to-ini-in-a-loop-str-object-has-no-attribute-items/11007>\
**Category:** Get Help\
**Tags:** ini, template, variables, ee\
**Created:** [November 19, 2024, 8:44pm UTC](https://forum.ansible.com/t/template-with-to-ini-in-a-loop-str-object-has-no-attribute-items/11007 "2024-11-19T20:44:29Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 19, 2024, 9:34pm UTC](https://forum.ansible.com/t/template-with-to-ini-in-a-loop-str-object-has-no-attribute-items/11007/7 "2024-11-19T21:34:43Z")

</div>

Actually ignore what I wrote above, since the `ini` files have sections I think you need a more complicated template? I have [one like this](https://git.coop/webarch/php/-/blob/main/templates/php.j2):

```auto
; {{ ansible_managed }}
{% if php_conf_file_proposed_sections | bool %}
{% for php_section in php_conf_file_proposed_vars | dict2items %}
[{{ php_section.key }}]
{% for php_variable_pair in php_section.value | dict2items %}
{% if php_conf_file_no_extra_spaces | bool %}
{{ php_variable_pair.key }}={{ php_variable_pair.value }}
{% else %}
{{ php_variable_pair.key }} = {{ php_variable_pair.value }}
{% endif %}
{% endfor %}
{% endfor %}
{% else %}
{% for php_conf_pair in php_conf_file_proposed_vars | ansible.builtin.dict2items %}
{% if php_conf_file_no_extra_spaces | bool %}
{{ php_conf_pair.key }}={{ php_conf_pair.value }}
{% else %}
{{ php_conf_pair.key }} = {{ php_conf_pair.value }}
{% endif %}
{% endfor %}
{% endif %}
; vim: syntax=dosini

```

To generate `ini` files from lists like this:

```yaml
  - name: PHP 8.3 configuration
    version: "8.3"
    state: present
    files:
      - name: PHP 8.3 CLI configuration
        path: /etc/php/8.3/cli/php.ini
        state: present
        conf:
          apc:
            "apc.coredump_unmap": "0"
            "apc.enable_cli": "1"
            "apc.enabled": "1"
            "apc.entries_hint": "4096"
            "apc.gc_ttl": "3600"
            "apc.serializer": "php"
            "apc.shm_segments": "1"
            "apc.shm_size": "32M"
            "apc.slam_defense": "1"
            "apc.ttl": "0"
            "apc.use_request_time": "0"
          "CLI Server":
            "cli_server.color": "1"
          MySQLi:
            "mysqli.allow_local_infile": "0"
            "mysqli.allow_persistent": "1"
            "mysqli.default_port": '3306'
            "mysqli.max_links": "-1"
            "mysqli.max_persistent": "-1"
            "mysqli.reconnect": "0"
            "mysqli.default_socket": "/run/mysqld/mysqld.sock"

```

However when I wrote this role `community.general.to_ini` didn’t exist… so perhaps all the above can be ignored!

I posted some links to `ini` resources for Ansible here, I don’t know if these will help:

> [@Call to action: Send us your "see also" suggestions](http://forum.ansible.com/t/call-to-action-send-us-your-see-also-suggestions/4093/2):
>
> Great idea, I’d also like to see more examples adding to pages perhaps another thread for that would make sense? INI / CONF The .ini / .conf file lookup, fliters and modules could be linked: [community.general.ini\_file module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html)[ansible.builtin.ini lookup](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ini_lookup.html)[community.general.to\_ini filter](https://docs.ansible.com/ansible/latest/collections/community/general/to_ini_filter.html)[community.general.from\_ini filter](https://docs.ansible.com/ansible/latest/collections/community/general/from_ini_filter.html) And also the JC INI parsers that can be used with [the JC filter](https://docs.ansible.com/ansible/latest/collections/community/general/jc_filter.html): [jc.parsers.ini](https://kellyjonbrazil.github.io/jc/docs/parsers/ini)[jc.parsers.ini\_dup](https://kellyjonbrazil.github.io/jc/docs/parsers/ini_dup) And perhaps the [Wikipedia INI page](https://en.wikipedia.org/wiki/INI_file) and a list of some key applications tha…

Sorry I’m too knackered tonight to help any more but if you are still stuck later this week I’m sure I could help solve it!

---

_[View the full topic](https://forum.ansible.com/t/template-with-to-ini-in-a-loop-str-object-has-no-attribute-items/11007)._
