kriansa / ansible-bundler

Turn Ansible playbooks into executable files
BSD 3-Clause "New" or "Revised" License
162 stars 21 forks source link

Bundle ansible-playbooks with all folders included ( like filter_plugins, callback_plugins, etc) #5

Closed rsk closed 5 years ago

rsk commented 5 years ago

ansible playbooks often include multiple filter plugins for example: there can be filter_plugins, callback_plugins etc.,

here is an example playbook that includes a filter_plugin the following is the folder structure.

.
├── filter_plugins
│   ├── test_filter.py
└── test_ansible.yml

1 directory, 3 files

code for test_filter.py The functionality of this filter is to append "something" sting to any value that is passed to filter

#!/usr/bin/env python

def test_filter(testvar):
    return "something"+str(testvar)

class FilterModule(object):
    ''' A filter to fetch ann attr from dict '''
    def filters(self):
        return {
            'test_filter': test_filter
        }

the playbook is as follows:

---
- hosts: localhost
  tasks:
    - name: test
      debug:
        msg: "{{ somevar | test_filter }}"

The following is the usage of the playbook:

[srallaba@samvaranpc ansible_dir]$ ansible-playbook -vvvv test_ansible.yml -e"somevar=testvalue"
ansible-playbook 2.4.3.0
  config file = None
  configured module search path = [u'/home/srallaba/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.16 (default, Apr 30 2019, 15:54:43) [GCC 9.0.1 20190312 (Red Hat 9.0.1-0.10)]
No config file found; using defaults
setting up inventory plugins
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc

PLAYBOOK: test_ansible.yml *******************************************************************************************************************************************************************************************************************
1 plays in test_ansible.yml

PLAY [localhost] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: srallaba
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344 `" && echo ansible-tmp-1566571609.56-75746724107344="` echo /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpcu3aCc TO /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344/setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344/ /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344/setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344/setup.py; rm -rf "/home/srallaba/.ansible/tmp/ansible-tmp-1566571609.56-75746724107344/" > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [test] **********************************************************************************************************************************************************************************************************************************
task path: /home/srallaba/ansible_dir/test_ansible.yml:4
ok: [localhost] => {
    "msg": "somethingtestvalue"
}
META: ran handlers
META: ran handlers

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0 

Currently when i bundle this playbook through bundle-playbook. There is option to include folders into the bundle. for example if we could add multiple folders(filter_plugins, callback_plugins via) to the bundle-playbook it would unlock potential of bundling customized playbooks.

example usage would be :

$ bundle-playbook --playbook-file=playbook.yml \
  --requirements-file=requirements.yml \
  --vars-file=vars.yml \
  --extra-deps=files
  --extra-folder=folder_name1 folder_name2 

This will help us including many kinds of folders/files that can be helpful in the playbook run.

kriansa commented 5 years ago

Currently you can use --extra-deps to include both files and folders and it copies it recursively.

In your case, you can run:

$ bundle-playbook --playbook-file=test_ansible.yml \
  --extra-deps=filter_plugins

Let me know if it works.

rsk commented 5 years ago

@kriansa it works like a charm. Thank you.

rsk commented 5 years ago

@kriansa one more question before you close this issue. What if i want multiple folders inside extra-deps ? how do i do that ?

kriansa commented 5 years ago

I'm glad it worked.

You can just pass --extra-deps multiple times. This is described on the docs.