Django-Ansible
dj_ansible is a Django App that allow Django applications to integrate with Ansible. basiclly it allow you to store inventory data in database using DjangoModels, and provide an API to execute Json-like playbooks.
currently supported on Ansible <= 2.7.10
Quick Start
-
install django-ansible
pip install django-ansible -
Add "dj_ansible" to your INSTALLED_APPS setting like this
INSTALLED_APPS = [ ... 'dj_ansible', ] -
Run
python manage.py makemigrationsto create migrations for the tables. -
Run
python manage.py migrateto create the needed tables. -
add your inventory data to two tables
- ansible_network_groups (model:
AnsibleNetworkGroup()) - ansible_network_hosts (model:
AnsibleNetworkHost())
like following :
from dj_ansible.models import AnsibleNetworkHost,AnsibleNetworkGroup my_group = AnsibleNetworkGroup(name='cisco_firewalls', ansible_connection='network_cli', ansible_network_os='asa', ansible_become=True) my_group.save() host = AnsibleNetworkHost(host='my_asa', ansible_ssh_host='192.168.1.1', ansible_user='admin', ansible_ssh_pass='pass', ansible_become_pass='pass', group=my_group) host.save() - ansible_network_groups (model:
Running Play-books
to run play-books use the execute API to execute your play-books
Example
-
create play-book dictionary
my_play = dict( name="may_first_play", hosts='cisco_firewalls', become='yes', become_method='enable', gather_facts='no', tasks=[ dict(action=dict(module='asa_command', commands=['show version','show ip'])), dict(action=dict(module='asa_config', lines=['network-object host 10.1.0.1']) ] ) -
run
my_playusing theexecuteAPIfrom dj_ansible.ansible_kit import execute result = execute(my_play) # print the stats print(json.dumps(result.stats, indent=4)) # print execution results print(json.dumps(result.results, indent=4)) -
the
resultobject consist of two jsons:
-
first
state, which look like this:{ "all_ok": false, "total_hosts": 3, "has_changed": false, "duration": 12.400323867797852, "hosts_stats": { "ok_hosts": { "count": 2, "hosts": [ "fw2", "fw1" ] }, "failed_hosts": { "count": 1, "hosts": [ "ios_switch1" ] }, "processed_hosts": { "count": 3, "hosts": [ "fw2", "fw1", "ios_switch1" ] }, "changed_hosts": { "count": 0, "hosts": [] } }, "hosts": [ { "host": "fw2", "status": "ok", "changed": false }, { "host": "fw1", "status": "ok", "changed": false }, { "host": "ios_switch1", "status": "failed", "changed": false } ]} -
and
result, which looks like this:{ "failed": [ { "host": "ios_switch1", "tasks": [ { "name": "asa_command", "result": { "msg": "timed out", "_ansible_no_log": false } } ] } ], "success": [ { "host": "fw2", "tasks": [ { "name": "asa_command", "result": { "invocation": { "module_args": { "username": null, "authorize": null, "password": null, "passwords": null, "context": null, "retries": 10, "auth_pass": null, "interval": 1, "commands": [ "show version" ], "host": null, "ssh_keyfile": null, "timeout": null, "provider": null, "wait_for": null, "port": null, "match": "all" } }, "stdout_lines": [ [ "Cisco Adaptive Security Appliance Software Version 9.5(3)6 ", "Device Manager Version 7.1(3)", .... "Configuration last modified by enable_15 at 12:55:31.479 EDT Sun Apr 7 2019" ] ], "changed": false, "stdout": [ "Cisco Adaptive Security Appliance Software Version 9.5(3)6 \nDevice Manager Version 7.1(3)\n\n... ], "_ansible_parsed": true, "_ansible_no_log": false } } ] }, { "host": "fw1", "tasks": [ { "name": "asa_command", "result": { "invocation": { "module_args": { "username": null, "authorize": null, "password": null, "passwords": null, "context": null, "retries": 10, "auth_pass": null, "interval": 1, "commands": [ "show version" ], "host": null, "ssh_keyfile": null, "timeout": null, "provider": null, "wait_for": null, "port": null, "match": "all" } }, "stdout_lines": [ [ "Cisco Adaptive Security Appliance Software Version 9.1(7)16 ", "", "Compiled on Thu 30-Mar-17 17:39 by builders", "System image file is \"disk0:/asa917-16-k8.bin\"", "Config file at boot was \"startup-config\"", "", .... "Configuration register is 0x1", "Configuration last modified by admin at 16:25:49.318 UTC Sat Apr 6 2019" ] ], "changed": false, "stdout": [ "Cisco Adaptive Security Appliance Software Version 9.1(7)16 \n\n.... ], "_ansible_parsed": true, "_ansible_no_log": false } } ] } ]}
Note
for now dj_ansible support network devices inventory only, contibution is so welcome to add support for other inventories types like servers.
Release files for django-ansible 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-ansible-1.0.3.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_ansible-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / django-ansible-1.0.3.tar.gz
| Download URL | django-ansible-1.0.3.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
944401c50db3ace399f30e7901dc1a09a9b3dd43a474c9450cbfc03c1a521bc4
|
|
BLAKE2b-256 checksum How to use checksums |
2840a9fa752a1c55bec4e4d55b46cec06d95cddb4ed5b953f6b93bb5ea251a72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15+
|
Release files / django_ansible-1.0.3-py3-none-any.whl
| Download URL | django_ansible-1.0.3-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5236b31a0cff9aa5f7a9dccad001685ae4475e1621337000b528477f2698ab96
|
|
BLAKE2b-256 checksum How to use checksums |
6b67f3226362d9f5645157ec48dee27ace3ae1af70edd08d6cc3e331fc254e9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15+
|