1#  Copyright (c) 2019 Red Hat, Inc.
2#
3#  Permission is hereby granted, free of charge, to any person obtaining a copy
4#  of this software and associated documentation files (the "Software"), to
5#  deal in the Software without restriction, including without limitation the
6#  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7#  sell copies of the Software, and to permit persons to whom the Software is
8#  furnished to do so, subject to the following conditions:
9#
10#  The above copyright notice and this permission notice shall be included in
11#  all copies or substantial portions of the Software.
12#
13#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19#  DEALINGS IN THE SOFTWARE.
20
21import os
22
23from molecule import logger
24from molecule import util
25from molecule.verifier import base
26
27log = logger.get_logger(__name__)
28
29
30class Ansible(base.Base):
31    """
32    `Ansible`_ is not the default test runner.
33
34    Molecule executes a playbook (`verify.yml`) located in the role's
35    `scenario.directory`.
36
37    .. code-block:: yaml
38
39        verifier:
40          name: ansible
41          lint:
42            name: ansible-lint
43
44    The testing can be disabled by setting ``enabled`` to False.
45
46    .. code-block:: yaml
47
48        verifier:
49          name: ansible
50          enabled: False
51
52    Environment variables can be passed to the verifier.
53
54    .. code-block:: yaml
55
56        verifier:
57          name: ansible
58          env:
59            FOO: bar
60    """
61
62    @property
63    def name(self):
64        return 'ansible'
65
66    @property
67    def default_options(self):
68        return {}
69
70    @property
71    def default_env(self):
72        env = util.merge_dicts(os.environ.copy(), self._config.env)
73        return util.merge_dicts(env, self._config.provisioner.env)
74
75    def execute(self):
76        if not self.enabled:
77            msg = 'Skipping, verifier is disabled.'
78            log.warn(msg)
79            return
80
81        msg = 'Running Ansible Verifier'
82        log.info(msg)
83
84        self._config.provisioner.verify()
85
86        msg = 'Verifier completed successfully.'
87        log.success(msg)
88