# inserting JSON blob into ec2 user\_data

**URL:** <https://forum.ansible.com/t/inserting-json-blob-into-ec2-user-data/20416>\
**Category:** Ansible Project\
**Created:** [February 2, 2016, 5:01am UTC](https://forum.ansible.com/t/inserting-json-blob-into-ec2-user-data/20416 "2016-02-02T05:01:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopherlove68](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@christopherlove68](https://forum.ansible.com/u/christopherlove68)\
**Post date:** [February 2, 2016, 5:01am UTC](https://forum.ansible.com/t/inserting-json-blob-into-ec2-user-data/20416/1 "2016-02-02T05:01:10Z")

</div>

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

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
