From 67ee30f13520557715c1822e0926a4cf9d2c220c Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Wed, 9 May 2012 15:08:45 -0700 Subject: [PATCH] Apply logging of arguments to modules (issue #122) The ohai and facter modules use /usr/bin/logger to log the fact that they have been invoked. I added 'import os' to the ping module so that it could have the same syslog statements as the other modules. I separated the condensed: shlex.split(open(argfile, 'r').read()) into two separate statements similar to the other modules. --- apt | 3 +++ async_status | 7 ++++++- async_wrapper | 4 ++++ command | 3 +++ copy | 7 ++++++- facter | 1 + file | 3 +++ git | 3 +++ group | 3 +++ ohai | 1 + ping | 6 ++++++ service | 3 +++ setup | 4 ++++ slurp | 7 ++++++- user | 3 +++ virt | 3 +++ yum | 3 +++ 17 files changed, 61 insertions(+), 3 deletions(-) diff --git a/apt b/apt index 57e98e49f5d..5cb50cb9d03 100755 --- a/apt +++ b/apt @@ -25,6 +25,7 @@ import os import sys import shlex import subprocess +import syslog import traceback APT_PATH = "/usr/bin/apt-get" @@ -113,6 +114,8 @@ if not os.path.exists(APT_PATH): argfile = sys.argv[1] args = open(argfile, 'r').read() items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_json(msg='the module requires arguments -a') diff --git a/async_status b/async_status index bf5b0a82d3e..33c09e653c7 100755 --- a/async_status +++ b/async_status @@ -28,13 +28,18 @@ import subprocess import sys import datetime import traceback +import syslog # =========================================== # FIXME: better error handling argsfile = sys.argv[1] -items = shlex.split(file(argsfile).read()) +args = open(argsfile, 'r').read() +items = shlex.split(args) + +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) params = {} for x in items: diff --git a/async_wrapper b/async_wrapper index 22f572d4fa2..61ae5117ef7 100755 --- a/async_wrapper +++ b/async_wrapper @@ -30,6 +30,7 @@ import datetime import traceback import signal import time +import syslog def daemonize_self(): # daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 @@ -76,6 +77,9 @@ wrapped_module = sys.argv[3] argsfile = sys.argv[4] cmd = "%s %s" % (wrapped_module, argsfile) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:])) + # setup logging directory logdir = os.path.expanduser("~/.ansible_async") log_path = os.path.join(logdir, jid) diff --git a/command b/command index 77ccb2c1a6a..761ba38a1e0 100755 --- a/command +++ b/command @@ -29,9 +29,12 @@ import datetime import traceback import shlex import os +import syslog argfile = sys.argv[1] args = open(argfile, 'r').read() +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) shell = False diff --git a/copy b/copy index f6cf6028610..f9c85ef2bbc 100755 --- a/copy +++ b/copy @@ -21,6 +21,7 @@ import sys import os import shlex +import syslog # =========================================== # convert arguments of form a=b c=d @@ -32,7 +33,11 @@ if len(sys.argv) == 1: argfile = sys.argv[1] if not os.path.exists(argfile): sys.exit(1) -items = shlex.split(open(argfile, 'r').read()) + +args = open(argfile, 'r').read() +items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) params = {} diff --git a/facter b/facter index 6b8f2bad17a..46cdf889cca 100755 --- a/facter +++ b/facter @@ -22,4 +22,5 @@ # facter # ruby-json +/usr/bin/logger -t ansible-facter Invoked as-is /usr/bin/facter --json 2>/dev/null diff --git a/file b/file index f06b210a73d..2872d3879c7 100755 --- a/file +++ b/file @@ -25,6 +25,7 @@ import shutil import stat import grp import pwd +import syslog try: import selinux HAVE_SELINUX=True @@ -125,6 +126,8 @@ def selinux_context(path): argfile = sys.argv[1] args = open(argfile, 'r').read() items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_kv(msg='the module requires arguments -a') diff --git a/git b/git index 7e9c77e0fb2..7388bad3cb0 100755 --- a/git +++ b/git @@ -31,6 +31,7 @@ import os import sys import shlex import subprocess +import syslog # =========================================== # Basic support methods @@ -57,6 +58,8 @@ if not os.path.exists(argfile): args = open(argfile, 'r').read() items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_json(msg="the command module requires arguments (-a)") diff --git a/group b/group index c065cb51b98..fc21c5c1a32 100755 --- a/group +++ b/group @@ -26,6 +26,7 @@ import grp import shlex import subprocess import sys +import syslog GROUPADD = "/usr/sbin/groupadd" GROUPDEL = "/usr/sbin/groupdel" @@ -131,6 +132,8 @@ if len(sys.argv) == 2 and os.path.exists(sys.argv[1]): else: args = ' '.join(sys.argv[1:]) items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_json(msg='the module requires arguments -a') diff --git a/ohai b/ohai index 3b6f3ae144c..e4cf0272651 100755 --- a/ohai +++ b/ohai @@ -18,4 +18,5 @@ # along with Ansible. If not, see . # +/usr/bin/logger -t ansible-ohai Invoked as-is /usr/bin/ohai diff --git a/ping b/ping index e5b068c8c00..ccc7cc722c9 100755 --- a/ping +++ b/ping @@ -22,4 +22,10 @@ try: except ImportError: import simplejson as json +import os +import syslog + +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked as-is') + print json.dumps({ "ping" : "pong" }) diff --git a/service b/service index 262db4094e9..63c4fe8bc70 100755 --- a/service +++ b/service @@ -25,6 +25,7 @@ import sys import shlex import subprocess import os.path +import syslog # TODO: switch to fail_json and other helper functions # like other modules are using @@ -95,6 +96,8 @@ def _do_enable(name, enable): argfile = sys.argv[1] args = open(argfile, 'r').read() items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_json(dict(failed=True, msg='this module requires arguments (-a)')) diff --git a/setup b/setup index 6345a9fe09a..0f99a6cc0f8 100755 --- a/setup +++ b/setup @@ -31,6 +31,7 @@ import socket import struct import subprocess import traceback +import syslog try: import json @@ -295,6 +296,9 @@ except: (k,v) = opt.split("=") setup_options[k]=v +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % setup_options) + ansible_file = os.path.expandvars(setup_options.get('metadata', DEFAULT_ANSIBLE_SETUP)) ansible_dir = os.path.dirname(ansible_file) diff --git a/slurp b/slurp index 36e84ecc09d..acb6b5f83ff 100755 --- a/slurp +++ b/slurp @@ -21,6 +21,7 @@ import sys import os import shlex import base64 +import syslog try: import json @@ -36,7 +37,11 @@ if len(sys.argv) == 1: argfile = sys.argv[1] if not os.path.exists(argfile): sys.exit(1) -items = shlex.split(open(argfile, 'r').read()) + +args = open(argfile, 'r').read() +items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) params = {} for x in items: diff --git a/user b/user index a397b1bebc0..5be3502f940 100755 --- a/user +++ b/user @@ -27,6 +27,7 @@ import grp import shlex import subprocess import sys +import syslog try: import spwd HAVE_SPWD=True @@ -264,6 +265,8 @@ if not os.path.exists(USERDEL): argfile = sys.argv[1] args = open(argfile, 'r').read() items = shlex.split(args) +syslog.openlog('ansible-%s' % os.path.basename(__file__)) +syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): fail_json(msg='the module requires arguments -a') diff --git a/virt b/virt index e2dee4c182d..ac048bab2d3 100755 --- a/virt +++ b/virt @@ -27,6 +27,7 @@ except ImportError: import os import sys import subprocess +import syslog try: import libvirt except ImportError: @@ -366,6 +367,8 @@ def main(): args = open(argfile, 'r').read() items = shlex.split(args) + syslog.openlog('ansible-%s' % os.path.basename(__file__)) + syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): return VIRT_FAILED, msg diff --git a/yum b/yum index ee773ee15f2..49d3bad8abb 100755 --- a/yum +++ b/yum @@ -27,6 +27,7 @@ import datetime import shlex import re import traceback +import syslog try: @@ -299,6 +300,8 @@ def main(): args = open(argfile, 'r').read() items = shlex.split(args) + syslog.openlog('ansible-%s' % os.path.basename(__file__)) + syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not len(items): msg = "the yum module requires arguments (-a)"