django应用程序,它提供了一个api来集成ansible和django项目

django-ansible的Python项目详细描述


django ansible

PyPI - StatusPyPIPyPI - Python VersionPyPI - Django VersionCodacy BadgeGitHub

django ansibe是一款django应用程序,允许django应用程序与ansibe集成。 基本上,它允许您使用djangomodels在数据库中存储库存数据,并提供 一个执行类似json的playbooks的api。

快速启动

  1. 安装django ansible

    pip install django-ansible

  2. 将“DJ Ansible”添加到这样的已安装应用程序设置中

    INSTALLED_APPS = [
     ...
     'dj_ansible',
    ]
    
  3. 运行python manage.py migrate创建所需的表。

  4. 将库存数据添加到两个表中

    • ansible_网络组(模型:AnsibleNetworkGroup()
    • Ansible_网络主机(型号:AnsibleNetworkHost()

    如下:

     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()
    

运行剧本

要运行剧本,请使用executeapi来执行剧本

示例
  1. 创建练习本词典

    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'])
              ]
          )
    
  2. 使用executeapi运行my_play

    from 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))
    
  3. result对象由两个json组成:

  • 第一个state,看起来像这样:

      {
      "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
          }
      ]}
    
  • 以及result,看起来是这样:

    {
    "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
                    }
                }
            ]
        }
    ]}
    

注意

目前DJ只支持网络设备目录,欢迎继续支持其他 清点服务器等类型。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
web Java/Guice配置MyExample类的应用程序侦听器时出错。GuiceServletConfig   java环境变量。exe始终指向JRE的路径,但不指向JDK   java如何解决这个索引器?   java如何解决Android MultiDex应用程序实例空指针异常?   delphi如何处理java通过tcp发送的序列化对象   java常量池条目   base64 java将jpg文件转换为字节字符串,然后再次转换回jpg   rest是否可以使用运行时解析的GenericType参数调用Java JAXRS请求方法?   Java:如何在Java中轻松更改配置文件值?   java无法通过imagebutton 安卓 studio打开网页   java Tomcat 6 java_主页   java使用@BindingResult和@Validated   性能neo4j Java API getRelationships方法的时间复杂性   java Vert中的sharedData异步映射的寿命有多长。十、   java Mockito模拟调用两次的方法调用   java如何避免这种ClassCastException?   java从POM文件获取artifactId出错   java如何使用for循环在一个范围内添加偶数