# Updating ansible.cfg files

**URL:** <https://forum.ansible.com/t/updating-ansible-cfg-files/40591>\
**Category:** Get Help\
**Created:** [February 24, 2025, 3:20pm UTC](https://forum.ansible.com/t/updating-ansible-cfg-files/40591 "2025-02-24T15:20:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy-maier](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/andy-maier/32/7706_2.png) [@andy-maier](https://forum.ansible.com/u/andy-maier)\
**Post date:** [February 24, 2025, 3:20pm UTC](https://forum.ansible.com/t/updating-ansible-cfg-files/40591/1 "2025-02-24T15:20:40Z")

</div>

Hi. We have the need to update a possibly existing `/etc/ansible/ansible.cfg` file during the installation of an Ansible collection. Instead of replacing the file, we’d like to just get the updates in that we need (a few settings in the `[defaults]` section), in order to be tolerant to existing files.

I looked at the `ansible-config` command, but it only lists/views/dumps the effective config, combining all of its sources.

Is there a command line tool or a Python library that can update `ansible.cfg` files?

---

<div class="post-metadata">

**Author:** ![NomakCooper](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nomakcooper/32/7309_2.png) [@NomakCooper](https://forum.ansible.com/u/NomakCooper)\
**Post date:** [February 24, 2025, 3:49pm UTC](https://forum.ansible.com/t/updating-ansible-cfg-files/40591/2 "2025-02-24T15:49:59Z")

</div>

I don’t think there is an official Ansible command or built-in tool that “patches” an existing **`ansible.cfg`**.

You should try using an external parser, such as **[ConfigUpdater](https://configupdater.readthedocs.io/en/latest/index.html)**.

example of how you can use **`ConfigUpdater`** to update the `[defaults]` section in your **`ansible.cfg`** while preserving comments and formatting:

```python
from configupdater import ConfigUpdater

config_path = '/etc/ansible/ansible.cfg'

# Create an updater instance and read the file
updater = ConfigUpdater()
updater.read(config_path)

# Ensure the [defaults] section exists; create it if missing
if 'defaults' not in updater.sections():
    updater.add_section('defaults')

# Access the [defaults]
defaults = updater['defaults']

# Update specific settings; these will be added or overwritten
defaults['inventory'] = '/path/to/your/inventory'
defaults['host_key_checking'] = 'False'

# Optionally, add a comment to one of the settings
defaults['inventory'].comment = 'Updated inventory path'

# Write the updated configuration back to the file
with open(config_path, 'w') as config_file:
    updater.write(config_file)

```

Obviously, since it’s an external script, I advise you to thoroughly test it before making any changes to the real **`ansible.cfg`** file.

---

<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:** [February 24, 2025, 9:30pm UTC](https://forum.ansible.com/t/updating-ansible-cfg-files/40591/3 "2025-02-24T21:30:05Z")

</div>

You could use the [community.general.ini\_file module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) if you wanted to do it using Ansible?
