I tried out some more debug statements; the .json suffix doesn't appear to
be present ?
The user-data "json" appears to be coming back as a string...
TASK: [common | debug var=user_data]
******************************************
ok: [ec2-54-253-114-32.ap-southeast-2.compute.amazonaws.com] => {
"item": "",
"user_data": {
"accept_ranges": "bytes",
"changed": false,
"connection": "close",
"content": "{\"hostname\":\"qa\"}",
"content_length": "17",
"content_location": "http://169.254.169.254/latest/user-data",
"content_type": "application/octet-stream",
"date": "Fri, 14 Mar 2014 02:26:12 GMT",
"etag": "\"1016948274\"",
"invocation": {
"module_args":
"url=http://169.254.169.254/latest/user-datareturn_content=yes",
"module_name": "uri"
},
"item": "",
"last_modified": "Thu, 13 Mar 2014 22:02:44 GMT",
"redirected": false,
"server": "EC2ws",
"status": 200
}
}
TASK: [common | debug var=user_data.json]
*************************************
ok: [ec2-54-253-114-32.ap-southeast-2.compute.amazonaws.com] => {
"item": "",
"user_data.json": "{{ user_data.json }}"
}
TASK: [common | debug var=user_data.content.json]
*****************************
ok: [ec2-54-253-114-32.ap-southeast-2.compute.amazonaws.com] => {
"item": "",
"user_data.content.json": "{{ user_data.content.json }}"
}
On 14 March 2014 12:15, Romeo Theriault <[email protected]> wrote:
> Hi Ben, If you want to access the JSON data it's available in the json
> subkey. So instead of:
>
> {{ user_data.content.hostname }}
>
> I believe it would be
>
> {{ user_data.json.hostname }}
>
> or something similar.
>
> Take a look at the {{ user_data.json }} or {{ user_data }} output with
> the debug module to get a better idea.
>
> Hope that helps,
> Romeo
>
> On Thu, Mar 13, 2014 at 2:35 PM, Ben Turner <[email protected]> wrote:
> > I've been trying to use the user-data provided by EC2 servers to
> determine a
> > servers hostname.
> >
> > So currently new server come up with a hostname of "ec2-123-234-345-456"
> or
> > similar, and I'd like in my ansible script to rename that server to
> > something like "demonstration".
> >
> > To do this, I've gone into my EC2 console, and added the following as
> > user-data to my server:
> >
> > {"hostname": "demonstration"}
> >
> > Then, in my ansible playbook, I've tried the following:
> >
> > - name: Load user data
> > uri: url=http://169.254.169.254/latest/user-datareturn_content=yes
> > register: user_data
> >
> > - debug: var=user_data.content
> >
> > - name: Set hostname from user data
> > command: hostname {{user_data.content.hostname}}
> > when: user_data is defined
> >
> > This produces the output (using the -v flag):
> >
> > PLAY [ec2]
> > ********************************************************************
> >
> > GATHERING FACTS
> > ***************************************************************
> > ok: [ec2-54-253-114-###.ap-southeast-2.compute.amazonaws.com]
> >
> > TASK: [common | Load user data]
> > ***********************************************
> > ok: [ec2-54-253-114-###.ap-southeast-2.compute.amazonaws.com] =>
> > {"accept_ranges": "bytes", "changed": false, "connection": "close",
> > "content": "{\"hostname\":\"demonstration\"}", "content_length": "28",
> > "content_location": "http://169.254.169.254/latest/user-data",
> > "content_type": "application/octet-stream", "date": "Fri, 14 Mar 2014
> > 00:30:30 GMT", "etag": "\"1016948274\"", "item": "", "last_modified":
> "Thu,
> > 13 Mar 2014 22:02:44 GMT", "redirected": false, "server": "EC2ws",
> "status":
> > 200}
> >
> > TASK: [common | debug var=user_data.content]
> > **********************************
> > ok: [ec2-54-253-114-###.ap-southeast-2.compute.amazonaws.com] => {
> > "item": "",
> > "user_data.content": {
> > "hostname": "demonstration"
> > }
> > }
> >
> > TASK: [common | Set hostname from user data]
> > **********************************
> > fatal: [ec2-54-253-114-###.ap-southeast-2.compute.amazonaws.com] =>
> One
> > or more undefined variables: 'unicode object' has no attribute 'hostname'
> >
> > So, whilst it seems that debugging user_data returns a JSON-like return
> > strucutre, and drilling down to user_data.content also returns a
> JSON-like
> > return structure, the actual JSON content returned by the uri task is
> unable
> > to be parsed using the Jinja2 dot notation, and instead appears to be an
> > impenetrable object, at least as far as I can determine.
> >
> > Is there a way of extracting this URI return content using core ansible
> > modules such that I can use it's JSON payload as ansible variables ?
> >
> > Regards,
> > Ben
> >
> > --
> > 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/978e1838-7964-4130-827f-2f82c0daa4f8%40googlegroups.com
> .
> > For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Romeo
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/FWSJulCzsQ8/unsubscribe.
> To unsubscribe from this group and all its topics, 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/CACUx58M%3D361LMPj4DFDjTrZQ0nNT57c32zLfKBm4HNupuxVtvQ%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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/CAGxBzUE-B76djVXnVZrxJcE%3DXoT54zkPTiqC9fbFZnVpcUU3ig%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.