fix check mode for all options

also simplified code paths and changed import away from *
fixes #19476
pull/19603/head
Brian Coca 9 years ago committed by Brian Coca
parent 91094ec96c
commit 3691c784d2

@ -342,6 +342,9 @@ EXAMPLES = '''
protocol: tcp protocol: tcp
''' '''
# import module snippets
from ansible.module_utils.basic import AnsibleModule
def append_param(rule, param, flag, is_list): def append_param(rule, param, flag, is_list):
if is_list: if is_list:
for item in param: for item in param:
@ -519,46 +522,39 @@ def main():
# Check if chain option is required # Check if chain option is required
if args['flush'] is False and args['chain'] is None: if args['flush'] is False and args['chain'] is None:
module.fail_json( module.fail_json( msg="Either chain or flush parameter must be specified.")
msg="Either chain or flush parameter must be specified.")
# Flush the table # Flush the table
if args['flush'] is True: if args['flush']:
flush_table(iptables_path, module, module.params) args['changed'] = True
module.exit_json(**args) if not module.check_mode:
flush_table(iptables_path, module, module.params)
# Set the policy # Set the policy
if module.params['policy']: elif module.params['policy']:
set_chain_policy(iptables_path, module, module.params) args['changed'] = True
module.exit_json(**args) if not module.check_mode:
set_chain_policy(iptables_path, module, module.params)
insert = (module.params['action'] == 'insert')
rule_is_present = check_present(iptables_path, module, module.params) # Chain
should_be_present = (args['state'] == 'present')
# Check if target is up to date
args['changed'] = (rule_is_present != should_be_present)
# Check only; don't modify
if module.check_mode:
module.exit_json(changed=args['changed'])
# Target is already up to date
if args['changed'] is False:
module.exit_json(**args)
if should_be_present:
if insert:
insert_rule(iptables_path, module, module.params)
else:
append_rule(iptables_path, module, module.params)
else: else:
remove_rule(iptables_path, module, module.params) insert = (module.params['action'] == 'insert')
rule_is_present = check_present(iptables_path, module, module.params)
should_be_present = (args['state'] == 'present')
# Check if target is up to date
args['changed'] = (rule_is_present != should_be_present)
if args['changed'] and not module.check_mode:
if should_be_present:
if insert:
insert_rule(iptables_path, module, module.params)
else:
append_rule(iptables_path, module, module.params)
else:
remove_rule(iptables_path, module, module.params)
module.exit_json(**args) module.exit_json(**args)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save