commit 7f35982f761e3ce0fab08b91ca2c6f188101a096 Author: Cedric Girard Date: Fri Jul 27 10:01:50 2018 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8b42eb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.retry diff --git a/playbooks/copy_and_delete.yml b/playbooks/copy_and_delete.yml new file mode 100644 index 0000000..588c66f --- /dev/null +++ b/playbooks/copy_and_delete.yml @@ -0,0 +1,9 @@ +- hosts: coreos + gather_facts: no + #default is linear + #strategy: free + vars_files: + - "{{inventory_dir}}/vars/vars.yml" + tasks: + - include_tasks: tasks/copy_files.yml + - include_tasks: tasks/delete_files.yml diff --git a/playbooks/copy_from_dirs.yml b/playbooks/copy_from_dirs.yml new file mode 100755 index 0000000..4ce0a92 --- /dev/null +++ b/playbooks/copy_from_dirs.yml @@ -0,0 +1,9 @@ +- hosts: devoteam + gather_facts: no + vars_files: + - "{{inventory_dir}}/vars/vars.yml" + tasks: + - name: "Copy files from {{ dir_src }} to {{ dir_dst }}" + copy: + src: "{{dir_src}}/" + dest: "{{dir_dst}}/" diff --git a/playbooks/copy_from_dirs_loops.yml b/playbooks/copy_from_dirs_loops.yml new file mode 100755 index 0000000..afa7f09 --- /dev/null +++ b/playbooks/copy_from_dirs_loops.yml @@ -0,0 +1,6 @@ +- hosts: coreos + gather_facts: no + vars_files: + - "{{inventory_dir}}/vars/vars.yml" + tasks: + - include_tasks: tasks/copy_files.yml diff --git a/playbooks/coreos-bootstrap.yml b/playbooks/coreos-bootstrap.yml new file mode 100755 index 0000000..ae9ee54 --- /dev/null +++ b/playbooks/coreos-bootstrap.yml @@ -0,0 +1,5 @@ +- hosts: coreos + gather_facts: False + roles: + - defunctzombie.coreos-bootstrap + diff --git a/playbooks/delete_files.yml b/playbooks/delete_files.yml new file mode 100644 index 0000000..0194aad --- /dev/null +++ b/playbooks/delete_files.yml @@ -0,0 +1,6 @@ +- hosts: coreos + gather_facts: no + vars_files: + - "{{inventory_dir}}/vars/vars.yml" + tasks: + - include_tasks: tasks/delete_files.yml diff --git a/playbooks/poweroff.yml b/playbooks/poweroff.yml new file mode 100755 index 0000000..6bc84eb --- /dev/null +++ b/playbooks/poweroff.yml @@ -0,0 +1,7 @@ +- hosts: alpine + gather_facts: false + tasks: + - name: poweroff + command: poweroff + become: true + ignore_errors: true diff --git a/playbooks/report.yml b/playbooks/report.yml new file mode 100755 index 0000000..c3bfb56 --- /dev/null +++ b/playbooks/report.yml @@ -0,0 +1,13 @@ +- hosts: coreos + tasks: + - name: get uptime + shell: uptime + register: uptime + - set_fact: + uptime: "{{uptime['stdout']}}" + - name: Build HTML report + template: + src: "{{playbook_dir}}/../templates/report.html" + dest: /tmp/report.html + delegate_to: localhost + run_once: true diff --git a/playbooks/roles/defunctzombie.coreos-bootstrap/.editorconfig b/playbooks/roles/defunctzombie.coreos-bootstrap/.editorconfig new file mode 100755 index 0000000..3b281f2 --- /dev/null +++ b/playbooks/roles/defunctzombie.coreos-bootstrap/.editorconfig @@ -0,0 +1,11 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 diff --git a/playbooks/roles/defunctzombie.coreos-bootstrap/.travis.yml b/playbooks/roles/defunctzombie.coreos-bootstrap/.travis.yml new file mode 100755 index 0000000..c94c67a --- /dev/null +++ b/playbooks/roles/defunctzombie.coreos-bootstrap/.travis.yml @@ -0,0 +1,19 @@ +--- +language: python +python: "2.7" + +env: + - SITE=test.yml + +before_install: + - sudo apt-get update -qq + - sudo apt-get install -y curl + +install: + - pip install ansible + + # Add ansible.cfg to pick up roles path. + - "printf '[defaults]\nroles_path = ../' > ansible.cfg" + +script: + - "ansible-playbook -i tests/inventory tests/$SITE --syntax-check" diff --git a/playbooks/roles/defunctzombie.coreos-bootstrap/LICENSE b/playbooks/roles/defunctzombie.coreos-bootstrap/LICENSE new file mode 100755 index 0000000..3f39b0a --- /dev/null +++ b/playbooks/roles/defunctzombie.coreos-bootstrap/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Roman Shtylman + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/playbooks/roles/defunctzombie.coreos-bootstrap/README.md b/playbooks/roles/defunctzombie.coreos-bootstrap/README.md new file mode 100755 index 0000000..c246147 --- /dev/null +++ b/playbooks/roles/defunctzombie.coreos-bootstrap/README.md @@ -0,0 +1,66 @@ +# coreos-bootstrap + +In order to effectively run ansible, the target machine needs to have a python interpreter. Coreos machines are minimal and do not ship with any version of python. To get around this limitation we can install [pypy](http://pypy.org/), a lightweight python interpreter. The coreos-bootstrap role will install pypy for us and we will update our inventory file to use the installed python interpreter. + +# install + +``` +ansible-galaxy install defunctzombie.coreos-bootstrap +``` + +# Configure your project + +Unlike a typical role, you need to configure Ansible to use an alternative python interpreter for coreos hosts. This can be done by adding a `coreos` group to your inventory file and setting the group's vars to use the new python interpreter. This way, you can use ansible to manage CoreOS and non-CoreOS hosts. Simply put every host that has CoreOS into the `coreos` inventory group and it will automatically use the specified python interpreter. +``` +[coreos] +host-01 +host-02 + +[coreos:vars] +ansible_ssh_user=core +ansible_python_interpreter=/home/core/bin/python +``` + +This will configure ansible to use the python interpreter at `/home/core/bin/python` which will be created by the coreos-bootstrap role. + +## Bootstrap Playbook + +Now you can simply add the following to your playbook file and include it in your `site.yml` so that it runs on all hosts in the coreos group. + +```yaml +- hosts: coreos + gather_facts: False + roles: + - defunctzombie.coreos-bootstrap +``` + +Make sure that `gather_facts` is set to false, otherwise ansible will try to first gather system facts using python which is not yet installed! + +## Example Playbook + +After bootstrap, you can use ansible as usual to manage system services, install python modules (via pip), and run containers. Below is a basic example that starts the `etcd` service, installs the `docker-py` module and then uses the ansible `docker` module to pull and start a basic nginx container. + +```yaml +- name: Nginx Example + hosts: web + sudo: true + tasks: + - name: Start etcd + service: name=etcd.service state=started + + - name: Install docker-py + pip: name=docker-py + + - name: pull container + raw: docker pull nginx:1.7.1 + + - name: launch nginx container + docker: + image="nginx:1.7.1" + name="example-nginx" + ports="8080:80" + state=running +``` + +# License +MIT diff --git a/playbooks/roles/defunctzombie.coreos-bootstrap/files/bootstrap.sh b/playbooks/roles/defunctzombie.coreos-bootstrap/files/bootstrap.sh new file mode 100755 index 0000000..82646de --- /dev/null +++ b/playbooks/roles/defunctzombie.coreos-bootstrap/files/bootstrap.sh @@ -0,0 +1,36 @@ +#/bin/bash + +set -e + +cd + +if [[ -e $HOME/.bootstrapped ]]; then + exit 0 +fi + +PYPY_VERSION=5.1.0 + +if [[ -e $HOME/pypy-$PYPY_VERSION-linux64.tar.bz2 ]]; then + tar -xjf $HOME/pypy-$PYPY_VERSION-linux64.tar.bz2 + rm -rf $HOME/pypy-$PYPY_VERSION-linux64.tar.bz2 +else + wget -O - https://bitbucket.org/pypy/pypy/downloads/pypy-$PYPY_VERSION-linux64.tar.bz2 |tar -xjf - +fi + +mv -n pypy-$PYPY_VERSION-linux64 pypy + +## library fixup +mkdir -p pypy/lib +ln -snf /lib64/libncurses.so.5.9 $HOME/pypy/lib/libtinfo.so.5 + +mkdir -p $HOME/bin + +cat > $HOME/bin/python < + + {% for host in play_hosts | sort %} +

