# How can I set\_fact based on a "registered" result?

**URL:** <https://forum.ansible.com/t/how-can-i-set-fact-based-on-a-registered-result/25418>\
**Category:** Ansible Project\
**Created:** [September 6, 2017, 4:14pm UTC](https://forum.ansible.com/t/how-can-i-set-fact-based-on-a-registered-result/25418 "2017-09-06T16:14:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [September 6, 2017, 4:14pm UTC](https://forum.ansible.com/t/how-can-i-set-fact-based-on-a-registered-result/25418/1 "2017-09-06T16:14:10Z")

</div>

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](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" }`

But I’m having trouble pulling the userdata. 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 }}”  
`

`TASK [Set facts needed from userdata] ***************************************************************************************************************** fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'nx'\n\nThe error appears to have been in 'data.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Set facts needed from userdata\n ^ here\n"}`

What am I missing? I’ve tried several things but haven’t hit on the solution so I’m open to any suggestions.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 6, 2017, 5:07pm UTC](https://forum.ansible.com/t/how-can-i-set-fact-based-on-a-registered-result/25418/2 "2017-09-06T17:07:12Z")

</div>

> 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  
> &nbsp;&nbsp;&nbsp;hosts: localhost  
> &nbsp;&nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Get userdata  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uri:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: [http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return\_content: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: metadata  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Set userdata fact  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userdata: "{{ metadata.content | from\_yaml }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: userdata
> 
> which returns:
> 
> TASK [debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"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:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Set facts needed from userdata  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system: "{{ userdata.nx-system }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;version: "{{ userdata.nx-version }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app: "{{ userdata.nx-app }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: "{{ userdata.nx-state }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [September 6, 2017, 5:39pm UTC](https://forum.ansible.com/t/how-can-i-set-fact-based-on-a-registered-result/25418/3 "2017-09-06T17:39:37Z")

</div>

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](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 }}”  
`
