Posts

Showing posts from August, 2023

Read a file and extract specific lines using Native Ansible Modules

I never realised that reading a file on a remote host and extracting specific lines from that file would be such an arduous task. As part of one of my projects, I had this requirement and without using "cat" or other shell utilities, it was quite cumbersome to read and extract the information that I was looking for. Read on to find out how it can be done using Ansible native commands. Note that I have suggested only one out of a possible many methods. Feel free to explore yourself and update the comments here! REMOTE FILE CONTENTS $ cat /tmp/IP_and_hostnames.txt $ cat /tmp/IP_and_hostnames.txt 192.168.230.165:example-host-1.example.com 192.168.230.166:example-host-2.example.com 192.168.230.167:example-host-3.example.com 192.168.230.175:example-host-1-vip.example.com 192.168.230.176:example-host-2-vip.example.com 192.168.230.177:example-host-3-vip.example.com 192.168.230.185:example-host-1-priv.example.com 192.168.230.186:e