This may be as much a Python question as an Ansible question. This is a function in a deploy script.
I'm trying to understand what "['tag_Environment_%s' % args.environment]" means.
Problem is I don't know enough about the syntax to even google the right topic. Any help?
Either an explanation, or pointer to a url, or a topic to search, would be very much appreciated.
Paul
from ansible.inventory import Inventory
def set_inventory(args):
subsets = ['tag_Environment_%s' % args.environment]
if args.subset:
subsets.append(args.subset)
inventory = Inventory(args.inventory)
inventory.subset(":&".join(subsets))
return inventory code here...
I’m trying to understand what “[‘tag_Environment_%s’ % args.environment]” means.
is a list literal in Python. [1, 2, 3] can also be written as list(1, 2, 3)
% is the string format operator. ‘bar: %s’ % ‘tada’ results in the string ‘bar: tada’
[‘tag_Environment_%s’ % args.environment] results in a list containing one element: a string that begins with ‘tag_Environment_’ followed by whatever args.environment contains.
Cheers,
Paul
That's how Python does string interpolation, in a similar style to C's sprintf(). https://docs.python.org/2/library/stdtypes.html#string-formatting-operations
pacem in terris / мир / शान्ति / سَلاَم / 平和
Kevin R. Bullock
Awsome! Without the print function I wasn’t thinking format. Nice, clear explanation. Thanks!
Paul