Recently, I was working on setting up a redis cluster along with separate users for readwrite and readonly. I setup the readonly user using the below privileges (ACLs): user redisreadonly on >mySuperSecretPassword ~* resetchannels -@all +@read +ping +asking Once this is setup, I wrote a simple program in python to connect to the redis cluster using the readonly credentials and print the number of keys. #!/usr/bin/env python3 """ pip3 install redis """ from redis.cluster import RedisCluster, ClusterNode # ─────── Cluster connection ─────── startup_nodes = [ ClusterNode("redis_host_1", 6379), ClusterNode("redis_host_2", 6379), ClusterNode("redis_host_3", 6379) ] rc = RedisCluster(startup_nodes=startup_nodes,decode_responses=True,username='redisreadonly',password='mySuperSecretPassw...
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
Interesting Oracle Applications (EBS) Interview Questions
Below are some interesting interview questions that I have faced over the years. What is the difference between a concurrent manager and a cron job? Can one patch a CRS Oracle home with the CRS services up? Suppose, there are 2 nodes node1 and node2 with CRS cluster and one wants to patch node1. Can it be done with node2 being up? Suppose one fires a select statement and the data does not in the library cache, then the data, obviously, has to be fetched from the data files. Which background process is used for this purpose? What are the sizing requirements for say, a 100 concurrent users Oracle Apps Installation? What is the difference between a shared appl_top and a staged appl_top?
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 ...
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....
Comments