Search code examples
ansibleansible-2.x

supports_check_mode for non-Python module


An Ansible module written in Python can support check mode by setting supports_check_mode=True:

module = AnsibleModule(
    argument_spec = dict(...),
    supports_check_mode=True
)

Now I have a 700+ lines Ruby script I'd like to turn into a module and would like to avoid translating it to Python.

Is there a way how to support check mode for non-Python modules?


Solution

  • Ansible will pass an argument _ansible_check_mode to the module, which is true if you are in check mode.

    Remember that the arguments are put in a file, and the path to the file is argument #2.

    Here is a PHP example:

    ./library/test_module.php

    #!/usr/bin/env php
    <?php
    
    // WANT_JSON Causes ansible to store args in JSON
    
    $args = json_decode(file_get_contents($argv[1]), true);
    
    $check_mode = !empty($args['_ansible_check_mode']);
    
    $output = array(
        'changed' => false,
        'checking' => $check_mode
    );
    
    echo json_encode($output);
    

    Matching playbook:

    ./test_module.yml

    ---
    - hosts: localhost
      gather_facts: no
      become: no
    
      tasks:
    
        - test_module:
            key: value