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-data return_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.

Reply via email to