Reputation: 286
I have this list.
provider:
name: prov1
ip_list:
- 1.2.3.4
- 4.5.6.7
- 7.8.9.10
I want add this list to config file, What I need:
File config:
dbr:excludeRange({"1.2.3.4"}) -- prov1
dbr:excludeRange({"4.5.6.7"}) -- prov1
dbr:excludeRange({"7.8.9.10"}) -- prov1
I checked different loop
, dict
and etc of the ansible but I still have not reached the desired.
result.
thanks.
Upvotes: 0
Views: 467
Reputation: 1002
This example will only work with the list you show there, if you want to work with multiple items, or multiple providers, you should provide a better var/list example but maybe this can help you, this example works using jinja expressions (Also i had to escape the characters '{' and '}' so they will appear in your file):
- name: create list
lineinfile:
line: "{% for ip in provider.ip_list %}dbr:excludeRange({{'{'}}{{ip}}{{'}'}}) -- {{provider.name}}\n{% endfor %}"
dest: /tmp/list.txt
create: yes
And this is the output from the list:
$ cat /tmp/list.txt
dbr:excludeRange({1.2.3.4}) -- prov1
dbr:excludeRange({4.5.6.7}) -- prov1
dbr:excludeRange({7.8.9.10}) -- prov1
Reference to understand why the complex jinja expression: Escaping in jinja
Upvotes: 1