pyramid13
pyramid13

Reputation: 286

Add a list to the file in ansible

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

Answers (1)

ikora
ikora

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

Related Questions