Ansible: Download a file from website and replace value

Ansible_Logo

In this post we are going to see how we can use the below two modules to download a file from a URL or and FTP server to the managed nodes, and replace certain content of that file.

  • ansible.builtin.get_url
  • ansible.builtin.replace
    • This module will replace all instances of a pattern within a file

In the below example we use three nodes:

  • centos9vm (192.168.48.128) – Controller node
  • centosMYOBvm (192.168.48.129) – Managed node – Part of dev group in inventory
  • centos9test1 (192.168.48.132) – Managed node – Part of dev group in inventory

Note: Both the managed nodes have /dev/sda device, but do not have /dev/vdb

Let us check the disk size in these nodes:

[root@centos9test1 ~]# fdisk -l | grep /dev/sda
Disk /dev/sda: 1 GiB, 1073741824 bytes, 2097152 sectors

[root@centosMYOBvm ~]# fdisk -l | grep /dev/sda
Disk /dev/sda: 6 GiB, 6442450944 bytes, 12582912 sectors

The below playbook downloads a sample template to make a note of the sizes of the sda and vdb disks, and after that it populated the file with actual disk size. In case /dev/vdb is present, it records the actual value, but makes it as NONE if the disk is not present

[root@centos9vm ~]# wget http://192.168.48.132/hardwareInfo.empty

–2024-07-06 00:43:50– http://192.168.48.132/hardwareInfo.empty
Connecting to 192.168.48.132:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 42
Saving to: ‘hardwareInfo.empty’

hardwareInfo.empty 100%[===================>] 42 –.-KB/s in 0s

2024-07-06 00:43:50 (4.60 MB/s) – ‘hardwareInfo.empty’ saved [42/42]

=== === 

[root@centos9vm ~]# cat hardwareInfo.empty

my_vda=disk_sda_size
my_vdb=disk_vdb_size

===== ==

Let us look at the playbook created

 

[root@centos9vm ~]# cat hardware.yml

====== ==
– – –
– name: Playbook to capture hardware info
  hosts: dev
  become: true
  ignore_errors: yes
  tasks:
    – name: Task to download the file from URL
      ansible.builtin.get_url:
        url: http://192.168.48.132/hardwareInfo.empty
        dest: /root/hardwareInfo.txt
        mode: 0755

    – name: Task to replace the /dev/sda_size
      ansible.builtin.replace:
        path: /root/hardwareInfo.txt
        regexp: “disk_sda_size”
        replace: “{{ ansible_facts.devices.sda.size }}”
        register: theOutput1

    – name: Task to print the output of sda size
      ansible.builtin.debug:
        var: theOutput1

    – name: Task to replace the /dev/vdb_size
      ansible.builtin.replace:
        path: /root/hardwareInfo.txt
        regexp: “disk_vdb_size”
        replace: “{{ ansible_facts.devices.vdb.size }}”
      register: theOutput2

    – name: Task to print the output of vdb size
      ansible.builtin.debug:
        var: theOutput2

    – name: Task to replace /dev/vdb_size value with NONE
      ansible.builtin.replace:
        path: /root/hardwareInfo.txt
        regexp: “disk_vdb_size”
        replace: NONE
      when:
        theOutput2.failed == true

====== ===

[root@centos9vm ~]# ansible-navigator run -m stdout hardware.yml

==== ===

PLAY [Playbook to capture hardware info] **********************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.48.129]
ok: [192.168.48.132]

TASK [Task to download the file from URL] *********************************************************************************************
ok: [192.168.48.132]
ok: [192.168.48.129]

TASK [Task to replace the /dev/sda_size] **********************************************************************************************
ok: [192.168.48.129]
ok: [192.168.48.132]

TASK [Task to print the output of sda size] *******************************************************************************************
ok: [192.168.48.132] => {
“theOutput1”: {
“changed”: false,
“failed”: false,
“msg”: “”,
“rc”: 0
}
}
ok: [192.168.48.129] => {
“theOutput1”: {
“changed”: false,
“failed”: false,
“msg”: “”,
“rc”: 0
}
}

TASK [Task to replace the /dev/vdb_size] **********************************************************************************************
fatal: [192.168.48.132]: FAILED! => {“msg”: “The task includes an option with an undefined variable. 

……..
fatal: [192.168.48.129]: FAILED! => {“msg”: “The task includes an option with an undefined variable. 
…ignoring

TASK [Task to print the output of vdb size] *******************************************************************************************
ok: [192.168.48.132] => {
“theOutput2”: {
“failed”: true,
…..

TASK [Task to replace /dev/vdb_size value with NONE] **********************************************************************************
ok: [192.168.48.129]
ok: [192.168.48.132]

PLAY RECAP ****************************************************************************************************************************
192.168.48.129 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
192.168.48.132 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1

===== ====

Now let us check the updated file in the managed nodes

[root@centos9vm ~]# ssh 192.168.48.129 ‘cat /root/hardwareInfo.txt’
my_vda=6.00 GB
my_vdb=NONE

[root@centos9vm ~]# ssh 192.168.48.132 ‘cat /root/hardwareInfo.txt’
my_vda=1.00 GB
my_vdb=NONE