{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ansible\n", "\n", "Ansible is a configuration management software.\n", "\n", "Connects via ssh or docker to a list of inventory machines and executes a series of tasks eventually grouped in playbooks.\n", "\n", "At first, create an inventory file with all your hosts." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cd ansible" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cat inventory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now check if you can ping the local host." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Check connections versus the local host in the \"course\" group\n", "\n", "!ansible -i inventory -m ping course" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Pinging all hosts gives some errors too, due to missing hosts or no ssh-key exchange\n", "!ansible -i inventory -m ping all" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further on inventories" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " You can split your servers in many inventory files, like\n", "\n", " - staging\n", " \n", "```\n", "# staging inventory file\n", "# run with\n", "# ansible -i staging ...\n", " [ws]\n", " staging-ws-[0:3]\n", " \n", " [jboss]\n", " staging-boss-[0:6]\n", "```\n", " \n", " - production\n", " \n", "```\n", "# production inventory file\n", "# run with\n", "# ansible -i production ...\n", " [ws]\n", " ws-[0:3]\n", " \n", " [jboss]\n", " boss-[0:6]\n", "```\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playbooks\n", "\n", "To run a group of tasks with ansible, just:\n", "\n", " - create a playbook.yml\n", " - run ansible-playbook -i inventory playbook.yml\n", " \n", "\n", "A playbook is a list of tasks in yml format, something like\n", "\n", "```\n", "#\n", "# playbook.yml\n", "#\n", "- name: All public traffic is redirected via https\n", " uri:\n", " url: http://{{server_host}}/\n", " validate_certs: false\n", " follow_redirects: none\n", " status_code: 301\n", " \n", "- name: This webapp is served\n", " uri:\n", " url: https://{{server_host}}/webapp-1\n", " validate_certs: false\n", " status_code: 200\n", " HEADER_testflag: test\n", "\n", "- name: The WS is serverd and requires authentication\n", " uri:\n", " url: https://{{server_host}}/rest/v1/method\n", " validate_certs: false\n", " status_code: 401\n", "\n", "```\n", "\n", "In this case, instead of making actual installation|setup tasks, we just created a testsuite validating our deployment. Now we must write another playbook which takes care of deployng the actual machine.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing our course environment\n", "\n", "We can write a playbook to test our course environment." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!cat python-course-test.yml" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!ansible-playbook -i inventory python-course-test.yml " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see something is missing: this playbook is not going to modify our machine but only test that everything is in place. \n", "\n", "See ```ansible-playbook --check ``` and ``` --diff ``` for further infos.\n", "\n", "We can run a setup playbook, conventionally named site.yml.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!cat site.yml" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!ansible-playbook -i inventory site.yml --limit=course # in this case the --limit does not change anything ;)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }