Giter Site home page Giter Site logo

charms.ansible's Introduction

charms.ansible

This was initially based on charms.ansible, but have later on adopted implementation that requires fewer dependencies and prerequisite steps so I think it is a cleaner solution than its original.

Design objectives:

  1. Design as reusable layer(s)
  2. Be compatible with Ubuntu and CentOS
  3. Simple pattern to execute a playbook

Assumption:

  1. Playbooks will be local (in charm) so to maintain the atomic nature of a HW charm — the model contains both the declaration of attributes and actions to handle runtime state transitions.

This method is inspired by this article. This is to take advantage of the Ansible Python API.

Design details

  1. Install prerequisites. Installing Ansible will fail on a vanilla Ubuntu because it misses a few dependencies. Using layer-basic by listing them out in layer.yaml:

    includes:
      - 'layer:basic'
    options:
      basic:
        packages:
          - libffi-dev
          - libssl-dev
          - python
          - python3-dev
  1. Install Ansible. Use Python wheel supported by layer-basic. In wheelhouse.txt:
  ```
  ansible==2.2.0
  ```
  1. ansible.cfg. Instead of using a global config, this is local so each charm can have its own variation if desired.

    [defaults]
    inventory = ./hosts
    log_path = /var/log/ansible/ansible.log
    remote_tmp = $HOME/.ansible/tmp
    local_tmp = $HOME/.ansible/tmp
    
    [ssh_connection]
    ssh_args = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s
    control_path = ~/.ansible/cp/ansible-ssh-%%h-%%p-%%r
    
  2. Options. Constructed a class to be the abstraction of Ansible options:

    class Options(object):
        """
        Options class to replace Ansible OptParser
        """
        ....
        verbosity=None,
        inventory=None,
        listhosts=None,
        subset=None,
        module_paths=None,
        extra_vars=None,
        forks=None,
        ask_vault_pass=None,
        vault_password_files=None,
        new_vault_password_file=None,
        output_file=None,
        tags=None,
        skip_tags=[],
        one_line=None,
        tree=None,
        ask_sudo_pass=None,
        ask_su_pass=None,
        sudo=None,
        sudo_user=None,
        become=None,
        become_method=None,
        become_user=None,
        become_ask_pass=None,
        ask_pass=None,
        private_key_file=None,
        remote_user=None,
        connection=None,
        timeout=None,
        ssh_common_args=None,
        sftp_extra_args=None,
        scp_extra_args=None,
        ssh_extra_args=None,
        poll_interval=None,
        seconds=None,
        check=None,
        syntax=None,
        diff=None,
        force_handlers=None,
        flush_cache=None,
        listtasks=None,
        listtags=[],
        module_path=None
  3. Playbook execution. Running it is to use Ansible's API call PlaybookExecutor.

    self.pbex = playbook_executor.PlaybookExecutor(
        playbooks=pbs,
        inventory=self.inventory,
        variable_manager=self.variable_manager,
        loader=self.loader,
        options=self.options,
        passwords=passwords)
    ....
    self.pbex.run()

Charm integration

Integrating with charm takes the followings:

  1. Include layer. In layer.yaml:

    includes:
      - 'layer:basic'
      - 'layer:ansible'
  2. Create a playbooks folder and place playbooks here:

    .
    ├── config.yaml
    ├── icon.svg
    ├── layer.yaml
    ├── metadata.yaml
    ├── playbooks
    │   └── test.yaml
    └── reactive
        └── solution.py
    
  3. Using config.yaml to pass in playbook for each action that is defined in the charm states. For example, define test.yaml for an action in state-0:

    options:
      state-0-playbook:
        type: string
        default: "test.yaml"
        description: "Playbook for..."
  4. Define the playbook. For example, a hello world that will create a file `/tmp/testfile.txt'.

    - name: This is a hello-world example
      hosts: 127.0.0.1
      tasks:
      - name: Create a file called '/tmp/testfile.txt' with the content 'hello world'.
        copy: content="hello world\n" dest=/tmp/testfile.txt
        tags:
          - sth

    Note that tags value sth must match playbook run call (see below).

  5. In charm .py file, from charms.layer.task import Runner, then in state-0 to call given playbook:

    playbook = config['state-0-playbook']
    runner = Runner(
        tags = 'sth', # <-- must match the tag in the playbook
        connection = 'local', # <-- must be "local"
        hostnames = '127.0.0.1', # <-- assuming execution in localhost
        playbooks = [playbook],
        private_key_file = '',
        run_data = {},
        become_pass = '',
        verbosity = 0
    )
    stats = runner.run()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.