How to use ChatGPT to build Jinja2 templates for Ansible Playbooks

Table of Contents

Introduction

ChatGPT is a great tool for building jinja2 templates for our ansible playbook tasks. In this article we will see some practical examples on using ChatGPT to build Ansible playbooks with jinja2 templating involved. 

In our last article we covered examples on how to use the ChatGPT to build Ansible Playbooks without jinja2 templates. If you happen to have missed our earlier article , please go through the link below 

Practical examples on using ChatGPT to build Ansible Playbooks + Jinja2 templates 

How to use ChatGPT to  build configs for Interfaces with Ansible + Jinja2 templates 

In this example we are using ChatGPT to build Ansible Playbook and Jinja2 templates for configuring two interfaces with Ip address. In the First response from ChatGPT we saw some errors in the playbook , so we requested it again to correct it . Then the second playbook example from  Chatgpt has those corrections made

#Request to ChatGPT 

Can you create the Jinja2 templates for the Assigning Ip address on 2 interfaces   using the ansible playbook 

#Response from ChatGPT 
Certainly! Here’s an example of an Ansible playbook using Jinja2 templates to assign IP addresses to two interfaces:

**Ansible playbook: `assign_ip.yml`**


---
- name: Configure IP addresses on interfaces
  hosts: your_target_host
  gather_facts: no
  
  vars:
    interfaces:
      - interface_name: GigabitEthernet0/0
        description: LAN Interface
        ip_address: 192.168.1.1
        subnet_mask: 255.255.255.0
      - interface_name: GigabitEthernet0/1
        description: WAN Interface
        ip_address: 203.0.113.1
        subnet_mask: 255.255.255.0
  
  tasks:
    - name: Generate interface configuration
      template:
        src: interface_config.j2
        dest: /path/to/interface_configs/{{ item.interface_name }}.cfg
      loop: "{{ interfaces }}"



**Jinja2 template: `interface_config.j2`**


interface {{ item.interface_name }}
  description {{ item.description }}
  ip address {{ item.ip_address }} {{ item.subnet_mask }}



In the above example, we have an Ansible playbook named `assign_ip.yml`. It defines a list of interfaces with their respective properties such as interface name, description, IP address, and subnet mask.

The playbook uses the `template` module to render the `interface_config.j2` template for each interface. It loops over the `interfaces` variable and generates a separate configuration file for each interface in the specified destination directory.

Make sure to replace `your_target_host` with the actual hostname or IP address of the target host where you want to configure the IP addresses. Also, modify the `dest` path in the playbook according to your requirements.

To run the playbook, use the following command:


ansible-playbook assign_ip.yml


This will execute the playbook and generate individual configuration files for each interface based on the provided Jinja2 template.
#Correction request to ChatGPT 

One correction , "{{ item.interface_name }}.cfg" this will cause issues as the interface_name has slash "GigabitEthernet0/0" 

#Response from ChatGPT 
You’re correct, using the slash in the file name can cause issues. To address this, you can replace the slash with another character (such as an underscore) in the file name. Here’s an updated version of the playbook:

**Ansible playbook: `assign_ip.yml`**


---
- name: Configure IP addresses on interfaces
  hosts: your_target_host
  gather_facts: no
  
  vars:
    interfaces:
      - interface_name: GigabitEthernet0/0
        description: LAN Interface
        ip_address: 192.168.1.1
        subnet_mask: 255.255.255.0
      - interface_name: GigabitEthernet0/1
        description: WAN Interface
        ip_address: 203.0.113.1
        subnet_mask: 255.255.255.0
  
  tasks:
    - name: Generate interface configuration
      template:
        src: interface_config.j2
        dest: "/path/to/interface_configs/{{ item.interface_name | replace('/', '_') }}.cfg"
      loop: "{{ interfaces }}"



**Jinja2 template: `interface_config.j2`**


interface {{ item.interface_name }}
  description {{ item.description }}
  ip address {{ item.ip_address }} {{ item.subnet_mask }}



In this updated playbook, the `replace()` filter is used to replace the slash (`/`) in the interface name with an underscore (`_`) in the destination file name. This ensures that the file name does not contain any characters that could cause issues.

Make sure to replace `your_target_host` with the actual hostname or IP address of the target host where you want to configure the IP addresses. Also, modify the `dest` path in the playbook according to your requirements.

When you run the playbook with the command `ansible-playbook assign_ip.yml`, it will generate individual configuration files for each interface using the modified file names based on the provided Jinja2 template.

How to use ChatGPT to build EBGP configs with Ansible + Jinja2 templates 

In this example we requested ChatGPT to render configs to bring up one ebgp peership between two devices using Ansible + Jinja2 templates. We can see that ChatGPT was able to build the configs for both local and remote network device 

#Request to ChatGPT 

Can you create the Jinja2 templates for the forming one eBGP peership  using the ansible playbook 

#Response from ChatGPT 
Certainly! Here’s an example of an Ansible playbook using Jinja2 templates to configure eBGP peering between two routers:

**Ansible playbook: `configure_ebgp.yml`**


---
- name: Configure eBGP peering
  hosts: your_target_hosts
  gather_facts: no

  vars:
    local_asn: 65000
    local_router_id: 192.168.1.1
    local_interface: GigabitEthernet0/0
    local_ip_address: 203.0.113.1
    remote_asn: 65001
    remote_router_id: 192.168.2.1
    remote_ip_address: 203.0.113.2

  tasks:
    - name: Generate local router configuration
      template:
        src: local_config.j2
        dest: /path/to/local_router_config.txt

    - name: Generate remote router configuration
      template:
        src: remote_config.j2
        dest: /path/to/remote_router_config.txt



**Jinja2 template: `local_config.j2`**

router bgp {{ local_asn }}
  bgp router-id {{ local_router_id }}
  neighbor {{ remote_ip_address }} remote-as {{ remote_asn }}
    description eBGP Peer
    update-source {{ local_interface }}



**Jinja2 template: `remote_config.j2`**


router bgp {{ remote_asn }}
  bgp router-id {{ remote_router_id }}
  neighbor {{ local_ip_address }} remote-as {{ local_asn }}
    description eBGP Peer
    update-source {{ local_interface }}



In the above example, we have an Ansible playbook named `configure_ebgp.yml`. It defines the variables required to configure eBGP peering between two routers.

The playbook uses the `template` module to render the `local_config.j2` and `remote_config.j2` templates. The `local_config.j2` template is rendered for the local router, and the `remote_config.j2` template is rendered for the remote router.

Make sure to replace `your_target_hosts` with the actual host(s) where you want to configure eBGP peering. Also, modify the `dest` paths in the playbook according to your requirements.

To run the playbook, use the following command:

ansible-playbook configure_ebgp.yml


This will execute the playbook and generate the configuration files for the local and remote routers based on the provided Jinja2 templates.

Conclusion

In this article we covered examples on how to use the ChatGPT to build ansible playbooks with Jinja2 templates. We can see there were some errors in the playbooks built by ChatGPT , however they were very small mistakes and can be quickly identified and fixed. ChatGPT can help us do things faster but we need to have some understanding of the tool. In this case the tool we were using is Ansible and jinja2 otherwise we won’t be able to debug the errors. 

If you are interested in more articles like this , Please check our series ChatGPT for Network Engineers using the link below 

https://discoveringsystems.com/category/chatgpt-for-network-engineers/

Leave a Comment

Your email address will not be published. Required fields are marked *