New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
template module doesn't return a backup file #83167
Comments
Files identified in the description:
If these files are incorrect, please update the |
@crazzy |
I believe this is ultimately a duplicate of #33907 |
|
Even so, the documentation should be fixed. It's an illustrative sketch of the process which is enough to go on if you're familiar with Ansible, but tweaking it slightly would make it more usable for a broad audience. - name: update config and back out if validation fails
block:
# This approach can be used with other actions that support `backup`,
# such as `copy` and `lineinfile`.
- name: do the actual update
template:
src: template.j2
dest: /foo/bar/baz
backup: yes
register: config_update
# The assumption is that the validation command will return non-zero for
# failure, make sure to use `failed_when` if that's not the case.
- name: validate the updated state
shell: my_validation_commmand
become: true
become_user: requiredbyapp
environment:
WEIRD_REQUIREMENT: 1
when: config_update is changed
rescue:
# Hopefully the previous configuration was working
- name: restore original file
copy:
remote_src: true
src: "{{ config_update['backup_file'] }}"
dest: /foo/bar/baz
always:
# We choose to always delete the backup, but it could also be left in place,
# moved somewhere for auditing, or only deleted in `rescue`.
- name: delete backup file
file:
path: "{{ config_update['backup_file'] }}"
state: absent
when: config_update is changed |
Agreed, docs should match behavior, I was just clarifying the currently expected behavior in this case. I'm not against always including it, but if the file is not created you could still have issues with the user assuming so in their play or the key returning a null value. Right now the above would also work but just checking |
closing as this was not a documented feature, just unclear from examples. docs PR above should help clarify. |
Summary
Using the backup option in the template module to get a backup_file property on the registered return value from the task doesn't work. It's indicated here that it should be working:
https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#complex-configuration-validation
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html
From what reading of the code I've done this option doesn't seem to be implemented but I may be wrong in this.
Doing a debug on the registered variable from my template task clearly shows there's no backup file being returned:
Issue Type
Bug Report
Component Name
template
Ansible Version
Configuration
OS / Environment
ansible host: CentOS Linux release 7.9.2009 (Core)
target host: Debian 12.5 / Proxmox VE 8.2.2
Steps to Reproduce
Expected Results
I expected the registered variable from the template task to contain the property "backup_file" as is indicated in the documentation that it would:
https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#complex-configuration-validation
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html
Actual Results
Code of Conduct
The text was updated successfully, but these errors were encountered: