• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

super-duper-simple rest api for ansible tasks

#Flansible

A super-simple rest api for Ansible


Flansible is a very simple rest api for executing Ansible ad-hoc-commands and playbooks. It it not a replacement for Ansible Tower.

Flansible is written in Flask, and uses celery for async task execution and optionally flower for real-time monitoring of celery.

Credits

Joerg Lehmann: Lots of feedback and help

Required python packages with tested versions (newer version should be fine):

ansible==2.1.0.0
celery==4.0.0
Flask==0.10.1
Flask-HTTPAuth==3.1.2
Flask-RESTful==0.3.5
flask-restful-swagger==0.19
flower==0.9.1
redis==2.10.5

Other things:

Celery requires a running datastore, either Redis or RabbitMQ - flansible does not configure this, and expects Redis or RabbitMQ to already be running and accessible.

Configuration

The config.ini file should be pretty self-explanatory. It's probably a good idea to remove lines 5-10 in app.py, as that's only a debug definition which probably won't make sense to you.

If you want to run the flask webserver on port 80 like I'm doing without running as root, you need to allow python to use priveliged ports:

sudo setcap 'cap_net_bind_service=+ep' /usr/bin/python2.7

Setup

Setup tested on Ubuntu 14.04

from the Flansible/Flansible directory (where the .py files live), run the following (either using screen or in separate terminals):

celery worker -A flansible.celery --loglevel=info (this starts the celery worker which will actually execute the things)

