Ansible: find and replace with remote IP

ansibleansible-playbook

I am using Ansible for auto apache configuration in which i have own app.conf. Now I have app.conf in ansible server and want ansible to replace the existing ip address with remote ip address. my app.conf look like this.

<VirtualHost \
10.10.10.10:80 \
10.10.10.10:80 \
>
DocumentRoot /var/www/application/httpdocs
ServerName lists
ServerAlias lists.*
UseCanonicalName Off
ScriptAlias /mailman/ /usr/lib/mailman/cgi-bin/
Alias /icons/ /var/www/icons/
Alias /pipermail/ /var/lib/mailman/archives/public/
<Directory /var/lib/mailman/archives/>
    Options FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

Now I want 10.10.10.10 to be replaced by remote ip address. can we do this using lineinfile module.

lineinfile:
  path: /etc/hosts
  regexp: '^10\.10\.10\.10'
  line: ''
  owner: root
  group: root
  mode: 0644

Best Answer

What you are looking for is a template. I'll use below the default ipv4 address on the remote host. You can explore the gathered facts if you need something else. In a nutshell:

templates/app.conf.j2

<VirtualHost {{ ansible_default_ipv4.address }}:80>
DocumentRoot /var/www/application/httpdocs
ServerName lists
ServerAlias lists.*
UseCanonicalName Off
ScriptAlias /mailman/ /usr/lib/mailman/cgi-bin/
Alias /icons/ /var/www/icons/
Alias /pipermail/ /var/lib/mailman/archives/public/
<Directory /var/lib/mailman/archives/>
    Options FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

Then you playbook should look like

- name: Configure my application
  hosts: my_hosts
  
  tasks:
    - name: deploy the configuration template
      template:
        src: app.conf.j2
        dest: path/to/app.conf