Skip to main content

Chaos toolkit Extension for using ansible as event execution tool

Project description

Chaos toolkit Extension for using ansible as driver

Python versions

This project is a collection of actions and probes, gathered as an extension to the Chaos Toolkit.

Please NOTE This extension is in the early stages of development. Please feel free to create an issue in case of needed ehancement or misfunctioning.


Ansible Icon


Install

This package requires Python 3.6+

To be used from your experiment, this package must be installed in the Python environment where chaostoolkit already lives.

$ pip install -U chaostoolkit-ansible

Principles

This chaos toolkit driver provides you an easy way to execute probe and/or actions using ansible modules. By using it, you can execute tasks, gather facts, ... on remote systems

Usage

Basic

Probes

To use the probes from this package, add the following to your experiment file:

In JSON:

"steady-state-hypothesis": {
    "title": "Tests",
    "probes": [
        {
            "type": "probe",
            "name": "test-current-directory",
            "tolerance": {
              "type": "jsonpath",
              "path": "$.*.task",
              "expect": "/home/me"
            },
            "provider": {
                "type": "python",
                "module": "chaosansible.probes",
                "func": "chaosansible_probe",
                "arguments": {
                    "host_list": ["myserver1", "myserver2"],
                    "facts": "yes",
                    "ansible": {
                        "module": "shell",
                        "args": "pwd"
                    }
                }
            }
        }
    ]
}

In YAML:

---
steady-state-hypothesis:
  title: The current working directory must be /home/me
  probes:
  - type: probe
    name: test-current-directory
    tolerance:
      type: jsonpath
      target: "$.*.task"
      pattern: /home/me
    provider:
      type: python
      module: chaosansible.probes
      func: chaosansible_probe
      arguments:
        host_list: ["myserver1", "myserver2"]
        facts: True
        ansible:
          module: shell
          args: pwd

That's it!

Probes can be gathered by using the stdout of an ansible task or through the ansible gather_facts module. Each time chaostoolkit-ansible runs, it returns a json that can be used in tolerance (using jsonpath, regex, ...)

This json is always formatted the same way (Exemple for a two targets host_list):

{
  "target1": {
    "fact": " -> JSON result of the ansible gather_facts",
    "task": " -> String result containing the stdout value of the task result - Empty when ansible task do not return stdout"
  },
  "target2": {
    "fact": "...",
    "task": "..."
  }
}

Actions

To use the actions from this package, add the following to your experiment file:

In JSON:

"method": [
    {
        "type": "action",
        "name": "delete-etc-hosts-file",
        "provider": {
            "type": "python",
            "module": "chaosansible.actions",
            "func": "chaosansible_run",
            "arguments": {
                "host_list": ["server1", "server2"],
                "become": true,
                "ansible": {
                    "module": "file",
                    "args": {
                        "path": "/etc/hosts",
                        "state": "absent"
                    }
                }
            }
        }
    }
]

In YAML:

---
method:
- type: action
  name: delete-etc-hosts-file
  provider:
    type: python
    module: chaosansible.actions
    func: chaosansible_run
    arguments:
        host_list: ["server1", "server2"]
        become: True
        ansible:
          module: file
          args: 
            path: /etc/hosts
            state: absent

Detailled usage

Configuration block

The configuration block can be used to specify specific parameters to use. This block can be omit unless you really need to change default ansible parameters to run your exeperiment

Configuration variables that can be used by this driver are:

  • ansible_module_path: Path of your ansible library
  • ansible_become_user: Privileged user used when you call privilege escalation (root by default)
  • ansible_ssh_private_key: Your ssh private key used to connect to targets (~/.ssh/id_rsa by default)
  • ansible_user: User on target host used by ansible (current username by default)
  • become_ask_pass: Password to escalate privileged when sudo needs one
  • ansible_extra_ssh_args: SSH extra args

In case you need to change one/or many default configuration(s), you can specify your value using the configuration block

Please feel free to ask, if you need access to other ansible configuration parameters

In JSON:

"configuration": {
    "ansible_ssh_private_key": "/home/me/.ssh/mykey"
}

In YAML:

configuration:
  ansible_ssh_private_key: "/home/me/.ssh/mykey"

Arguments

chaosansible_run and chaosansible_probes use arguements (Most argument are classical ansible parameters):

Argument Type Required Default value Description
host_list Array localhost List of host to use
facts bool false Gather_facts
become bool false Escalate privilege to run task
run_once bool false Run the task only once on one target
num_target str all "all" or "x" where x is an integer. Run the task to only x target among the host_list. Ideal to create random event
ansible dict {} Execute ansible task. Cf ansible dict format. If ansible is not set, no task except ansible gather_facts (if facts set to True)

