# Custom facts that require sudo

**URL:** <https://forum.ansible.com/t/custom-facts-that-require-sudo/16174>\
**Category:** Ansible Project\
**Created:** [September 10, 2014, 4:08pm UTC](https://forum.ansible.com/t/custom-facts-that-require-sudo/16174 "2014-09-10T16:08:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [September 10, 2014, 4:08pm UTC](https://forum.ansible.com/t/custom-facts-that-require-sudo/16174/1 "2014-09-10T16:08:48Z")

</div>

I ’ ve written some custom facts that require sudo to be executed (Those are scripts that parse some files in /etc/) .

My custom facts scripts are of course not executed when ansible is gathering facts. On the other hand I need the facts gathered as ansible user.  
My workaround for this is now:

`

- name: retrieve nonsudo facts  
setup:  
register: nsfacts

- name: retr. sudo facts  
setup:  
sudo: yes

`

Is there a feature to cover this problem? Maybe some way to tell ansible to complete nonsudo facts by sudo facts?

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 10, 2014, 4:22pm UTC](https://forum.ansible.com/t/custom-facts-that-require-sudo/16174/2 "2014-09-10T16:22:18Z")

</div>

"My custom facts scripts are of course not executed when ansible is gathering facts. "

I’m confused about this, do you mean “facts.d” facts?

If you are writing local facts they would in fact be gathered when you are gathering facts.

It seems you might be calling your own facts module and named it “setup”, which you should not do, really, but I’m unclear if you did.  
You can also set “sudo: True” at play level.

It may be that you want to write a “my\_facts” module instead, unless you have a specific need for facts.d.

Let me know a bit more info and we’ll get closer to the answer.

Thanks!

---

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [September 10, 2014, 4:54pm UTC](https://forum.ansible.com/t/custom-facts-that-require-sudo/16174/3 "2014-09-10T16:54:30Z")

</div>

Sorry for the confusion.  
I got a custom fact script on the remote machine.

`/etc/ansible/facts.d/node_guid.fact`

which looks like this:

`  
#! /usr/bin/env python  
import sys  
import json

def get\_prop(line):  
return line.split(“=”)[1].strip()

try:  
f = open(‘/etc/zypp/credentials.d/NCCcredentials’)  
except IOError:  
print(“{}”)  
sys.exit(0)

context = {}  
keys = [‘username’]

for line in f:  
for key in keys:  
if line.startswith(key):  
context[key] = get\_prop(line)

print json.dumps(context)  
sys.exit(0)

`

'`/etc/zypp/credentials.d/NCCcredentials' is set readable only for root. Therefore 'node_guid.fact' only returns facts as sudo. Which is the reason why I call the setup module with `sudo:yes``

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 11, 2014, 2:16pm UTC](https://forum.ansible.com/t/custom-facts-that-require-sudo/16174/4 "2014-09-11T14:16:25Z")

</div>

Yeah that seems fine.

You will want to turn off “gather\_facts” and call it explicitly.

What you are doing with site facts is a little weird, most folks would probably do a fact module and rather than calling it twice, split different things into different modules.
