-1

I have the following task in one of my roles:

- name: Moodle--{{ moodle_domain }}--Apache--Virtual Host--Configuration-- Copy
  become: yes
  loop:
    - src: ../templates/moodle.test.conf.j2
      dest: /etc/apache2/sites-enabled/{{ moodle_domain }}.conf
    - src: ../templates/moodle.test-ssl.conf.j2
      dest: /etc/apache2/sites-enabled/{{ moodle_domain }}-ssl.conf
  ansible.builtin.copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: u+rw,g+r,o+r
  notify: Apache--Service-- Restart

One of the templates used above, look like this:

<VirtualHost *:80>
        ServerName {{ moodle_domain }}
        Redirect permanent / https://{{ moodle_domain }}/
</VirtualHost>

I know the moodle_domain variable is passed to the loop, because I can see that the file names on the target host are correct.

But it is not passed to the template. I know this, because on the target host, the files still contain {{ moodle_domain }} instead of the actual domain.

What am I doing wrong?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Geoffrey
  • 5,407
  • 10
  • 43
  • 78
  • 3
    Because you are using the `copy` module, which just copies. You want to use the [`template` module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html). – β.εηοιτ.βε Feb 10 '22 at 11:39
  • Who is the genius who voted to close this? – Geoffrey Feb 10 '22 at 12:00
  • @β.εηοιτ.βε thanks. I realized that, but could not answer my own question yet. – Geoffrey Feb 10 '22 at 12:01
  • 1
    Thanks for that nice ironic tone. Your question is obviously a typo, you even state it in your answer _I previously had template, but copied ..._, plus, there is close to no chance this question will ever help anyone else (and it has surely been asked before). So, yes, it does deserve the appropriate close vote. – β.εηοιτ.βε Feb 10 '22 at 12:23
  • You should have said so in the first place. It is more than a typo. I had forgotten about template and thought src'ing from templates and using .j2 extension is what did the trick. It can definitely help others. "There is not chance.." Give me a break! – Geoffrey Feb 10 '22 at 12:27
  • In case you wonder, I am the other "genius" who voted to close. Using `copy` instead of `template` when needing to expand jinja2 variables is clearly a typo (as bad as forgetting an indentation after an if in python or using b64decode instead of b64encode to encode a string). Hence this question and its self answer are clearly [off-topic](/help/on-topic) and will likely not help anyone in the future. Note also that if your intent was to provide from scratch an FAQ style answer, there is a "answer your own question" checkbox on the question form to provide both in a single action. – Zeitounator Feb 10 '22 at 22:09
  • Thanks for spoiling Stack Overflow, guys. – Geoffrey Feb 10 '22 at 23:24

1 Answers1

-1

I should have ansible.builtin.template not ansible.builtin.copy. I previously had template, but copied the looping style from another role that did not need templating.

Geoffrey
  • 5,407
  • 10
  • 43
  • 78