# Module provided facts. but using shell script

**URL:** <https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086>\
**Category:** Ansible Project\
**Created:** [November 14, 2012, 1:56pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086 "2012-11-14T13:56:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 14, 2012, 1:56pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/1 "2012-11-14T13:56:19Z")

</div>

Dear List,

I’d like to run a “site\_facts” shell program on the intended host to pick up some variables that are defined on the host’s filesystem.  
I read this: “[http://ansible.cc/docs/moduledev.html#module-provided-facts](http://ansible.cc/docs/moduledev.html#module-provided-facts)” and understand how it should be done using python and the json import thing.

Could it be done using a shell program though? I’d like to read a file on the host, and return the contents back ansible for use in later actions (Especially with the template module)

Is there a special invocation I need to use with my shell module so I can return facts for later use?

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 14, 2012, 2:12pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/2 "2012-11-14T14:12:24Z")

</div>

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.

--Michael

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 14, 2012, 2:32pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/3 "2012-11-14T14:32:55Z")

</div>

> 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
> 
> * * *
> 
> - hosts: XXXXXX
> 
> vars:
> 
> email: unixbeheer@XXXXXX.nl
> 
> smarthost: pex1.cname.XXXXXX.nl
> 
> user: mmaas
> 
> sudo: true
> 
> tasks:
> 
> - name: Local vars ophalen
> 
> 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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 14, 2012, 2:36pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/4 "2012-11-14T14:36:07Z")

</div>

Yeah, in order to create facts, it needs something like this:

{  
&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_facts" : {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"foo" : "some string",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"baz" : 1234  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

This will make $foo and $bar available.

Leaving out the "ansible\_facts" hash and surfacing foo/bar at the top  
level won't do anything.

This is how Ansible tells something is setting facts or not. Any  
module, even one that causes other side effects, can set variables in  
this way by including the "ansible\_facts" hash/dictionary.

Hope this helps!

--Michael

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 14, 2012, 2:42pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/5 "2012-11-14T14:42:18Z")

</div>

Excellent!

So that was the trick. Anyway to do this same thing but with the shorthand version?  
[http://ansible.cc/docs/moduledev.html#shorthand-vs-json](http://ansible.cc/docs/moduledev.html#shorthand-vs-json)

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 14, 2012, 2:50pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/6 "2012-11-14T14:50:52Z")

</div>

I think we need to remove the docs on the shorthand version because it  
doesn't work (i.e. not supported) anymore, but need to confirm.

It probably would be better to make it work again, if so, instead 🙂

What I'm suggesting is you print out a valid JSON string, actually.

You can do it over multiple lines if it makes it easier for the bash script.

---

<div class="post-metadata">

**Author:** ![Andreas\_Davour](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Andreas\_Davour](https://forum.ansible.com/u/Andreas_Davour)\
**Post date:** [November 15, 2012, 9:49am UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/7 "2012-11-15T09:49:10Z")

</div>

I have used the shorthand version just fine, trying to write a module  
in bash. I might not gte the business logic right, but getting the  
values back to ansible works fine. Twiddling "facts" might be  
something else, but I'd hate to see the shorthand system go away now  
when I've started using it.

/andreas

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 15, 2012, 12:55pm UTC](https://forum.ansible.com/t/module-provided-facts-but-using-shell-script/12086/8 "2012-11-15T12:55:33Z")

</div>

Yeah, we confirmed the shorthand version works fine as of last night,  
so I was incorrect in thinking I had broken it. I believe we even  
have a test 🙂
