How to use Chatgpt to build Ansible Playbooks 

Table of Contents

Introduction

In this article we will explore how to use ChatGPT to build the Ansible playbooks based on our request. Ansible is a popular automation tool used by the System Admins and network Engineers to automate their day to day tasks. Building expertise on Ansible is very important moving forward  

Practical examples on using Chatgpt to build Ansible playbooks

Changing the Hostname of the Linux machine

In this example we are going to seek help from CHatGPT to build a simple playbook to change hostname of the hosts for which we are running the tasks 

#Request to ChatGPT 

Can you help me build a Ansible playbook to change the hostname of the linux machine 

#Response from ChatGPT 

Certainly! I can help you build an Ansible playbook to change the hostname of a Linux machine. Here’s an example playbook you can use:

#yaml playbook
---
- name: Change hostname
  hosts: your_target_hosts
  become: yes

  tasks:
    - name: Update hostname
      hostname:
        name: your_new_hostname
      register: result

    - name: Print the result
      debug:
        var: result



In this playbook:

– `your_target_hosts` should be replaced with the target hosts where you want to change the hostname. You can specify individual hostnames or groups defined in your Ansible inventory file.
`your_new_hostname` should be replaced with the desired new hostname for the target machine.

To run this playbook, save it to a file, e.g., `change_hostname.yaml`, and use the `ansible-playbook` command:

#execute it on Bash terminal
ansible-playbook change_hostname.yaml



Make sure you have Ansible installed and the necessary SSH connectivity and authentication set up for the target hosts. Additionally, ensure that the user running the playbook has the required privileges to change the hostname (e.g., using `sudo` or being logged in as the root user).

Note: This playbook assumes you are using a Linux distribution that supports the `hostname` module in Ansible, such as Ubuntu, CentOS, or Debian. If you are using a different distribution, please let me know so I can assist you accordingly.

Backup files

In this example , we are requesting ChatGPT to create a playbook to backup syslogs from the hosts. 

#Request to ChatGPT 

Can you help me build a Ansible playbook to backup /var/log/syslog from multiple hosts 

#Response from ChatGPT 

Certainly! Here’s an example Ansible playbook to backup the `/var/log/syslog` file from multiple hosts:

#yaml playbook
---
- name: Backup syslog
  hosts: your_target_hosts
  become: yes

  tasks:
    - name: Create backup directory
      file:
        path: /path/to/backup/directory
        state: directory
        mode: '0755'

    - name: Copy syslog file
      copy:
        src: /var/log/syslog
        dest: /path/to/backup/directory/{{ inventory_hostname }}_syslog.log


In this playbook:

– `your_target_hosts` should be replaced with the target hosts from which you want to backup the `syslog` file.
– `/path/to/backup/directory` should be replaced with the path to the directory where you want to store the backup files. Make sure the directory exists on the target hosts.
– `{{ inventory_hostname }}` is a variable that represents the hostname of each target host. It will be used to generate a unique filename for each backup.

To run this playbook, save it to a file, e.g., `backup_syslog.yaml`, and use the `ansible-playbook` command:

#execute it on bash terminal
ansible-playbook backup_syslog.yaml



Make sure you have Ansible installed and the necessary SSH connectivity and authentication set up for the target hosts. Additionally, ensure that the user running the playbook has the required privileges to read the `/var/log/syslog` file and write to the backup directory.

Note: This playbook assumes you are running it from a control machine that has SSH access to the target hosts. The control machine should have the necessary Ansible inventory file configured to define the target hosts.

Conclusion

In this article we have covered some basic examples on how to request ChatGPT to build ansible playbooks based on our request. If we have an understanding on how Ansible works  , we can build and test the playbooks provided by ChatGPT quickly. If we are trying to do some complex tasks we can chain multiple small task provided in the  playbook example from ChatGPT  to make it one large playbook which can do complex tasks

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 *