Question:
How can I run a playbook in python script? What is the equivalent of the following using ansible module in python:
1 2 3 |
ansible -i hosts dbservers -m setup ansible-playbook -i hosts -vvvv -k site.yml |
I was looking at their documenation in http://docs.ansible.com/developing_api.html but they have very limited examples.
Answer:
This covered in the Ansible documentation under “Python API.”
For example, ansible -i hosts dbservers -m setup
is implemented via:
1 2 3 4 5 6 7 8 9 |
import ansible.runner runner = ansible.runner.Runner( module_name='setup', module_args='', pattern='dbservers', ) dbservers_get_facts = runner.run() |
There are a bunch of non-documented parameters in the __init__
method of Runner (from ansible.runner
). There’s too many to list inline, but I’ve included some of the parameters in this post as a guess to what you’re specifically looking for.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class Runner(object): ''' core API interface to ansible ''' # see bin/ansible for how this is used... def __init__(self, host_list=C.DEFAULT_HOST_LIST, # ex: /etc/ansible/hosts, legacy usage module_path=None, # ex: /usr/share/ansible module_name=C.DEFAULT_MODULE_NAME, # ex: copy module_args=C.DEFAULT_MODULE_ARGS, # ex: "src=/tmp/a dest=/tmp/b" ... pattern=C.DEFAULT_PATTERN, # which hosts? ex: 'all', 'acme.example.org' remote_user=C.DEFAULT_REMOTE_USER, # ex: 'username' remote_pass=C.DEFAULT_REMOTE_PASS, # ex: 'password123' or None if using key remote_port=None, # if SSH on different ports private_key_file=C.DEFAULT_PRIVATE_KEY_FILE, # if not using keys/passwords sudo_pass=C.DEFAULT_SUDO_PASS, # ex: 'password123' or None ... sudo=False, # whether to run sudo or not sudo_user=C.DEFAULT_SUDO_USER, # ex: 'root' module_vars=None, # a playbooks internals thing play_vars=None, # play_file_vars=None, # role_vars=None, # role_params=None, # default_vars=None, # extra_vars=None, # extra vars specified with he playbook(s) is_playbook=False, # running from playbook or not? inventory=None, # reference to Inventory object ... su=False, # Are we running our command via su? su_user=None, # User to su to when running command, ex: 'root' su_pass=C.DEFAULT_SU_PASS, vault_pass=None, ... ): |
For instance, the above command that specifies a sudo user and pass would be:
1 2 3 4 5 6 7 8 |
runner = ansible.runner.Runner( module_name='setup', module_args='', pattern='dbservers', remote_user='some_user' remote_pass='some_pass_or_python_expression_that_returns_a_string' ) |
For playbooks, look into playbook.PlayBook, which takes a similar set of initializers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class PlayBook(object): ''' runs an ansible playbook, given as a datastructure or YAML filename. ... ''' # ***************************************************** def __init__(self, playbook = None, host_list = C.DEFAULT_HOST_LIST, module_path = None, .... |
and can be executed with the .run()
method. e.g.:
1 2 3 4 |
from ansible.playbook import PlayBook pb = PlayBook(playbook='/path/to/book.yml, --other initializers--) pb.run() |
more robust usage can be found in the ansible-playbook
file.
As far as I know, translating playbooks to Python modules is a bit more involved, but the documentation listed above should get you covered and you can reuse the YAML parser built into Ansible to convert playbooks to variables.