You can easily do it in shell or any programming language as long as
you return JSON format.
Be careful with your prints and your shell module can echo back JSON just fine.
Ok, but I’m not getting the variables inside the template:
Extremely simplified, just to understand what I’m getting:
1.) The shell module I’m sending:
mmaas@XXXXXX:~/playbooks$ cat ./XXXXXX/library/omgeving
#!/bin/bash
cat /etc/ansible/local_vars
2.) The contents of /etc/ansible/local_vars on the host:
mmaas@xmonopsview:/etc/ansible$ cat ./local_vars
{
“host”: “XXXXXX”,
“port”: “50000”
}
3.) The playbook:
mmaas@XXXXXX:~/playbooks$ cat ./XXXXXX/local_vars_test.yml
vars:
email: unixbeheer@XXXXXX.nl
smarthost: pex1.cname.XXXXXX.nl
user: mmaas
sudo: true
tasks:
action: omgeving
- name: Template neerzetten
action: template src=…/templates/local_vars_test.j2 dest=/etc/ansible/templated.conf owner=root group=root mode=0644
4.) And the result when running:
mmaas@XXXXXX:~/playbooks$ ansible-playbook -K ./XXXXXX/local_vars_test.yml -v
sudo password:
PLAY [XXXXXX] *********************
GATHERING FACTS *********************
ok: [XXXXXX]
TASK: [Local vars ophalen] *********************
ok: [XXXXXX] => {“host”: “XXXXXX”, “port”: “50000”}
TASK: [Template neerzetten] *********************
ok: [XXXXXX] => {“changed”: false, “dest”: “/etc/ansible/templated.conf”, “group”: “root”, “md5sum”: “68b329da9893e34099c7d8ad5cb9c940”, “mode”: “0644”, “owner”: “root”, “src”: “/home/mmaas/.ansible/tmp/ansible-1352902982.54-257885461491988/source”, “state”: “file”}
PLAY RECAP *********************
XXXXXX: ok=3 changed=0 unreachable=0 failed=0
But the “/etc/ansible/templated.conf” on the intended host is empty after this run.
4.) The template file is also very simple for now:
mmaas@XXXXXX:~/playbooks$ cat ./templates/local_vars_test.j2
{{ host }}
{{ port }}
Thanks again,
Mark