Ansible Pilot

Edit single-line text - Ansible Playbook for Changing IP Address of Remote Hosts

This playbook uses the ansible.builtin.lineinfile and ansible.builtin.service modules to automate the process of updating the IP address and netmask values in the network configuration file and restarting the networking service on remote hosts.

April 30, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Automating the process of updating the IP address and netmask values in the network configuration file of multiple remote hosts can be a time-consuming and tedious task. However, with Ansible, it’s possible to automate this process and save time and effort.

Ansible is an open-source automation tool that allows users to automate IT infrastructure tasks, including configuration management, application deployment, and orchestration. One of the key features of Ansible is its ability to manage multiple remote hosts simultaneously, making it an ideal choice for managing large-scale IT infrastructure.

In this article, we will explore how to use Ansible to automate the process of updating the IP address and netmask values in the network configuration file of remote hosts.

Ansible module lineinfile

Today we’re talking about the Ansible module lineinfile. The full name is ansible.builtin.lineinfile, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years and it supports a large variety of operating systems. You are able to insert, update and remove a single line of text in a file.

Main Parameters

This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. “line” is the line of text we would like to insert in the file, easy! By default, the text is going to be inserted at the end of the file, but we could personalize it in a specific position with “insertafter” or “insertbefore”. If there is any tool to validate the file we could specify it in the validate parameter, very useful for configuration files. If the file does not exist we could also “create” it! Usually, we would like to insert a text line but we could also remove using state in conjunction with parameter absent. Let me also highlight that we could also specify some permissions or SELinux property.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

Code

This Ansible playbook changes the IP address and netmask of a remote host by updating the network configuration file.

The hosts field specifies that this playbook should run on all hosts. The become field is set to true, which means that the playbook will run with elevated privileges (i.e. as a superuser).

The vars field sets two variables, new_ip_address and netmask, to the desired IP address and netmask values.

The tasks field contains two tasks. The first task uses the ansible.builtin.lineinfile module to update the network configuration file (/etc/network/interfaces). It searches for a line that starts with the word “address” and replaces it with the new IP address value using the {{ new_ip_address }} variable. The second task does the same thing but for the netmask value. Both tasks also send a notification to a handler called “Restart Network Service”.

The handlers field contains one handler called “Restart Network Service”. It uses the ansible.builtin.service module to restart the networking service on the remote host.

Overall, this playbook is useful for automating the process of changing the IP address and netmask of multiple remote hosts simultaneously.

First, we need to create an Ansible playbook that will perform the necessary tasks. The playbook should include the following fields:

Here is an example playbook:

- name: Change IP Address of Remote Host
  hosts: all
  become: true
  vars:
    new_ip_address: 192.168.1.100
    netmask: 255.255.255.0
  tasks:
    - name: Update Network Configuration File
      ansible.builtin.lineinfile:
        path: /etc/network/interfaces
        regexp: '^address'
        line: 'address {{ new_ip_address }}'
      notify:
        - Restart Network Service

    - name: Update Netmask Configuration File
      ansible.builtin.lineinfile:
        path: /etc/network/interfaces
        regexp: '^netmask'
        line: 'netmask {{ netmask }}'
      notify:
        - Restart Network Service

  handlers:
    - name: Restart Network Service
      ansible.builtin.service:
        name: networking
        state: restarted

Let’s break down the playbook and examine each section in more detail.

The hosts field specifies the hosts that the playbook should run on. In this example, we have set it to all, which means that the playbook will run on all hosts.

The become field is set to true, which means that the playbook will run with elevated privileges (i.e. as a superuser). This is necessary because updating the network configuration file and restarting the networking service require root access.

The vars field sets two variables, new_ip_address and netmask, to the desired IP address and netmask values.

The tasks field contains two tasks. The first task uses the ansible.builtin.lineinfile module to update the network configuration file (/etc/network/interfaces). It searches for a line that starts with the word “address” and replaces it with the new IP address value using the {{ new_ip_address }} variable. The second task does the same thing but for the netmask value. Both tasks also send a notification to a handler called “Restart Network Service”.

The handlers field contains one handler called “Restart Network Service”. It uses the ansible.builtin.service module to restart the networking service on the remote host.

To run the playbook, save it as a YAML file (e.g. change-ip.yml) and run the following command:

ansible-playbook change-ip.yml

This will execute the playbook on all hosts specified in the hosts field and update their IP address and netmask values.

Recap

This article discusses how to use Ansible to automate the process of updating the IP address and netmask values in the network configuration file of multiple remote hosts. It provides an example playbook that includes the necessary fields and modules to perform the update and restart the networking service. The article highlights how Ansible can simplify IT infrastructure management and improve operational efficiency. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases