[Python] Using a variable inside a variable in an Ansible playbook

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to copy configuration files to a set of remote systems. Each configuration file is unique and has the server name as part of the file name.

    When I try to create the source file name in the playbook, I get errors complaining about nested variables, variable not found, file not found, etc. depending upon how I try to get the process to work. Since I am a YAML newbie, it's being a challenge.

    Here's a sample of what I'm trying to do. Any ideas or words of wisdom?

    configs directory contents:

    cfg_ONE.cfg
    cfg_TWO.cfg
    cfg_THREE.cfg


    hosts file:

    [mysystems]
    ONE ansible_host=one.example.com
    TWO ansible_host=two.example.com
    THREE ansible_host=three.example.com


    script file:

    ---
    - hosts: all

    vars:
    config_file='cfg_{{inventory_hostname}}.cfg'

    tasks:

    - name: Copy Config Files
    copy:
    src: configs/{{config_file}}
    dest: /etc/my_app/update.conf


    Thanx!

    Continue reading...

Compartilhe esta Página