2
\$\begingroup\$

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
        - cmelgreen@gmail.com
        - 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 }}'
\$\endgroup\$
6
  • 1
    \$\begingroup\$ You could add 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 new ansible-playbook then it's just up vagrant up to get your reproducible environments and vagrant destroy to tear them down when you're done. \$\endgroup\$
    – masseyb
    Oct 1 '20 at 18:14
  • 1
    \$\begingroup\$ The end goal of everything being you could wipe everything but my GitHub account and aws credentials and with one or to commands spin up everything from scratch \$\endgroup\$
    – Coupcoup
    Oct 1 '20 at 18:57
  • 1
    \$\begingroup\$ Yes, is made for it. I used to use vagrant + virtualbox locally for running and provisioning a VM using ansible-playbooks then when I was happy with the deployment specs I'd use the same ansible-playbooks to provision AWS EC2 instances (slightly different because I build AMI's on schedule using packer 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 the ansible-playbook run. \$\endgroup\$
    – masseyb
    Oct 1 '20 at 19:00
  • 1
    \$\begingroup\$ Using 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 run ansible-playbook's) then purge it with vagrant destroy when you're done with it. I have an example project (albeit a little outdated) to bootstrap 3 VM's using vagrant then install docker and configure docker swarm using an ansible-playbook. Nowadays irl I'd suggest all the things in docker! \o/ \$\endgroup\$
    – masseyb
    Oct 1 '20 at 19:09
  • 1
    \$\begingroup\$ If you're deploying to AWS (or developing in AWS) there exists a vagrant-aws plugin. \$\endgroup\$
    – masseyb
    Oct 1 '20 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.