first commit

This commit is contained in:
Cedric Girard 2018-07-27 10:01:50 +02:00
commit 7f35982f76
38 changed files with 17905 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.retry

View File

@ -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

9
playbooks/copy_from_dirs.yml Executable file
View File

@ -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}}/"

View File

@ -0,0 +1,6 @@
- hosts: coreos
gather_facts: no
vars_files:
- "{{inventory_dir}}/vars/vars.yml"
tasks:
- include_tasks: tasks/copy_files.yml

5
playbooks/coreos-bootstrap.yml Executable file
View File

@ -0,0 +1,5 @@
- hosts: coreos
gather_facts: False
roles:
- defunctzombie.coreos-bootstrap

View File

@ -0,0 +1,6 @@
- hosts: coreos
gather_facts: no
vars_files:
- "{{inventory_dir}}/vars/vars.yml"
tasks:
- include_tasks: tasks/delete_files.yml

7
playbooks/poweroff.yml Executable file
View File

@ -0,0 +1,7 @@
- hosts: alpine
gather_facts: false
tasks:
- name: poweroff
command: poweroff
become: true
ignore_errors: true

13
playbooks/report.yml Executable file
View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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.

View File

@ -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

View File

@ -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 <<EOF
#!/bin/bash
LD_LIBRARY_PATH=$HOME/pypy/lib:$LD_LIBRARY_PATH exec $HOME/pypy/bin/pypy "\$@"
EOF
chmod +x $HOME/bin/python
$HOME/bin/python --version
touch $HOME/.bootstrapped

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
#!/bin/bash
LD_LIBRARY_PATH=$HOME/pypy/lib:$LD_LIBRARY_PATH $HOME/pypy/bin/$(basename $0) $@

View File

@ -0,0 +1,10 @@
---
dependencies: []
galaxy_info:
author: defunctzombie
description: "bootstrap coreos hosts to run ansible"
license: "MIT"
min_ansible_version: 1.4
categories:
- system

View File

@ -0,0 +1,31 @@
- name: Check if bootstrap is needed
raw: stat $HOME/.bootstrapped
register: need_bootstrap
ignore_errors: True
- name: Run bootstrap.sh
script: bootstrap.sh
when: need_bootstrap | failed
- name: Check if we need to install pip
shell: "{{ansible_python_interpreter}} -m pip --version"
register: need_pip
ignore_errors: True
changed_when: false
when: need_bootstrap | failed
- name: Copy get-pip.py
copy: src=get-pip.py dest=~/get-pip.py
when: need_pip | failed
- name: Install pip
shell: "{{ansible_python_interpreter}} ~/get-pip.py"
when: need_pip | failed
- name: Remove get-pip.py
file: path=~/get-pip.py state=absent
when: need_pip | failed
- name: Install pip launcher
copy: src=runner dest=~/bin/pip mode=0755
when: need_pip | failed

View File

@ -0,0 +1,4 @@
---
- hosts: all
roles:
- coreos-bootstrap

View File

@ -0,0 +1,9 @@
#!/bin/bash
echo -n "Give me a user:"
read user
echo -n "Give me a password:"
read password
echo "Hello ${user} your password is ${password}"

View File

@ -0,0 +1,6 @@
- hosts: coreos
gather_facts: no
tasks:
- include_tasks: tasks/switch_root.yml
handlers:
- import_tasks: tasks/switch_user.yml

4
playbooks/tags.yml Normal file
View File

@ -0,0 +1,4 @@
- hosts: localhost
gather_facts: false
tasks:
- import_tasks: tasks/task_dummy.yml

View File

@ -0,0 +1,5 @@
- name: "Copy {{ file }} to /tmp"
copy:
src: "{{ file }}"
dest: /tmp/
mode: 0755

View File

@ -0,0 +1,7 @@
- name: "Copy two files from {{ dir_src }} to {{ dir_dst }}"
copy:
src: "{{dir_src}}/{{item}}"
dest: "{{dir_dst}}/"
loop:
- "{{file01}}"
- "{{file02}}"

View File

