36
36
- Can be one of the options specified in U(http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options).
37
37
- If value provided does not exist in the above options, it will be used as a literal string.
38
38
- To use tags as hostnames use the syntax tag:Name=Value to use the hostname Name_Value, or tag:Name to use the value of the Name tag.
39
+ - Jinja2 filters can be added to the hostnames string. Added in version 9.2.0.
39
40
type: list
40
41
elements: raw
41
42
default: []
268
269
- us-east-1
269
270
hostvars_prefix: 'aws_'
270
271
hostvars_suffix: '_ec2'
272
+
273
+ ---
274
+
275
+ # Define hostnames variables with jinja2 filters.
276
+ plugin: amazon.aws.aws_ec2
277
+ regions:
278
+ - us-east-1
279
+ hostnames:
280
+ - "tag:Name | replace('test', 'prod')"
271
281
"""
272
282
273
283
import re
@@ -549,6 +559,26 @@ def _sanitize_hostname(self, hostname):
549
559
else :
550
560
return to_text (hostname )
551
561
562
+ def _get_hostname_with_jinja2_filter (self , instance , preference , return_single_hostname = False ):
563
+ jinja2_filter = None
564
+ is_template = False
565
+ if "|" in preference :
566
+ preference , jinja2_filter = preference .split ("|" , maxsplit = 1 )
567
+ preference = preference .rstrip ()
568
+ is_template = True
569
+ if preference .startswith ("tag:" ):
570
+ hostname = _get_tag_hostname (preference , instance )
571
+ else :
572
+ hostname = _get_boto_attr_chain (preference , instance )
573
+ if is_template :
574
+ template_var = "{{'%s'|%s}}" % (hostname , jinja2_filter )
575
+ if isinstance (hostname , list ):
576
+ template_var = "{{%s|%s}}" % (hostname , jinja2_filter )
577
+ hostname = self .templar .template (variable = template_var , disable_lookups = False )
578
+ if isinstance (hostname , list ) and return_single_hostname :
579
+ hostname = hostname [0 ] if hostname else None
580
+ return hostname
581
+
552
582
def _get_preferred_hostname (self , instance , hostnames ):
553
583
"""
554
584
:param instance: an instance dict returned by boto3 ec2 describe_instances()
@@ -570,11 +600,8 @@ def _get_preferred_hostname(self, instance, hostnames):
570
600
separator = preference .get ("separator" , "_" )
571
601
if hostname and hostname_from_prefix and "prefix" in preference :
572
602
hostname = hostname_from_prefix + separator + hostname
573
- elif preference .startswith ("tag:" ):
574
- tags = _get_tag_hostname (preference , instance )
575
- hostname = tags [0 ] if tags else None
576
603
else :
577
- hostname = _get_boto_attr_chain ( preference , instance )
604
+ hostname = self . _get_hostname_with_jinja2_filter ( instance , preference , return_single_hostname = True )
578
605
if hostname :
579
606
break
580
607
if hostname :
@@ -602,10 +629,8 @@ def _get_all_hostnames(self, instance, hostnames):
602
629
separator = preference .get ("separator" , "_" )
603
630
if hostname and hostname_from_prefix and "prefix" in preference :
604
631
hostname = hostname_from_prefix [0 ] + separator + hostname [0 ]
605
- elif preference .startswith ("tag:" ):
606
- hostname = _get_tag_hostname (preference , instance )
607
632
else :
608
- hostname = _get_boto_attr_chain ( preference , instance )
633
+ hostname = self . _get_hostname_with_jinja2_filter ( instance , preference )
609
634
610
635
if hostname :
611
636
if isinstance (hostname , list ):
0 commit comments