From 2e1c889275bcdbb2d31665d0f67fc510ed008640 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Tue, 19 Dec 2017 17:30:10 -0800 Subject: [PATCH] Have command nanny tell you how to disable the warning (#34061) * Have command nanny tell you how to disable the warning --- lib/ansible/modules/commands/command.py | 17 +++++++++++++++-- .../targets/command_shell/tasks/main.yml | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/commands/command.py b/lib/ansible/modules/commands/command.py index 0ab85ff128b..7e1e072702c 100644 --- a/lib/ansible/modules/commands/command.py +++ b/lib/ansible/modules/commands/command.py @@ -132,10 +132,23 @@ def check_command(module, commandline): 'dnf': 'dnf', 'zypper': 'zypper'} become = ['sudo', 'su', 'pbrun', 'pfexec', 'runas', 'pmrun'] command = os.path.basename(commandline.split()[0]) + + disable_suffix = "If you need to use command because {mod} is insufficient you can add" \ + " warn=False to this command task or set command_warnings=False in" \ + " ansible.cfg to get rid of this message." + substitutions = {'mod': None, 'cmd': command} + if command in arguments: - module.warn("Consider using file module with %s rather than running %s" % (arguments[command], command)) + msg = "Consider using the {mod} module with {subcmd} rather than running {cmd}. " + disable_suffix + substitutions['mod'] = 'file' + substitutions['subcmd'] = arguments[command] + module.warn(msg.format(**substitutions)) + if command in commands: - module.warn("Consider using %s module rather than running %s" % (commands[command], command)) + msg = "Consider using the {mod} module rather than running {cmd}. " + disable_suffix + substitutions['mod'] = commands[command] + module.warn(msg.format(**substitutions)) + if command in become: module.warn("Consider using 'become', 'become_method', and 'become_user' rather than running %s" % (command,)) diff --git a/test/integration/targets/command_shell/tasks/main.yml b/test/integration/targets/command_shell/tasks/main.yml index 20ff3aeae6f..b8d54381874 100644 --- a/test/integration/targets/command_shell/tasks/main.yml +++ b/test/integration/targets/command_shell/tasks/main.yml @@ -43,7 +43,7 @@ assert: that: - "tar.warnings | length() == 1" - - "'Consider using unarchive module rather than running tar' in tar.warnings[0]" + - "'Consider using the unarchive module rather than running tar' in tar.warnings[0]" - name: use command to execute chown command: chown -h @@ -54,7 +54,7 @@ assert: that: - "chown.warnings | length() == 1" - - "'Consider using file module with owner rather than running chown' in chown.warnings[0]" + - "'Consider using the file module with owner rather than running chown' in chown.warnings[0]" - name: use command with unsupported executable arg command: ls /dev/null executable=/bogus