I'm developing a playbook with Ansible 2.0 where I want to insert a blob of 
JSON data (read from a file) into the user_data segment of an EC2 instance.

The task looks like this:

- name: Create Instance

   local_action:

     module: ec2

     region: "{{ aws_region }}"

     keypair: "{{ key_name }}"

     instance_type: "m4.xlarge"

     image: "{{ ami }}"

    assign_public_ip: yes

     group: cmic-sg

     vpc_subnet_id: "{{ vpc_subnet_id }}"

     exact_count: 1

     count_tag:

       Name: "instance ansible"

     instance_tags:

       Name: "instance ansible"

     wait: yes

     user_data: "{{ lookup('file', '/tmp/metadata.txt') }}"

   register: app_instance


The JSON data read from the file looks like this:

{

"key1" : "value1",

"key2" : "value2",

“key3" : "value3"

}


This errors out as the string passed for user_data is apparently treated as 
a python dict:

An exception occurred during task execution. The full traceback is:

Traceback (most recent call last):

  File 
"/home/cl186051/.ansible/tmp/ansible-tmp-1454387154.58-221595234821842/ec2", 
line 3575, in <module>

    main()

  File 
"/home/cl186051/.ansible/tmp/ansible-tmp-1454387154.58-221595234821842/ec2", 
line 1415, in main

    (tagged_instances, instance_dict_array, new_instance_ids, changed) = 
enforce_count(module, ec2, vpc)

  File 
"/home/cl186051/.ansible/tmp/ansible-tmp-1454387154.58-221595234821842/ec2", 
line 801, in enforce_count

    = create_instances(module, ec2, vpc, override_count=to_create)

  File 
"/home/cl186051/.ansible/tmp/ansible-tmp-1454387154.58-221595234821842/ec2", 
line 1029, in create_instances

    res = ec2.run_instances(**params)

  File 
"/usr/local/lib64/python2.6/site-packages/boto-2.38.0-py2.6.egg/boto/ec2/connection.py",
 
line 930, in run_instances

    params['UserData'] = base64.b64encode(user_data).decode('utf-8')

  File "/usr/lib64/python2.6/base64.py", line 53, in b64encode

    encoded = binascii.b2a_base64(s)[:-1]

TypeError: b2a_base64() argument 1 must be string or read-only buffer, not 
dict



If I add single quotes around the JSON data read from the file (user_data: 
"'{{ lookup('file', '/tmp/metadata.txt') }}'")  then the playbook runs 
successfully, but the user_data value retrieved from the metadata server 
isn't valid JSON unless the leading and trailing single quotes are removed 
first.


Is there a better way to insert JSON data into the user_data?


Thanks in advance!


    Chris



-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/8e62bd9a-adee-4233-8513-e2c62907d56a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to