Yep, realized I was trying to parse it as JSON, or YAML very much
incorrectly...
I have a solution now, though it's quite ugly and requires replacing a
character and splitting on newlines....
- name: Get userdata
uri:
url: http://169.254.169.254/latest/user-data
return_content: yes
register: rawdata
- name: Replace hyphen with undserscore since hyphens are not valid in
python variables
set_fact:
metadata: "{{ rawdata | replace('-','_') }}"
- name: Set userdata fact
set_fact:
userdata: "{{ metadata.content.split('\n') }}"
- set_fact:
"{{ item.split('=')[0] }}": "{{ item.split('=')[1] }}"
with_items: "{{ userdata }}"
On Wednesday, September 6, 2017 at 12:07:41 PM UTC-5, Kai Stian Olstad
wrote:
>
> On 06. sep. 2017 18:14, Dayton Jones wrote:
> > ansible-playbook 2.3.0.0
> > python version = 2.7.12
> >
> > I'm trying to set some facts based on the user-data given for an EC2
> > instance:
> >
> > - name: Initialization
> > hosts: localhost
> > connection: local
> > tasks:
> > - name: Get userdata
> > uri:
> > url: http://169.254.169.254/latest/user-data
> > return_content: yes
> > register: metadata
> > - name: Set userdata fact
> > set_fact:
> > userdata: "{{ metadata.content | from_yaml }}"
> > - debug:
> > var: userdata
> >
> > which returns:
> >
> > TASK [debug]
> >
> ***************************************************************************************************************************************************************
>
>
> > ok: [localhost] => {
> > "changed": false,
> > "userdata": "nx-app=web nx-name=my.hostname.net nx-state=standby
> > nx-system=prod nx-version=01"
> > }
>
> This show that userdata is a string.
>
>
> > But I'm having trouble pulling the userdata.<value> out:
> >
> > - name: Set facts needed from userdata
> > set_fact:
> > system: "{{ userdata.nx-system }}"
> > version: "{{ userdata.nx-version }}"
> > app: "{{ userdata.nx-app }}"
> > state: "{{ userdata.nx-state }}"
> > name: "{{ userdata.nx-name }}"
>
> Since userdata is a string it's not possible to use array syntax.
>
>
> > What am I missing? I've tried several things but haven't hit on the
> > solution so I'm open to any suggestions.
>
> What is the output of
> - debug: var=metadata.content
>
>
> --
> Kai Stian Olstad
>
--
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/f972014f-6a29-4d19-a8d5-758f3f78a900%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.