Ok, so the ec2_facts module DOES pull down the EC2 information - thanks for
that.
When keys are created, all hypens (-) are converted to underscores (_), so
the key is ansible_ec2_user_data
The value of this key still comes down as "a string, which happens to be
json", so it doesn't work to ask for {{ ansible_ec2_user_data.hostname }}
However, adding in the "from_json" filter to that does work, and got me to
this final solution:
- action: ec2_facts
- hostname: name={{ (ansible_ec2_user_data|from_json).hostname }}
Thanks for all the tips - putting them together, and going through the
source code, got me to this final solution !
Ben
On Monday, 17 March 2014 23:30:48 UTC+11, Matt Martz wrote:
>
> It is actually possible to use set_fact with json data already, or to use
> info from json data for pretty much anything. You just need to use the
> from_json filter.
>
> - set_fact:
> hostname: "{{ (user_data.content|from_json).hostname }}"
>
> or in the example given:
>
> - name: Set hostname from user data
>
>
> command: "hostname {{ (user_data.content|from_json).hostname }}"
>
>
> when: user_data is defined
>
> Although 2 things to mention:
>
> 1) As mentioned, I do think that the ec2_facts module already grabs
> user-data <
> https://github.com/ansible/ansible/blob/devel/library/cloud/ec2_facts#L61>
>
> If I am reading the code correctly, would end up looking like:
>
> {{ hostvars[inventory_hostname]['ansible_ec2_user-data']['hostname'] }}
>
> 2) Instead of issuing a hostname change via the command module, look at
> the hostname module <http://docs.ansible.com/hostname_module.html>
> --
> Matt Martz
> [email protected] <javascript:>
>
> On March 17, 2014 at 6:49:59 AM, Dag Wieers ([email protected] <javascript:>)
> wrote:
>
> On Mon, 17 Mar 2014, Ben Turner wrote:
>
> > Looks like all "user-data" in AWS is just a "string" at the end of the
> day.
> >
> > So there is no way for Ansible to parse that string and pull out values,
> > without a third-party module such as
> > https://github.com/jpmens/ansible-ec2-userdata say ?
>
> It would be a very simple improvement to set_fact to allow a string to
> become a json-object, however the syntax of set_fact was made so that
> every parameter is a fact name.
>
> However a set_fact_from_json module would be super-easy to implement, so
> that you can combine it with uri (or other registered variables).
>
> Another option is to extend the uri module with an option like:
> content_as_json=yes
>
> My prefered solution is to include this in the inventory step (outside of
> Ansible) so that it can be processed asynchronous (and cached) to Ansible.
>
> --
> -- dag wieers, [email protected] <javascript:>, http://dag.wieers.com/
> -- dagit linux solutions, [email protected] <javascript:>,
> http://dagit.net/
>
> [Any errors in spelling, tact or fact are transmission errors]
>
> --
> 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] <javascript:>.
> To post to this group, send email to
> [email protected]<javascript:>.
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/alpine.LRH.2.02.1403171242390.6649%40pikachu.3ti.be.
>
>
> 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/e5746c7c-55e1-47a3-b2e3-ed11194103e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.