Yz_Xyz
(Yz Xyz)
1
Hi - I want to create 3 variables based on hostname. These variables will be used inside other tasks.
Host Pattern: mabos
Ex: mabosqa1was01
what is the best way to create variables and use for subsequent task on host ?
should I write 3 different shell scripts to register appropriate variables ?
Thanks,
You can create your own filter. I’ve created this one that you should easily be able to modify to suit your needs.
`
import math
import collections
from ansible import errors
def uppercase_all(arg):
return arg.upper()
def extract_siteid(arg):
if len(arg) < 10:
return FALSE
site_id = arg[:7]
return site_id
def extract_datacenter(arg):
if len(arg) < 10:
return FALSE
datacenter = arg[7:10]
return datacenter
class FilterModule(object):
def filters(self):
return {‘uppercase_all’: uppercase_all, ‘extract_siteid’: extract_siteid, ‘extract_datacenter’: extract_datacenter}
`
system
(system)
3
just define vars based on inventory_hostname:
group_vars/all.yml
nodenum: '{{inventory_hostname|regex_replace("mabos.+?(\d+)$", "\1")}}'
component: '{{inventory_hostname|regex_replace("mabos.+?\d+(\w+?)\d+$", "\1")}}'
...