what does this mean? ['tag_Environment_%s' % args.environment]

31 views
Skip to first unread message

Paul Stivers

unread,
Apr 25, 2015, 2:37:41 AM4/25/15
to ansible...@googlegroups.com
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...


Paul Tötterman

unread,
Apr 25, 2015, 10:58:11 AM4/25/15
to ansible...@googlegroups.com
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

Kevin Bullock

unread,
Apr 25, 2015, 10:58:26 AM4/25/15
to ansible...@googlegroups.com
> On Apr 25, 2015, at 1:37 AM, Paul Stivers <psti...@gmail.com> wrote:
>
> 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]

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

Paul Stivers

unread,
Apr 25, 2015, 1:23:19 PM4/25/15
to ansible...@googlegroups.com
Awsome! Without the print function I wasn't thinking format. Nice, clear explanation. Thanks!

Paul
Reply all
Reply to author
Forward
0 new messages