├── .gitignore ├── LICENSE ├── README.md ├── ansible.cfg ├── test.yml ├── test_modules.py └── test_playbook.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # PyInstaller 26 | # Usually these files are written by a python script from a template 27 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 28 | *.manifest 29 | *.spec 30 | 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | htmlcov/ 37 | .tox/ 38 | .coverage 39 | .cache 40 | nosetests.xml 41 | coverage.xml 42 | 43 | # Translations 44 | *.mo 45 | *.pot 46 | 47 | # Django stuff: 48 | *.log 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Oriol Rius 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | Two examples describing how to use Ansible programmatically in Python. 4 | 5 | - test_modules.py : set up the environment to run an Ansible module 6 | - test_playbook.py: creates an inventory and loads a .yml file with a playbook, then runs the playbook and shows the results like Ansible-playbook CLI tool 7 | 8 | Other files: 9 | - ansible.cfg : just a basic Ansible configuration file 10 | - test.yml : simple playbook 11 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | host_key_checking = False 3 | remote_user = root 4 | log_path = ansible.log 5 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | - hosts: sample_group_name 2 | tasks: 3 | - name: just an uname 4 | command: uname -a 5 | -------------------------------------------------------------------------------- /test_modules.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import ansible.runner 3 | import ansible.playbook 4 | import ansible.inventory 5 | from ansible import callbacks 6 | from ansible import utils 7 | import json 8 | # the fastest way to set up the inventory 9 | 10 | # hosts list 11 | hosts = ["127.0.0.1"] 12 | # set up the inventory, if no group is defined then 'all' group is used by default 13 | example_inventory = ansible.inventory.Inventory(hosts) 14 | 15 | pm = ansible.runner.Runner( 16 | module_name = 'command', 17 | module_args = 'uname -a', 18 | timeout = 5, 19 | inventory = example_inventory, 20 | subset = 'all', # name of the hosts group 21 | private_key_file = "Host's private key", 22 | remote_user = 'user to login with, root is default user', 23 | remote_pass = 'user password' 24 | ) 25 | 26 | out = pm.run() 27 | 28 | print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': ')) 29 | 30 | -------------------------------------------------------------------------------- /test_playbook.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import ansible.runner 3 | import ansible.playbook 4 | import ansible.inventory 5 | from ansible import callbacks 6 | from ansible import utils 7 | import json 8 | 9 | ### setting up the inventory 10 | 11 | ## first of all, set up a host (or more) 12 | example_host = ansible.inventory.host.Host( 13 | name = '10.11.12.66', 14 | port = 22 15 | ) 16 | # with its variables to modify the playbook 17 | example_host.set_variable( 'var', 'foo') 18 | 19 | ## secondly set up the group where the host(s) has to be added 20 | example_group = ansible.inventory.group.Group( 21 | name = 'sample_group_name' 22 | ) 23 | example_group.add_host(example_host) 24 | 25 | ## the last step is set up the invetory itself 26 | example_inventory = ansible.inventory.Inventory() 27 | example_inventory.add_group(example_group) 28 | example_inventory.subset('sample_group_name') 29 | 30 | # setting callbacks 31 | stats = callbacks.AggregateStats() 32 | playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) 33 | runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) 34 | 35 | # creating the playbook instance to run, based on "test.yml" file 36 | pb = ansible.playbook.PlayBook( 37 | playbook = "test.yml", 38 | stats = stats, 39 | callbacks = playbook_cb, 40 | runner_callbacks = runner_cb, 41 | inventory = example_inventory, 42 | check=True 43 | ) 44 | 45 | # running the playbook 46 | pr = pb.run() 47 | 48 | # print the summary of results for each host 49 | print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': ')) 50 | --------------------------------------------------------------------------------