JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
24 days ago5 views
👨‍💻Programming
ip_address
old_hostname
new_hostname
10.10.1.11
pc-old-01
delhi-fin-01
10.10.1.12
pc-old-02
delhi-fin-02
10.10.1.13
pc-old-03
delhi-fin-03
hosts.csv
/opt/ansible/hosts.csv

[ubuntu_clients]
10.10.1.11
10.10.1.12
10.10.1.13

[ubuntu_clients:vars]
ansible_user=ansible
ansible_become=true


---
- name: Change Ubuntu hostnames
  hosts: ubuntu_clients
  become: true
  gather_facts: true

  vars:
    csv_file: /opt/ansible/hosts.csv

  tasks:

    - name: Read CSV file
      community.general.read_csv:
        path: "{{ csv_file }}"
      register: csv_data
      delegate_to: localhost
      run_once: true

    - name: Match current host entry
      set_fact:
        matched_host: "{{ item }}"
      loop: "{{ csv_data.list }}"
      when: item.ip_address == ansible_host

    - name: Show hostname change
      debug:
        msg: "Changing {{ matched_host.old_hostname }} to {{ matched_host.new_hostname }}"

    - name: Validate existing hostname
      fail:
        msg: "Hostname mismatch! Current hostname is not expected."
      when: ansible_hostname != matched_host.old_hostname

    - name: Set new hostname
      hostname:
        name: "{{ matched_host.new_hostname }}"

    - name: Update /etc/hosts
      lineinfile:
        path: /etc/hosts
        regexp: '^127.0.1.1'
        line: "127.0.1.1 {{ matched_host.new_hostname }}"

    - name: Reboot machine
      reboot:
        reboot_timeout: 300

⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.

← Back to timeline