How to update nested variables in Ansible

I have additional information, such as db connection information, etc., stored in /etc/ansible/facts.d/environment.fact.

They become available as type variables ansible_local.environment.database.name. What is the best way to update the database name?

I tried the set_fact module, but could not correctly update the nested variable. It just overwrites the entire hash ansible_local.

- name: Update database name
  set_fact:
  args:
    ansible_local:
      environment:
        database:
          name: "{{ db_name }}"
+4
source share
2 answers

This should help if you are using Ansible 2.0 or later.

- set_fact:
    test:
      app:
        in: 1
        out: 2

- set_fact:
    test_new:
      app:
        transform: 3

- set_fact:
    test: "{{test|combine(test_new,recursive=True)}}"

- debug: var=test

A combine- Jinja2 filter. Make sure you use the option recursivein such cases.

+1
source

- . . ansible.conf:

# if inventory variables overlap, does the higher precedence one win
# or are hash values merged together?  The default is 'replace' but
# this can also be set to 'merge'.
#hash_behaviour = replace

, hash_behaviour = merge, , .

0

All Articles