How to create a new config file in Ansible playbook

www.l‮ttua‬uri.com
How to create a new config file in Ansible playbook

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:

  1. Define the variables you want to use in your template. For example, you might have a variable app_port that specifies the port number on which the application should listen:
app_port: 8080
  1. Create a Jinja2 template for the configuration file. For example, if you want to create a configuration file for an Apache web server, you might create a template like the following:
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.

  1. Use the 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.

Created Time:2017-10-28 21:39:01  Author:lautturi