Score:1

Really slow converting host vars to env file with ansible

cn flag

I have the following task in ansible:

- name: Set env vars
  lineinfile:
    path: "~/.env"
    mode: 0600
    create: yes
    line: "{{ item.key|upper }}=\"{{ item.value }}\""
    regexp: "^{{ item.key|upper }}="
  loop: "{{ env_file |dict2items }}"

It's creating the .env file line-by-line but it is REALLY slow. Is there a better way?

The .yml file looks like:

env_file:
  key1: "value1"
  key2: "value2"

The resulting file has the format:

KEY1="value1"
KEY2="value2"

Each line takes about 20 seconds to process and it really adds up.

br flag
Very probably there is something wrong with the connection. To be sure, try the same task locally (- hosts: localhost).
cn flag
@VladimirBotka I'm inclined to agree, or it was some sort of memory leak because I restarted my IDE and the whole process sped up considerably. Still, A. Darwin's solution is good and I'm using it as well.
Score:0
my flag

Try using the template module and writing a Jinja2 file. It tends to be faster than lineinfile when dealing with loops.

cn flag
Thank you, for the benefit of others the jinja2 file should look like this: ` {% for k, v in env_file.items() %} {{ k | upper }}="{{ v }}" {% endfor %} ` (add appropriate linebreaks)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.