python runserver.py (this starts the actual webserver. You're free to replace the built-in flask server with something else)

OPTIONAL: flower --broker=redis://localhost:6379/0 (this starts the flower web gui, which gives provides information for running tasks. Replace the value of --broker with your own connection string for the redis/rabbitmq data store). Flower will be available on http://<hostname>:5555.

Usage: Vagrant

To test the solution with vagrant, you can run vagrant up from the root of the repo directory. I haven't set up the apache thingy yet, so you'll have to run the following inside vagrant (by using vagrant ssh) after the box comes online:

cd /vagrant/Flansible

celery worker -A flansible.celery --loglevel=info (use screen to run celery in a non-blocking way)

python runserver.py (use screen to run the app in a non-blocking way)

you can now interact with the flansible rest api using http://localhost:3000/api.

Usage: General

Flansible comes with swagger documentation, which can be reached at http://<hostname>/api/spec.html, with the json spec located at http://<hostname>/api/spec. This should be your first stop for getting to know the expected json payload when interacting with the api.

Usage: Role-based access

Flansible uses basic auth, with username/password configurable in the rbac.json file (default: admin/admin)

rbac.json allows linking usernames with a list of allowed inventories. The example provided shows how to configure a "devadmin" user which doesn't have access to Ansible's default inventory:

{
  "rbac": [
    {
      "user": "admin",
      "password": "admin",
      "inventories": [
        "username admin has implicit access to all inventories",
        "no need to specify anything here"
      ]

    },
    {
      "user": "devadmin",
      "password": "devpassword",
      "inventories": [
        "/some/folder/dev",
        "/home/thadministrator/hosts"
      ]

    }

  ]
}

Not that the username "admin" is special, and is not subject to inventory access checking.

The idea behind this rbac implementation is to allow separate credentials for executing tasks and playbooks against dev/test environments without having access to make changes to production systems.

Usage: Ad-hoc commands

Issue a POST to http://<hostname>/api/ansiblecommand with contenttype Application/Json.

The body should contain the following (for updated info, see the swagger spec mentioned above):

{
   "host_pattern": "localhost", 
    "module":  "find",
    "module_args":  {                            
        "paths":  "/tmp",       
        "age":  "2d",
        "recurse" : true
    }                            
}    

where

  • module: The ansible module to execute
  • host_filter: The host or host filter to execute on (defaults to "localhost" if omitted)
  • extra_args: array of objects containing any extra vars

Usage: Playbooks

Issue a POST to http://<hostname>/api/ansibleplaybook with contenttype application/Json.

This is an example of playbook execution:

{
  "playbook_dir": "/home/thadministrator",
  "playbook": "test.yml",
  "become": true
}

Flansible will verify that the playbook dir/file exists before submitting the job for execution.

Usage: Getting status

both ansibleplaybook and ansiblecommand will return a task_id value. That value can be used to check the status and output of the job. This is done by issuing a GET to http://<hostname>/api/ansibletaskstatus/<task_id> with contenttype Application/Json.

Usage: Getting output

The output of the ansible command/playbook can be viewed live while the task is running, and afterwards. Issue a GET cal to: http://<hostname>/api/ansibletaskoutput/<task_id> with contenttype Application/Json. The output from this call should resemble what you see in bash when executing Ansible interactively.

how it looks

  • Execute an Ansible command (/api/ansiblecommand). The returning task_id is used to check status:

alt text

  • Use the returned task_id to get the Ansible job output (/api/ansibletaskoutput/<task-id>):

alt text

  • Use Flower to check job statuses:

alt text

  • Use Swagger to get a feel for the api (this is very much work in progress):

alt text

how it works in docker

vagrant ssh docker-compose -f docker-compose.yml build docker-compose -f docker-compose.yml up -d sudo docker exec -it vagrant_flansible_1 bash ansible-playbook /home/flansible/playbook/site-docker.yml

cd /home/flansible/Flansible; python runserver.py cd /home/flansible/Flansible; C_FORCE_ROOT=1 celery worker -A flansible.celery --loglevel=info flower --broker=redis://localhost:6379/0

More Repositories

1

Ansible-win_dsc

Native DSC module for Ansible
PowerShell
52
star
2

ecr-updater

Allows use of AWS ECR registries with Kubernetes no matter where your cluster is running.
Python
36
star
3

AnsibleDscModuleGenerator

Scripts for generating Ansible modules from PowerShell DSC resources
PowerShell
32
star
4

Traefik-Flask-Opentracing-Blogpost

Code for blog post
Python
20
star
5

Kubernetes-Auth0

Step-by-step walkthru of setting up Kubernetes with Auth0-based authentication/authorization
Python
20
star
6

PowershellModules

PowerShell
15
star
7

Ansible-Auto-Generated-Modules

You shouldn't be using this repo, please use Ansible's builtin win_dsc module instead.
PowerShell
11
star
8

ansible-arm-deployment

Ansible modules for interacting with Azure Resource Manager
Python
10
star
9

ansibleroles-winreboot

Ansible role for controlling reboot on Windows nodes.
PowerShell
9
star
10

AzureDeploymentEngine

A PowerShell module for automated deployment of stuff in Azure.
PowerShell
9
star
11

Flauthority

A lightweight CA and rest api for issuing certificates
Python
8
star
12

awshelper

wrapper which lets you use aws sso-based credentials with your existing utilities
Python
8
star
13

Kubelini

Ansible playbook based on "Kubernetes the hard way"
Shell
7
star
14

vault-mssql-lab

A small lab for playing with Hashicorp Vault and SQL Server
Shell
7
star
15

Auth0Proxy

A reverse proxy for auth-enabling (almost) any website
Python
6
star
16

K8sProbeTester

Python
5
star
17

fastapi-pydantic-testing

some do's and donts for unit testing fastapi apps with pydantic settings
Python
5
star
18

psday2017

Code n things for my PSday 2017 presentation
PowerShell
5
star
19

remmina-resizer

Python
4
star
20

armrest

Web api supporting Ansible Dynamic Inventory
C#
3
star
21

vmm-extensions

Small utils for VMM 2012R2 in the form of a VMM console Add-In.
C#
3
star
22

Consul-Talk-March-2018

Code and examples from the Hashicorp Norway User Group meetup 12.03.2018
Python
3
star
23

dapr-speed-testing

Testing various ways of invoking dapr in python apps
Python
3
star
24

kubernetes-auth0-blogpost

Python
1
star
25

Rails-AzureAD

Ruby on Rails app with Azure AD authentication using SAML 2.0
Ruby
1
star
26

drone-makisu-plugin

Python
1
star
27

Rough-Massive-Hook

Sample project for .Net-based apis with versioning, consul things and Ansible things.
C#
1
star
28

AzureAutomationDebug

Emulated cmdlets for local debugging of Azure Automation Powershell runbooks
PowerShell
1
star
29

VMmDynamicInventory

Web API for using VMM as an Ansible dynamic Inventory source
C#
1
star
30

daps

helper for making dapr (dapr.io) more robust
Python
1
star
31

dispatch

Python
1
star
32

traefikreplay

Go
1
star
33

docker-ansible_win

Dockerfile for ansible node with stuff for managing Windows on it
1
star
34

PsDocker

PowerShell functions for interacting with the Docker Remote API
PowerShell
1
star