- 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: userdataTASK [debug] ***************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"userdata": "nx-app=web nx-name=my.hostname.net nx-state=standby nx-system=prod nx-version=01"
} - 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"} 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 }}"