Report: {{hostvars[host]['ansible_hostname']}}

+ + + + + + +
hostname{{hostvars[host]['ansible_hostname']}}
OS{{hostvars[host]['ansible_distribution']}}
architecture{{hostvars[host]['ansible_architecture']}}
role{{hostvars[host]['role']}}
uptime_cmd{{hostvars[host]['uptime']}}
+ {% endfor %} + + diff --git a/test/group_vars/alpine.yml b/test/group_vars/alpine.yml new file mode 100755 index 0000000..7081f58 --- /dev/null +++ b/test/group_vars/alpine.yml @@ -0,0 +1,3 @@ +ansible_user: alpine +role: testvm +ansible_python_interpreter: python3 diff --git a/test/group_vars/coreos.yml b/test/group_vars/coreos.yml new file mode 100755 index 0000000..98cb70f --- /dev/null +++ b/test/group_vars/coreos.yml @@ -0,0 +1,3 @@ +ansible_user: core +ansible_python_interpreter: /home/core/bin/python +role: testvm diff --git a/test/group_vars/devoteam.yml b/test/group_vars/devoteam.yml new file mode 100755 index 0000000..834f302 --- /dev/null +++ b/test/group_vars/devoteam.yml @@ -0,0 +1 @@ +ansible_user: cgirard diff --git a/test/host_vars/cgirard-laptop.yml b/test/host_vars/cgirard-laptop.yml new file mode 100755 index 0000000..2edb4fe --- /dev/null +++ b/test/host_vars/cgirard-laptop.yml @@ -0,0 +1 @@ +role: Devoteam Laptop diff --git a/test/hosts.yml b/test/hosts.yml new file mode 100755 index 0000000..f5444f6 --- /dev/null +++ b/test/hosts.yml @@ -0,0 +1,22 @@ +local: + hosts: + localhost: +devoteam: + hosts: + cgirard-laptop: +coreos: + hosts: + coreos1: + ansible_host: 192.168.122.221 + coreos2: + ansible_host: 192.168.122.220 + coreos3: + ansible_host: 192.168.122.222 +alpine: + hosts: + alpine1: + ansible_host: 192.168.122.110 + alpine2: + ansible_host: 192.168.122.111 + alpine3: + ansible_host: 192.168.122.112 diff --git a/test/myVault.yml b/test/myVault.yml new file mode 100644 index 0000000..9c0fd5c --- /dev/null +++ b/test/myVault.yml @@ -0,0 +1,7 @@ +$ANSIBLE_VAULT;1.1;AES256 +37356139323466666131323035383834643462353530653635396438353032373936323765333165 +6162663932393162656562376332333633666637383664350a613637333433616263373266373533 +31353263386135333164623439306661396264383062353831393335633962353133303539303965 +3537626139346537610a376631346263623333633333666333386331306630306163336237383332 +35383964613462306462343762313931316264383931336132383261373161333763616132633061 +6264366366656533343565626564613136306564383038663263 diff --git a/test/vars/vars.yml b/test/vars/vars.yml new file mode 100755 index 0000000..670a267 --- /dev/null +++ b/test/vars/vars.yml @@ -0,0 +1,4 @@ +dir_src: ~/Images +dir_dst: /tmp/images +file01: wallpaper.jpg +file02: wallpaper.txt