2 # -*- coding: utf-8 -*-
4 # (c) 2015, Linus Unnebäck <linus@folkdatorn.se>
6 # This file is part of Ansible
8 # This module is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This software is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this software. If not, see <http://www.gnu.org/licenses/>.
21 from __future__ import absolute_import
25 short_description: Run targets in a Makefile
26 requirements: [ make ]
28 author: Linus Unnebäck (@LinusU) <linus@folkdatorn.se>
30 - Run targets in a Makefile.
39 - Any extra parameters to pass to make
44 - Any extra options to pass to make
49 - cd into this directory before running make
54 # Build the default target
55 - make: chdir=/home/ubuntu/cool-project
57 # Run `install` target as root
58 - make: chdir=/home/ubuntu/cool-project target=install
61 # Pass in extra arguments to build
63 chdir: /home/ubuntu/cool-project
70 # TODO: Disabled the RETURN as it was breaking docs building. Someone needs to
75 def format_params(params):
76 return [k + '=' + str(v) for k, v in params.items()]
79 def push_arguments(cmd, args):
80 if args['extra_args'] is not None:
81 cmd.extend(shlex.split(args['extra_args']))
82 if args['target'] is not None:
83 cmd.append(args['target'])
84 if args['params'] is not None:
85 cmd.extend(format_params(args['params']))
89 def check_changed(make_path, module, args):
90 cmd = push_arguments([make_path, '--question'], args)
91 rc, _, __ = module.run_command(cmd, check_rc=False, cwd=args['chdir'])
95 def run_make(make_path, module, args):
96 cmd = push_arguments([make_path], args)
97 module.run_command(cmd, check_rc=True, cwd=args['chdir'])
101 module = AnsibleModule(
102 supports_check_mode=True,
104 target=dict(required=False, default=None, type='str'),
105 params=dict(required=False, default=None, type='dict'),
106 extra_args=dict(required=False, default=None, type='str'),
107 chdir=dict(required=True, default=None, type='str'),
113 target=module.params['target'],
114 params=module.params['params'],
115 extra_args=module.params['extra_args'],
116 chdir=module.params['chdir'],
118 make_path = module.get_bin_path('make', True)
120 # Check if target is up to date
121 args['changed'] = check_changed(make_path, module, args)
123 # Check only; don't modify
124 if module.check_mode:
125 module.exit_json(changed=args['changed'])
127 # Target is already up to date
128 if not args['changed']:
129 module.exit_json(**args)
131 run_make(make_path, module, args)
132 module.exit_json(**args)
134 from ansible.module_utils.basic import *
136 if __name__ == '__main__':