Ansible Templates

Templates use Jinja2 templating to generate files based on defined #202205042134. Use the #202204300820 template inside #202204272036. The following shows its usage:

---
- name: Ensure apache is installed and started
  hosts: web
  become: yes

  vars:
    http_port: 80
    http_docroot: /var/www/mysite.com

  tasks:
    - name: Verify correct config file is present
      template:
        src: templates/httpd.conf.j2 # from local directory
        dest: /etc/httpd/conf/httpd.conf # copy to this directory on remote hosts
# templates/httpd.conf.j2
Listen {{ http_port }}
DocumentRoot {{ http_docroot }}

Note: We could set owner, group and mode within the template module in order to restrict permission to the file.

Links to this page
#automation