Ansible dict format:

Classic ansible task are in the form:

name: task name
ansible-module-name:
  module-parameter1: value1
  module-parameter2: value2

This is translate into chaos experiment file like this: In JSON

  "ansible": {
      "module": "ansible-module-name",
      "args": {
        "module-parameter1": "value1",
        "module-parameter2": "value2"
      }
  }

In YAML

  ansible:
    module: ansible-module-name
    args:
      module-parameter1: value1
      module-parameter2: value2

Example with the ansible mount module (Umount a filesystem):

In JSON

  "ansible": {
      "module": "mount",
      "args": {
        "path": "/data",
        "state": "unmounted"
      }
  }

In YAML

  ansible:
    module: mount
    args:
      path: /data
      state: unmounted

Example of usage

Delete /etc/hosts file on 2 random servers out of 5

In JSON

"method": [
    {
        "type": "action",
        "name": "delete-etc-hosts-file",
        "provider": {
            "type": "python",
            "module": "chaosansible.actions",
            "func": "chaosansible_run",
            "arguments": {
                "host_list": ["server1","server2","server3","server4","server5"],
                "num_target": "2",
                "become": true,
                "ansible": {
                    "module": "file",
                    "args": {
                        "path": "/etc/hosts",
                        "state": "absent"
                    }
                }
            }
        }
    }
]

In YAML

method:
- type: action
  name: delete-etc-hosts-file
  provider:
    type: python
    module: chaosansible.actions
    func: chaosansible_run
    arguments:
        host_list: ["server1","server2","server3","server4","server5"]
        become: True
        num_target: "2"
        ansible:
          module: file
          args: 
            path: /etc/hosts
            state: absent

Run 100% cpu load on 3 server out of 5

In JSON

"method": [
    {
        "type": "action",
        "name": "delete-etc-hosts-file",
        "provider": {
            "type": "python",
            "module": "chaosansible.actions",
            "func": "chaosansible_run",
            "arguments": {
                "host_list": ["server1","server2","server3","server4","server5"],
                "num_target": "3",
                "ansible": {
                    "module": "shell",
                    "args": {
                        "cmd": "stress-ng --cpu 0 --cpu-method matrixprod -t 60s",
                    }
                }
            }
        }
    }
]

In YAML

method:
- type: action
  name: delete-etc-hosts-file
  provider:
    type: python
    module: chaosansible.actions
    func: chaosansible_run
    arguments:
        host_list: ["server1","server2","server3","server4","server5"]
        become: True
        num_target: "3"
        ansible:
          module: shell
          args: 
            cmd: stress-ng --cpu 0 --cpu-method matrixprod -t 60s

Contribute

If you wish to contribute more functions to this package, you are more than welcome to do so. Please, fork this project, make your changes following the usual PEP 8 code style, sprinkling with tests and submit a PR for review.

The Chaos Toolkit projects require all contributors must sign a Developer Certificate of Origin on each commit they would like to merge into the master branch of the repository. Please, make sure you can abide by the rules of the DCO before submitting a PR.

Develop

If you wish to develop on this project, make sure to install the development dependencies. But first, create a virtual environment and then install those dependencies.

$ pip install -r requirements-dev.txt -r requirements.txt

Then, point your environment to this directory:

$ pip install -e .

Now, you can edit the files and they will be automatically be seen by your environment, even when running from the chaos command locally.

Test

To run the tests for the project execute the following:

$ pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chaostoolkit-ansible-0.2.3.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

chaostoolkit_ansible-0.2.3-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file chaostoolkit-ansible-0.2.3.tar.gz.

File metadata

  • Download URL: chaostoolkit-ansible-0.2.3.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

File hashes

Hashes for chaostoolkit-ansible-0.2.3.tar.gz
Algorithm Hash digest
SHA256 9007a0883d96497e28bc15d3029429ebacb9305343ff709275100aba16549f22
MD5 16490d5e5e31758a2493d842bd2b1051
BLAKE2b-256 ead727535db2befce14d98b06ce5b962666ff83ce77a78943094178f93318f42

See more details on using hashes here.

File details

Details for the file chaostoolkit_ansible-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: chaostoolkit_ansible-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

File hashes

Hashes for chaostoolkit_ansible-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d9cce3d24b58513135a21cb42251bd436ce3da7e13164fe71e663320c8af4bd6
MD5 7dfd5387b05c1fb303bce16c83fef518
BLAKE2b-256 5f36fcdb1c693efea8fa2167de17c40e6a7628a3892680056e11593743048785

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page