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
Elasticsearch: Get output in CSV format using curl and jq
If you've used elasticsearch , you'll know that each call to it has to be through REST APIs, either using Kibana , curl, postman or similar tools. If you are like me, then curl is the way to go! However, that does pose one problem - how does one select specific fields, akin to select statements of SQL/RDBMS DBs? The answer is using jq , the lightweight and flexible command-line JSON processor. This is my use-case: I have an elasticsearch cluster in which daily snapshots are scheduled and I would like to get the output of snapshot name, snapshot repo, name of the SLM policy etc., in a comma separated row format so that I can directly use it to push that data to my backup catalog in PostgreSQL. So, I query "_snapshot/<snapshot_repo>/backup_name_start_string*" and the output is quite long and convoluted (the more the backups, the longer the output)! Here, I present the way to shorten the output as well as to fetch it in row based CSV format, easier to understa...
EXCEPTION_ACCESS_VIOLATION in Jinitiator and IE crashes
The Environment Apps Version : 11.5.10.2 Jinitiator Version : 1.3.1.26 Internet Explorer Version : 6 The Issue One of our end users was receiving a very strange error while using jinitiator. The Internet explorer window would crash (close by itself) after creating a file HS_ERR_PID .log on the desktop, where #### stands for the Process ID of the f60webmx session. This error is intermittent and not easily reproducible. Moreover, the user would have to redo his/her work and hence this issue was turning out to be very vexing indeed. The error log is as below: An unexpected exception has been detected in native code outside the VM. Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x6D043D1AFunction name=Java_sun_java2d_loops_DefaultComponent_IntIsomorphicCopy Library=C:\Program Files\Oracle\JInitiator 1.3.1.26\bin\awt.dll Current Java thread: at sun.java2d.loops.DefaultComponent.IntIsomorphicCopy(Native Method) at sun.java2d.loops.IntRgbToIntRgb.OpaqueBlit(Unknown Source) at ...
Redis user permissions (ACL) and python connection
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...
Comments