Update extra-vars and save to disk.

Morning All

I run some backups from Ansible
ansible-playbook backup.yml
-i “$ANSIBLE_PLAY/bin/servers.sh”
–extra-vars “@database.yml”
–extra-vars “db_name=MyDatabase”

Works perfectly.
Backs up the MyDatabase using the paramaters in database.yml
But once the backup has completed, I’d like to update one the variabkes in database.yml to say te backup was successful.

I’m not even sure where to start looking - can someone point me in the right direction please…


MyDatabase:
Type: Database
Env: PPE
Services:
Primary: myDB_PRIMARY.LOCAL
Standby: myDB_STANDBY.LOCAL
Type: TCPIP
Backup:
service: mydb_bkp
full_backup_day: SUN
retention: 7
LastBack: success <= this to update,
DataCentres:
Locations: [ “DC3”, “DC4” ]
DC3:
g_db_unique_name: “mydb”
g_db_sid: “mydb”
DC4:
g_db_unique_name: “mydb”
g_db_sid: “mydb”

Updating a hardcoded extra variable from a playbook isn’t a typical pattern, there might be a better approach. Have you considered using a task in the playbook to check if the backup exists instead? The extra variable could be defined using Jinja2, and reference the registered task result and fall back to a default value.

Given database.yml

MyDatabase:
  Type: Database
  Env: PPE
Services:
  Primary: myDB_PRIMARY.LOCAL
  Standby: myDB_STANDBY.LOCAL
  Type: TCPIP
Backup:
  service: mydb_bkp
  full_backup_day: SUN
  retention: 7
  LastBack: success <= this to update,
DataCentres:
  Locations: [DC3, DC4]
DC3:
  g_db_unique_name: mydb
  g_db_sid: mydb
DC4:
  g_db_unique_name: mydb
  g_db_sid: mydb

The playbook pb.yml:

- hosts: localhost

  tasks:

    - name: Backup.
      block:
        - debug:
            msg: "{{ db_name }}: Success."
      rescue:
        - debug:
            msg: "{{ db_name }}: Error."
        - meta: end_host

    - name: Record.
      block:
        - include_vars:
            file: database.yml
            name: database
        - template:
            src: database.yml.j2
            dest: database.yml
          vars:
            update:
              Backup:
                LastBack: "{{ '%Y-%m-%d %H:%M:%S' | strftime }}"
            _database: "{{ [database, update] | combine(recursive=true) }}"

Updates database.yml on success:

shell> ansible-playbook -e db_name=MyDatabase -e @database.yml pb.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => 
    msg: 'MyDatabase: Success.'

TASK [include_vars] ************************************************************
ok: [localhost]

TASK [template] ****************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

templates/database.yml.j2

{{ _database | to_nice_yaml }}

Updated database.yml

Backup:
    LastBack: '2026-10-22 03:52:50'
    full_backup_day: SUN
    retention: 7
    service: mydb_bkp
DC3:
    g_db_sid: mydb
    g_db_unique_name: mydb
DC4:
    g_db_sid: mydb
    g_db_unique_name: mydb
DataCentres:
    Locations:
    - DC3
    - DC4
MyDatabase:
    Env: PPE
    Type: Database
Services:
    Primary: myDB_PRIMARY.LOCAL
    Standby: myDB_STANDBY.LOCAL
    Type: TCPIP

Awesome - thanks Vladimir. This worked perfectly for me.