--- - name: Read yaml file ansible.builtin.include_vars: file: "{{ yaml_file }}" name: yaml_data - name: Update spec with hostAliases - CR when: "'spec' in yaml_data" ansible.builtin.copy: content: "{{ yaml_data | combine({'spec': {'hostaliases': hostaliases }}, recursive=True) | to_nice_yaml(indent=2) }}" dest: "{{ yaml_file }}" mode: '0644'