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 copy a file from a source on the Ansible control machine to a destination on the remote host, while replacing variables in the source file with values specified in the playbook.

Here is an example of using the template module to create a new configuration file:

- name: Create a new config file
template:
src: template.j2
dest: /path/to/config.conf

In this example, the source file template.j2 is located on the Ansible control machine and will be used as the template for creating a new configuration file on the remote host. The destination file /path/to/config.conf will be created on the remote host with the contents of the template file, with any variables replaced by values defined in the playbook.

The source file, template.j2, is a Jinja2 template, which allows you to use variables in your template file. For example:

port = {{ port }}

You would then define the value of the port variable in your playbook:

vars:
port: 8080

When the template module runs, the port variable in the template.j2 file will be replaced with the value 8080, and the resulting file will be copied to /path/to/config.conf on the remote host.

Note: The path to the source file and the destination file should be adjusted as needed to match your specific environment and requirements.

Leave a Comment