Although, I've used Ansible extensively for a lot of automation and orchestration tasks, using Ansible for AWS was indeed, a new territory for me. This turned out to be a blessing, since along with using Ansible for AWS tasks, I also learnt how to use WSL (Windows Subsystem for Linux) on a Windows machine. Though WSL's been around for some time, I still hadn't come around to using it since I was mostly using my Macbook pro. Not anymore, though! Anyway, I have listed below the steps to: Install WSL on Windows 11 23H2 patch Install AWS CLI on Ubuntu 22.04 (Exact version - 22.04.3 LTS) Install Ansible and the amazon.aws collection Use AWS CLI to get the list of VPCs in the region - us-east-1 (or a region of your choice) Create a python file/script to get the list of VPCs in the region - us-east-1 (or a region of your choice) Create an Ansible playbook to get the list of VPCs in the region - us-east-1 (or a region of your choice. You may download the comple...
Read a file and extract specific lines using Native Ansible Modules
- Get link
- X
- Other Apps
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:example-host-2-priv.example.com 192.168.230.187:example-host-3-priv.example.com 192.168.230.195:oracle-rac-scan.example.com 192.168.230.196:oracle-rac-scan.example.com 192.168.230.197:oracle-rac-scan.example.com
REQUIREMENT
From the above file, fetch only the hostnames (with and without domain name and only hostnames that don’t contain the “-vip”, “-scan” and “-priv” strings) and the corresponding IP Addresses
- extract only the hostnames without “-vip”, “-scan” and “-priv” strings and without IP Address (see below)
example-host-1.example.com example-host-2.example.com example-host-3.example.com
- extract only the hostnames without domain and without “-vip”, “-scan” and “-priv” strings and without IP Address (see below)
example-host-1 example-host-2 example-host-3
- extract only the IP Addresses corresponding to the hostnames and without “-vip”, “-scan” and “-priv” strings (see below)
192.168.230.165 192.168.230.166 192.168.230.167
SOLUTION IN SHELL
## Get the list of hosts (with and without the domain name) ## and the list of IP addresses and write to the temp hosts list file. IP_AND_HOSTNAMES_FILE=/tmp/IP_and_hostnames.txt DOMAIN_NAME=example.com TEMP_HOST_LIST_FILE=/tmp/specific_IP_and_hostnames.txt awk -F':' '! /scan/ && ! /priv/ && ! /vip/ {print $2}' ${IP_AND_HOSTNAMES_FILE} | sed -e "s,\.${DOMAIN_NAME},,g" >> ${TEMP_HOST_LIST_FILE} ## Hostnames without domain awk -F':' '! /scan/ && ! /priv/ && ! /vip/ {print $2}' ${IP_AND_HOSTNAMES_FILE} >> ${TEMP_HOST_LIST_FILE} ## Hostnames with domain awk -F':' '! /scan/ && ! /priv/ && ! /vip/ {print $1}' ${IP_AND_HOSTNAMES_FILE} >> ${TEMP_HOST_LIST_FILE} ## IP Addresses of hostnames
SOLUTION IN ANSIBLE
--- - hosts: all gather_facts: False vars: IP_AND_HOSTNAMES_FILE: /tmp/IP_and_hostnames.txt DOMAIN_NAME: example.com HOST_DETAILS_FILE: /tmp/specific_IP_and_hostnames.txt tasks: - name: Read the IP_AND_HOSTNAMES_FILE using slurp module ansible.builtin.slurp: src: "{{ IP_AND_HOSTNAMES_FILE }}" register: ip_and_hostnames_file_contents_in_base_64 - name: Print the output of the file - IP_AND_HOSTNAMES_FILE that was read using slurp - into register variable ip_and_hostnames_file_contents_in_plain_text debug: msg: "{{ ip_and_hostnames_file_contents_in_base_64['content'] | b64decode }}" register: ip_and_hostnames_file_contents_in_plain_text - name: Write the hosts with the domain name using the variable - ip_and_hostnames_file_contents_in_plain_text - to a file lineinfile: path: "{{ HOST_DETAILS_FILE }}" line: "{{ item.split(':')[1] }}" create: True with_items: "{{ ip_and_hostnames_file_contents_in_plain_text.msg.split('\n') }}" when: - "'scan' not in item" - "'priv' not in item" - "'vip' not in item" - "item != ''" - name: Write the hosts without the domain name using the variable - ip_and_hostnames_file_contents_in_plain_text - to a file lineinfile: path: "{{ HOST_DETAILS_FILE }}" line: "{{ item.split(':')[1].split('.')[0] }}" create: True with_items: "{{ ip_and_hostnames_file_contents_in_plain_text.msg.split('\n') }}" when: - "'scan' not in item" - "'priv' not in item" - "'vip' not in item" - "item != ''" - name: Write the IP Address of the RAC hosts using the variable - ip_and_hostnames_file_contents_in_plain_text - to a file lineinfile: path: "{{ HOST_DETAILS_FILE }}" line: "{{ item.split(':')[0] }}" create: True with_items: "{{ ip_and_hostnames_file_contents_in_plain_text.msg.split('\n') }}" when: - "'scan' not in item" - "'priv' not in item" - "'vip' not in item" - "item != ''"
- Get link
- X
- Other Apps
Popular posts from this blog
Check if UTL_FILE and FND_FILE are working fine
Yesterday, while troubleshooting a vexing ORA-29280 error, I came across 2 useful PL/SQL anonymous blocks that can be used to test FND_FILE and UTL_FILE to find out whether they are indeed working fine. UTL_FILE set serveroutput on DECLARE file_location VARCHAR2(256) := '<first entry on utl_file_dir>'; file_name VARCHAR2(256) := 'utlfile1.lst'; file_text VARCHAR2(256) := 'THIS IS A TEST'; file_id UTL_FILE.file_type; BEGIN file_id := UTL_FILE.fopen(file_Location, file_name, 'W'); UTL_FILE.put_line(file_id, file_text); UTL_FILE.fclose(file_id); EXCEPTION WHEN UTL_FILE.INVALID_PATH THEN dbms_output.put_line('Invalid path ' || SQLERRM); WHEN OTHERS THEN dbms_output.put_line('Others '|| SQLCODE || ' ' || SQLERRM); END; / References: Metalink (MOS) Note ID: 261693.1 FND_FILE set serveroutput on exec FND_FILE.PUT_LINE(FND_FILE.LOG, 'THIS IS A TEST'); HTH....
Modify retention period of workflow queues
Yesterday, there was a requirement to increase the retention period of WF_DEFERRED and WF_BPEL_Q so that the developers could troubleshoot issues involving business events. This can be done this way: 1. Check the retention period of existing workqueues. SQL> SELECT owner, name, retention FROM all_queues WHERE name LIKE 'WF%'; OWNER NAME RETENTION --------------- --------------------------------------------- ------------------------------ APPS WF_BPEL_Q 0 APPLSYS WF_INBOUND_QUEUE ...
Clone database home (clone.pl) deprecated in Oracle 19c
Back to blogging, at last. Well, this time it's about clone.pl. Background about clone.pl The easiest way to install a patched and up-to-date Oracle home is to physically copy it from a source server (that is already patched and up-to-date) and use clone.pl on the target server so that the binaries are linked to the target server's OS binaries (like gcc, libstdc++ etc.,), registered with the Oracle Inventory etc. The alternative to clone.pl is to: Install Oracle binaries using GUI or command line Apply the latest patchset (PSU/CPU patches) As you can see now, clone.pl is really useful in order to cut down time to build a database in a new server. More about clone.pl here and here (12c), here (18c). However, in Oracle 19c, Oracle has officially said - " Starting from oracle database 19c ,the clone.pl script is deprecated and can be removed in a future release. Hence, Oracle recommends that you use the software-only installation option, available in the database installe...
Comments