@ -0,0 +1,7 @@
- name: "delete two files in {{ dir_dst }}"
file:
path: "{{dir_dst}}/{{item}}"
state: absent
loop:
- "{{file01}}"
- "{{file02}}"

View File

@ -0,0 +1,6 @@
- name: "Ensure {{ module }} is present"
become: true
pip:
name: "{{ module }}"
state: present
version: 3.3

View File

@ -0,0 +1,4 @@
- name: Switch to root
become: true
shell: whoami
notify: switch to ansible_user

View File

@ -0,0 +1,3 @@
- name: switch to ansible_user
become_user: "{{ansible_user}}"
shell: whoami

View File

@ -0,0 +1,40 @@
- name: task 1
tags:
- a
debug:
- name: task 2
tags:
- b
debug:
- name: task 3
tags:
- c
debug:
- name: task 4
tags:
- d
debug:
- name: task 5
tags:
- e
debug:
- name: task 6
tags:
- a
debug:
- name: task 7
tags:
- b
debug:
- name: task 8
tags:
- c
debug:
- name: task 9
tags:
- d
debug:
- name: task 10
tags:
- e
debug:

12
playbooks/test_prompt.yml Normal file
View File

@ -0,0 +1,12 @@
- hosts: alpine
gather_facts: false
vars_prompt:
- name: foo
prompt: Give me your foo
private: yes
confirm: yes
tasks:
- name: print foo
debug:
msg: "foo: {{ foo }}"

17
playbooks/use_vault.yml Normal file
View File

@ -0,0 +1,17 @@
- hosts: alpine
gather_facts: no
vars_files:
- "{{ playbook_dir }}/../test/myVault.yml"
vars:
- module: pexpect
- file: "{{ playbook_dir }}/scripts/input_script.sh"
tasks:
- include_tasks: tasks/pip_module_present.yml
- include_tasks: tasks/copy_file_to_tmp.yml
- name: Input test
expect:
command: /bin/ash /tmp/input_script.sh
responses:
(?i)user: "{{ user }}"
(?i)password: "{{ password }}"

14
templates/report.html Executable file
View File

@ -0,0 +1,14 @@
<html>
<body>
{% for host in play_hosts | sort %}
<h3>Report: {{hostvars[host]['ansible_hostname']}}</h3>
<table>
<tr><td>hostname</td><td>{{hostvars[host]['ansible_hostname']}}</td></tr>
<tr><td>OS</td><td>{{hostvars[host]['ansible_distribution']}}</td></tr>
<tr><td>architecture</td><td>{{hostvars[host]['ansible_architecture']}}</td></tr>
<tr><td>role</td><td>{{hostvars[host]['role']}}</td></tr>
<tr><td>uptime_cmd</td><td>{{hostvars[host]['uptime']}}</td></tr>
</table>
{% endfor %}
</body>
</html>

3
test/group_vars/alpine.yml Executable file
View File

@ -0,0 +1,3 @@
ansible_user: alpine
role: testvm
ansible_python_interpreter: python3

3
test/group_vars/coreos.yml Executable file
View File

@ -0,0 +1,3 @@
ansible_user: core
ansible_python_interpreter: /home/core/bin/python
role: testvm

1
test/group_vars/devoteam.yml Executable file
View File

@ -0,0 +1 @@
ansible_user: cgirard

View File

@ -0,0 +1 @@
role: Devoteam Laptop

22
test/hosts.yml Executable file
View File

@ -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

7
test/myVault.yml Normal file
View File

@ -0,0 +1,7 @@
$ANSIBLE_VAULT;1.1;AES256
37356139323466666131323035383834643462353530653635396438353032373936323765333165
6162663932393162656562376332333633666637383664350a613637333433616263373266373533
31353263386135333164623439306661396264383062353831393335633962353133303539303965
3537626139346537610a376631346263623333633333666333386331306630306163336237383332
35383964613462306462343762313931316264383931336132383261373161333763616132633061
6264366366656533343565626564613136306564383038663263

4
test/vars/vars.yml Executable file
View File

@ -0,0 +1,4 @@
dir_src: ~/Images
dir_dst: /tmp/images
file01: wallpaper.jpg
file02: wallpaper.txt