summaryrefslogtreecommitdiff
path: root/bin/ansible
blob: 12ad89fcff37974dfd56645c3613d8ecf4561203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

########################################################
from __future__ import (absolute_import)
__metaclass__ = type

__requires__ = ['ansible']
try:
    import pkg_resources
except Exception:
    # Use pkg_resources to find the correct versions of libraries and set
    # sys.path appropriately when there are multiversion installs.  But we
    # have code that better expresses the errors in the places where the code
    # is actually used (the deps are optional for many code paths) so we don't
    # want to fail here.
    pass

import os
import sys

from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError
from ansible.utils.display import Display

########################################################

if __name__ == '__main__':

    cli = None
    display = Display()
    me = os.path.basename(__file__)

    try:
        if me == 'ansible-playbook':
            from ansible.cli.playbook import PlaybookCLI as mycli
        elif me == 'ansible':
            from ansible.cli.adhoc import AdHocCLI as mycli
        elif me == 'ansible-pull':
            from ansible.cli.pull import PullCLI as mycli
        elif me == 'ansible-doc':
            from ansible.cli.doc import DocCLI as mycli
        elif me == 'ansible-vault':
            from ansible.cli.vault import VaultCLI as mycli
        elif me == 'ansible-galaxy':
            from ansible.cli.galaxy import GalaxyCLI as mycli

        cli = mycli(sys.argv, display=display)
        if cli:
            cli.parse()
            sys.exit(cli.run())
        else:
            raise AnsibleError("Program not implemented: %s" % me)

    except AnsibleOptionsError as e:
        cli.parser.print_help()
        display.display(str(e), stderr=True, color='red')
        sys.exit(5)
    except AnsibleParserError as e:
        display.display(str(e), stderr=True, color='red')
        sys.exit(4)
# TQM takes care of these, but leaving comment to reserve the exit codes
#    except AnsibleHostUnreachable as e:
#        display.display(str(e), stderr=True, color='red')
#        sys.exit(3)
#    except AnsibleHostFailed as e:
#        display.display(str(e), stderr=True, color='red')
#        sys.exit(2)
    except AnsibleError as e:
        display.display(str(e), stderr=True, color='red')
        sys.exit(1)
    except KeyboardInterrupt:
        display.error("interrupted")
        sys.exit(99)