set_fact adding unicode "[u' in front of new variable (2.3.1.0)

617 views
Skip to first unread message

Alex White

unread,
Jun 21, 2017, 5:30:23 PM6/21/17
to Ansible Project
I just recently updated to 2.3.1.0. I've used set_fact before the upgrade to set my AWS instance ID to a variable "coreaid". 


- name: Save instance ID to coreaid
set_fact:
coreaid: "{{ ec2_asg.instances }}"

- debug: msg="the id= {{ coreaid }}"

- name: Set the instance public IP to a variable
register: instance_public_ip
shell: sudo aws ec2 describe-instances --filters "Name=instance-id,Values='{{ coreaid }}'" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text


I set the fact, and it's correct:

ok: [local] => {

    "ansible_facts": {

        "coreaid": [

            "i-XXXXX"


When I debug or use it later in the play the value is:

ok: [local] => {

    "msg": "the id= [u'i-XXX']"


What am I doing that is causing the "[u' to be added to the beginning of the instance ID?

Thanks!

Brian Coca

unread,
Jun 21, 2017, 5:48:04 PM6/21/17
to Ansible Project
"coreaid": [

"i-XXXXX"
]

and [u'i-XXX'] are actually the same thing, just displayed differently.

coreaid is a list, with a unicode string, which internally is
represented as [u'i-XXX'], What you are seeing as 'correct' is
JSONified output, that is why you see the difference.


----------
Brian Coca

Alex White

unread,
Jun 21, 2017, 10:18:37 PM6/21/17
to Ansible Project
Brian, thank you!

Additional information provided by the IRC chan:

Complex types get stringified using python's default representation. the [ is for start of list, and each string has u'...' denoting that it's a unicode string
instead of set_fact: instance_id="expression", use set_fact: instance_id="expression[0]"
Reply all
Reply to author
Forward
0 new messages