Implement `requires_command` attribute for facts.
This modifies the command to first check if the command exists, and only
if so execute the actual fact command.
This will enable removing the `|| true` part of fact commands, removing
potentially hidden errors.
@@ -82,6 +82,8 @@ class FactBase(object):
shell_executable = None
+ requires_command = None
+
@staticmethod
def default():
'''
@@ -193,6 +195,11 @@ def get_facts(state, name, args=None, ensure_hosts=None, apply_failed_hosts=True
command = command(*host_args)
+ if fact.requires_command:
+ command = '! command -v {0} > /dev/null || ({1})'.format(
+ fact.requires_command, command,
+ )
+
greenlet = state.fact_pool.spawn(
host.run_shell_command,
command,
