1
0
Fork 0
mirror of https://github.com/kasmtech/ansible.git synced 2026-01-23 02:25:25 +00:00

KASM-1829 Create new ansible playbook that installs Kasm Workspaces in multi server configuration.

This commit is contained in:
Ian Tangney 2021-09-08 22:39:52 +00:00 committed by Justin Travis
parent 3862f63a9f
commit 8b5c662c5f
13 changed files with 289 additions and 79 deletions

129
README.md
View file

@ -1,91 +1,62 @@
# Ansible
### Installing Ansible
This project requires ansible 2.9.24 or greater on the host running the ansible playbook. The target systems do no not need Ansible installed.
The steps below for installing Ansible have been tested on CentOS 7.9.2009, CentOS 8.4.2105, Debian 9.13, Debian 10.10, Ubuntu 18.04.5, and Ubuntu 20.04.3.
1. Ensure pip3 is installed
Ubuntu/Debian: `sudo apt install python3-pip`
CentOS: `sudo yum install python3-pip`
2. Add local bin directory to path in bashrc
```
echo 'PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc
source ~/.bashrc
```
3. Use pip to install ansible
`python3 -m pip install --user -U pip && python3 -m pip install --user -U ansible`
4. Ensure that ansible version is greater than 2.9.24
`ansible --version`
## Kasm Multi Server install
This project will deploy Kasm Workspaces in a multi-server deployment using Ansible.
* It installs the kasm components on the systems specified in the ansible `inventory` required for the respective roles (db, web, agent).
* It creates a new swapfile to ensure that the total swap space matches the size `desired_swap_size` specified on the files in group_vars/.
* It enables the docker daemon to run at boot to ensure that kasm services are started after a reboot.
It has been tested on CentOS 7.9.2009, CentOS 8.4.2105, Debian 9.13, Debian 10.10, Ubuntu 18.04.5, and Ubuntu 20.04.3
![Diagram][Image_Diagram]
[Image_Diagram]: https://f.hubspotusercontent30.net/hubfs/5856039/Ansible/Ansible%20Multi%20Server.png "Diagram"
## Getting started
### Ansible Configuration
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
1. Open `roles/install_common/vars/main.yml`, `group_vars/agent.yml` and update variables if desired.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
2. Open `inventory` file and fill in the hostnames / ips for the servers that will be fulfilling the agent, webapp and db roles.
3. Run the deployment.
## Add your files
`ansible-playbook -Kk -u [username] -i inventory install_kasm.yml`
- [ ] [Create](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
Ansible will prompt you for the ssh password and sudo password (will almost always be the same password).
```
cd existing_repo
git remote add origin https://gitlab.com/kasm-technologies/internal/ansible.git
git branch -M main
git push -uf origin main
```
Or, if you have ssh keys copied over to your servers and have NOPASSWD in sudoers you can just run.
## Integrate with your tools
`ansible-playbook -u [username] -i inventory install_kasm.yml`
- [ ] [Set up project integrations](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/integrations/)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Automatically merge when pipeline succeeds](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://docs.gitlab.com/ee/user/clusters/agent/)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://gitlab.com/-/experiment/new_project_readme_content:54802fa94dad05b594c597bfbb8c3510?https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
4. Login to the deployment as admin@kasm.local using the IP of one of the WebApp servers (eg https://192.168.1.2)
5. Navigate to the Agents tab, and enable each Agent after it checks in. (May take a few minutes)

2
group_vars/agent.yml Normal file
View file

@ -0,0 +1,2 @@
# This generally should be (1g x number_of_sessions / number_of_agents)
desired_swap_size: 5g

1
group_vars/db.yml Normal file
View file

@ -0,0 +1 @@
desired_swap_size: 4g

1
group_vars/web.yml Normal file
View file

@ -0,0 +1 @@
desired_swap_size: 4g

7
install_kasm.yml Normal file
View file

@ -0,0 +1,7 @@
- hosts:
- db
- web
- agent
roles:
- install_common
any_errors_fatal: true

6
inventory Normal file
View file

@ -0,0 +1,6 @@
[web]
ubuntu18-web
[db]
ubuntu18-db
[agent]
ubuntu18-agent

View file

@ -0,0 +1,10 @@
- name: Check connection from agent to webserver
uri:
url: "https://{{ web_ip }}/api/__healthcheck"
timeout: 5
validate_certs: false
- name: Install agent role
command: "bash {{ tempdir.path }}/kasm_release/install.sh -S agent -e -p {{ target_ip }} -m {{ web_ip }} -M {{ manager_token }}"
register: web_install
become: true

View file

@ -0,0 +1,4 @@
- name: Install database role
command: "bash {{ tempdir.path }}/kasm_release/install.sh -S db -e -Q {{database_password}} -R {{redis_password}} -U {{user_password}} -P {{admin_password}} -M {{manager_token}}"
register: db_install
become: true

View file

@ -0,0 +1,32 @@
# Setup default creds if users don't set them in the vars/main.yml
- set_fact:
database_password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
when: database_password is not defined
run_once: true
delegate_to: localhost
- set_fact:
redis_password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
when: redis_password is not defined
run_once: true
delegate_to: localhost
- set_fact:
user_password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
when: user_password is not defined
run_once: true
delegate_to: localhost
- set_fact:
admin_password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
when: admin_password is not defined
run_once: true
delegate_to: localhost
- set_fact:
manager_token: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
when: manager_token is not defined
run_once: true
delegate_to: localhost

View file

@ -0,0 +1,111 @@
- include_tasks:
file: default_credentials.yml
- name: Check if Kasm is installed
stat:
path: /opt/kasm/current
register: kasm_path
- assert:
that:
- not kasm_path.stat.exists
fail_msg: /opt/kasm/current exists, kasm appears to be installed. Please ensure kasm is uninstalled before continuing.
- set_fact:
db_ip: "{{ hostvars[groups['db'][0]]['ansible_default_ipv4']['address'] }}"
web_ip: "{{ hostvars[groups['web'][0]]['ansible_default_ipv4']['address'] }}"
# IP of the host that ansible is being ran against
target_ip: "{{ ansible_default_ipv4.address }}"
- name: Override manager hostname if configured
set_fact:
web_ip: "{{ manager_hostname }}"
when: manager_hostname is defined
- name: Check if kasm swapfile exists
stat:
path: /mnt/kasm.swap
register: kasm_swapfile
- name: Get current swapsize in bytes
# Meminfo outputs in Kb for some reason so we convert to bytes
shell: cat /proc/meminfo | grep SwapTotal | awk '{print $2 * 1024}'
register: current_swap_size
- set_fact:
# We only want to make a swapfile large enough to make up the difference between
# the current swapsize and our desired size.
new_swap_size: "{{ desired_swap_size | human_to_bytes - current_swap_size.stdout | int }}"
- debug:
var: new_swap_size
- name: Run swap tasks
include_tasks:
file: mkswap.yml
when:
- new_swap_size | int > 0
- not kasm_swapfile.stat.exists
- name: Create temporary directory
tempfile:
state: directory
register: tempdir
# Debian 10 doesn't ship with the ca-certificates package installed by default
# installing curl is portable to to ensure that ca-certificates is installed
- name: Ensure we have curl installed
package:
name: curl
state: present
become: true
- name: Download kasm installer
get_url:
url: "{{ kasm_installer_url }}"
dest: "{{ tempdir.path }}/kasm.tar.gz"
checksum: "{{ kasm_installer_checksum }}"
register: kasm_installer
- name: unarchive kasm installer
unarchive:
remote_src: yes
src: "{{ kasm_installer.dest }}"
dest: "{{ tempdir.path }}"
- name: Run Kasm db install tasks
include_tasks:
file: db_install.yml
when: "'db' in group_names"
- name: Run Kasm web install tasks
include_tasks:
file: web_install.yml
when: "'web' in group_names"
- name: Run Kasm agent install tasks
include_tasks:
file: agent_install.yml
when: "'agent' in group_names"
- name: enable the docker service to run at boot
service:
name: docker
enabled: true
become: true
when: start_docker_on_boot
- name: Delete temporary directory
file:
path: "{{ tempdir.path }}"
state: absent
- name: Print credentials
debug:
msg:
- "Database Password: {{ database_password }}"
- "Redis Password: {{ redis_password }}"
- "Manager Token: {{ manager_token }}"
- "user@kasm.local password: {{ user_password }}"
- "admin@kasm.local password: {{ admin_password }}"
run_once: true

View file

@ -0,0 +1,26 @@
- name: Create swap file
# We can't use falloc because it creates "file with holes"
# https://man.archlinux.org/man/swapon.8#Files_with_holes
command: "dd if=/dev/zero bs=1M count={{ (new_swap_size | int / 1024 / 1024) | int }} of=/mnt/kasm.swap"
become: true
- name: Set swapfile permissions
file:
path: /mnt/kasm.swap
mode: 0600
become: true
- name: Run mkswap command
command: mkswap /mnt/kasm.swap
become: true
- name: Mount swap on boot
lineinfile:
path: /etc/fstab
line: "/mnt/kasm.swap swap swap defaults 0 0"
become: true
- name: Run swapon
command: swapon /mnt/kasm.swap
become: true

View file

@ -0,0 +1,16 @@
- name: Check connection from web to postgres on db server
wait_for:
port: 5432
host: "{{ db_ip }}"
timeout: 60
- name: Check connection from web to redis on db server
wait_for:
port: 6379
host: "{{ db_ip }}"
timeout: 60
- name: Install web role
command: "bash {{ tempdir.path }}/kasm_release/install.sh -S app -e -q {{ db_ip }} -Q {{ database_password }} -R {{ redis_password }} -n {{ target_ip }}"
register: web_install
become: true

View file

@ -0,0 +1,23 @@
kasm_installer_url: https://kasm-static-content.s3.amazonaws.com/kasm_release_1.9.0.077388.tar.gz
kasm_installer_checksum: sha256:d925a20ee949ef6a9759587069983eb3a6d56b0532210e74be75894b2a5915ce
# If you want custom passwords change them below, otherwise they will be auto generated and displayed
# in a message at the end of the run.
# Password for user@kasm.local in webui
#user_password: changeme
# Password for admin@kasm.local in webui
#admin_password: changeme
# Password that webapp uses to connect to postgres
#database_password: changeme
# Passwird that webapp uses to connect to redis
#redis_password: changeme
# Token that agents use to connect to webapp
#manager_token: changeme
# Start docker daemon at boot
start_docker_on_boot: true