This is a follow-up to Bash script to automate dev environment setup.
In that question I'd thrown together a (sloppy) shell script to automatically setup my development environment. One of the answers suggested using Ansible and after a bit of reading realized it would help me with some configuration of remote servers as well so I decided to give it a go.
Below is playbook that sets up my dev environment the same way as the original bash script but hopefully a bit cleaner. I'm planning on using Ansible to setup a new CI/CD pipeline in a reproducible way as a replacement for the github -> dockerhub -> manual deployment so this is really testing the waters with Ansible before moving on to that.
Right now it all I need to do is clone the repo the two files are in and then run bootstrap.sh and everything gets set up from there.
Any/all pointers appreciated!
bootstrap.sh:
sudo apt update && sudo apt -y upgrade
sudo apt install -y ansible
mv AnsibleDevEnv/setup.yml ~/
ansible-playbook setup.yml
. .bash_profile
And then the Ansible playbook setup.yml:
---
- name: Dev Setup
hosts: localhost
vars:
folders:
- go
- python
- js
- pemKeys
downloads:
url:
- https://deb.nodesource.com/setup_14.x
- https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
- https://storage.googleapis.com/golang/getgo/installer_linux
sudo_files:
- setup_14.x
- Anaconda3-2020.02-Linux-x86_64.sh
user_files:
- installer_linux
keys:
- https://packages.microsoft.com/keys/microsoft.asc
- https://download.docker.com/linux/debian/gpg
repos:
- deb [trusted=yes arch=amd64] https://download.docker.com/linux/debian {{ docker_version.stdout }} stable
- deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main
packages:
- apt-transport-https
- ca-certificates
- gnupg2
- software-properties-common
- libgl1-mesa-glx
- libegl1-mesa
- libxrandr2
- libxrandr2
- libxss1
- libxcursor1
- libxcomposite1
- libasound2
- libxi6
- libxtst6
- libpq-dev
- python3-dev
- python3-pip
- protobuf-compiler
- apt-transport-https
- code
- nodejs
- postgresql-11
- docker-ce
node_lib:
- react
- react-scripts
- react-dom
go_get:
- go get github.com/lib/pq
- export
- GO111MODULE=on go get github.com/golang/protobuf/protoc-gen-go
- GO111MODULE=on go get -u google.golang.org/grpc
pip:
- psycopg2
git_config:
name:
- user.name
- user.email
- color.ui
value:
- cmelgreen
- [email protected]
- true
tasks:
- name: make folders
file:
path: './{{ item }}'
mode: 0755
state: directory
with_items: '{{ folders }}'
- name: install rpm
apt:
name: rpm
state: latest
update_cache: yes
become: yes
- name: add keys
apt_key:
state: present
url: '{{ item }}'
with_items: '{{ keys }}'
become: yes
- name: save docker version to variable
shell: lsb_release -cs
register: docker_version
- name: add repositories
apt_repository:
repo: '{{ item }}'
state: present
with_items: '{{ repos }}'
become: yes
- name: download files
get_url:
url: '{{ item }}'
dest: .
mode: +x
with_items: '{{ downloads.url }}'
- name: run as root downloads
command: './{{ item }}'
with_items: '{{ downloads.sudo_files }}'
become: yes
- name: run as user downloads
command: './{{ item }}'
with_items: '{{ downloads.user_files }}'
- name: add source ./.bashrc to .bash_profile
lineinfile:
path: ./.bash_profile
line: 'source ./.bashrc'
- name: install packages
apt:
name: '{{ packages }}'
state: latest
update_cache: yes
become: yes
- name: set docker permissions
file:
path: /var/run/docker.sock
mode: 0666
become: yes
- name: install react
npm:
name: '{{ item }}'
global: yes
state: latest
with_items: '{{ node_lib }}'
become: yes
- name: go get some libraries
shell: '. ~/.bash_profile && {{ item }}'
args:
executable: /bin/bash
with_items: '{{ go_get }}'
- name: pip some stuff conda has a hard time with
pip:
name: '{{ pip }}'
vagant
to your toolkit which you can use to spin up an environment (i.e. virtual machine, AWS EC2 instance, ..) and provision the instance directly with your newansible-playbook
then it's just upvagrant up
to get your reproducible environments andvagrant destroy
to tear them down when you're done. \$\endgroup\$vagrant
+virtualbox
locally for running and provisioning a VM usingansible-playbooks
then when I was happy with the deployment specs I'd use the sameansible-playbooks
to provision AWS EC2 instances (slightly different because I build AMI's on schedule usingpacker
for production). Is basically a CLI for managing VM's with their own config and provisioner hooks. There also exists a shell provisioner for executing shell commands after the machine is created and before theansible-playbook
run. \$\endgroup\$vagrant
you can bring it down to a single command for provisioning the dev environment (vagrant up
: will create a VM, execute shell commands then runansible-playbook
's) then purge it withvagrant destroy
when you're done with it. I have an example project (albeit a little outdated) to bootstrap 3 VM's usingvagrant
then installdocker
and configuredocker swarm
using anansible-playbook
. Nowadays irl I'd suggest all the things indocker
! \o/ \$\endgroup\$vagrant-aws
plugin. \$\endgroup\$