Good morning,
Is there a way to perform variable replacement in text read from an external file via the file lookup into a list, in the same way that it would be if the list was specified in the playbook task?
I want to read a set of commands from a different file depending on the value of a variable (which is fine, that works) but also to be able to override a value on a case by case basis.
Unfortunately I can’t use a Jinja2 template file as the module does not work properly with them. (At least with versions up to and including 2.6)
What I’m trying to do is to use the ios_config module to configure interfaces on Cisco switches. I can’t use ios_l2_interface because there are settings I need to pass which can’t yet be set with that module so I have to pass a list of commands to ios_config instead. It does support template files, but unfortunately a number of other parameters for ios_config are for some reason mutually exclusive with using templates vs lists of strings, and those parameters are needed for it to work.
If I pass a list of strings to the module, it works as desired:
commands:
-
switchport mode trunk
-
switchport trunk native vlan {{ cisco_access_vlan | default(‘1’) }}
-
…
Ideally I want to be using a with_items loop on a dict contained in config variables to iterate through a set, and based on a variable in that dict, reading a file:
commands: “{{ lookup(‘file’, ‘{{ role_path }}/templates/profile-{{ item.profile }}.txt’).split(‘\n’) }}”
This works but passes the text verbatim to the module (if the text in the first example is used, the second line can’t be parsed by the device and gives errors). Worst case I’ll have to create a profile file every time I want something slightly different. But I’d hoped to create a more reusable system.
So is there any way to use the file lookup as given in the second example, but still do variable expansion/substitution such as in the first example above.
Thanks,
Steve.