To create a new configuration file in an Ansible playbook, you can use the template
module. The template
module allows you to define a configuration file using Jinja2 templates, and then use variables to fill in the values.
Here is an example of how to use the template
module to create a new configuration file in an Ansible playbook:
app_port
that specifies the port number on which the application should listen:app_port: 8080
Listen {{ app_port }} <VirtualHost *:{{ app_port }}> DocumentRoot /var/www/html ServerName example.com </VirtualHost>
This template uses the app_port
variable to specify the port number on which the web server should listen.
template
module to create the configuration file. In your playbook, you can use the template
module to define the location of the template file and the destination of the generated configuration file:- name: Create Apache configuration template: src: templates/apache.conf.j2 dest: /etc/apache2/sites-available/example.com.conf
This will create a new configuration file called example.com.conf
in the /etc/apache2/sites-available
directory, based on the Jinja2 template templates/apache.conf.j2
.