Modify registered variable content from the copy module?

455 views
Skip to first unread message

burn...@umn.edu

unread,
Nov 30, 2016, 5:29:07 PM11/30/16
to Ansible Project
Hello - 

Is there a way to modify the content of a registered variable? I have a shared role that logs output of my deployments.

tasks/main.yml
- name: Log {{ filename }} changes
  blockinfile
:
    dest
: "{{ log_dir }}/{{ pb_logname }}"
    content
: |
     
**********
     
File '{{ filename }}' updated!
     
Modifying role: "{{ role_path }}"
     
Source file: {{ registered_content.src }}
     
New file: {{ registered_content.dest }}
     
Backup file: {{ registered_content.backup_file }}
     
**********
    create
: yes
    insertafter
: EOF
    marker
: ""
 
when: log_type == "file change"


Thus far this has been working great because there was always a pre-existing file to replace. However, I just hit some config that creates a whole new file that never previously existed. Therefore, the first time it runs I get the below error.

TASK [role_config_loadbalancer : Log misc.properties changes] ******************
fatal: [target]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'backup_file'\n\nThe error appears to have been in '/manh/roles/role_utility_logging/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Log {{ filename }} changes\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

In hindsight, this makes sense, since there was nothing to backup in the first place. So I'm wondering if I can manually add a value in 'backup_file' field after it's already been registered? 

TASK [role_config_loadbalancer : debug] ****************************************
ok: [target] => {
    "deployed_copy_output": {
        "backup_file": "ADD TEXT HERE",  
        "changed": true, 
        "checksum": "0d4216d1a25218c5656d8e0db2e68499eba74238", 
        "dest": "/apps/scope/manh/MDA/distribution/DeploymentDirector/installer/properties/misc.properties", 
        "gid": 2345, 
        "group": "g_wmspt", 
        "md5sum": "49552048354e51b2a5d2959a93441766", 
        "mode": "0644", 
        "owner": "wmspt", 
        "secontext": "system_u:object_r:default_t:s0", 
        "size": 9980, 
        "src": "/manh/software/distribution/ansible/staged_config/ptl01a0fap006/MDA/misc.properties", 
        "state": "file", 
        "uid": 3267
    }
}

This way I can continue to use the single logging role without needing to accommodate for corner cases like this and pollute the play output. Or perhaps there is a better way to design this all together. Thoughts?

Thanks!

Alexander H. Laughlin

unread,
Nov 30, 2016, 7:40:09 PM11/30/16
to Ansible Project
The only way I've been able to do something like this, assuming I understand correctly, is to write the value to a vars file in the playbook, then load that vars file when you need it later on in the playbook.
Reply all
Reply to author
Forward
0 new messages