Setup Development Environment with Ansible

2 minute read

Prerequsite

Setup ssh on target machine

sudo apt install openssh-server
ssh-copy-id 192.168.1.103

Install ansible on control node

sudo apt install python3-pip

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
python3 -m pip install --user ansible

python3 -m pip install --upgrade --user ansible
python3 -m pip show ansible

Install packages and setup linux with ansible

Create a file called inventory containing the ip address that are ready for installation:

[flash_nodes]
192.168.1.103

[build_nodes]
192.168.1.102

And then create the playbook as follows:

cat playbook.yml

- hosts: all
  become: yes
  tasks:

    - name: Development Environment Setup
      ansible.builtin.apt:
        update_cache: yes
        name:
          - android-tools-adb
          - cmake
          - curl
          - fd-find
          - fonts-wqy-microhei
          - git
          - libavahi-compat-libdnssd-dev
          - libcurl4-openssl-dev
          - libreadline-dev
          - libssl-dev
          - libxtst-dev
          - mutt
          - ncdu
          - pandoc
          - picocom
          - python3-pip
          - python3-usb
          - qtbase5-dev
          - ripgrep
          - sendmail
          - texlive-xetex
          - vim
          - xdotool
          - zsh
        state: present

    - name: Remove Unused packages
      ansible.builtin.apt:
        name:
          - evolution
          - firefox
        state: absent

    - name: Cleanup
      ansible.builtin.shell: |
        rmdir /home/{{ansible_user}}/Documents
        rmdir /home/{{ansible_user}}/Downloads
        rmdir /home/{{ansible_user}}/Music
        rmdir /home/{{ansible_user}}/Pictures
        rmdir /home/{{ansible_user}}/Public
        rmdir /home/{{ansible_user}}/Templates
        rmdir /home/{{ansible_user}}/Videos

        exit 0
      args:
        executable: /bin/bash

    - name: ssh key
      ansible.builtin.copy:
        src: /home/{{ansible_user}}/.ssh/id_rsa
        dest: /home/{{ansible_user}}/.ssh/id_rsa
        owner: "{{ansible_user}}"
        group: "{{ansible_user}}"
        mode: '0600'

    - name: Basic Setup
      ansible.builtin.shell: |
        gsettings set org.gnome.shell favorite-apps "['org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop']"

        # grant access to /dev/ttyUSBX (and friends)
        adduser `whoami` dialout

        # Add current user to group docker
        usermod -aG docker $(whoami)

        exit 0
      args:
        executable: /bin/bash

    - name: Disable blank screen
      ansible.builtin.shell: |
        dconf write /org/gnome/desktop/session/idle-delay 'uint32 0'

        exit 0
      args:
        executable: /bin/bash

    - name: Git Configuration
      ansible.builtin.shell: |
        git config --global http.sslVerify false
        git config --global http.postBuffer 1048576000

        exit 0
      args:
        executable: /bin/bash

    - name: Github
      ansible.builtin.shell: |
        grep raw.githubusercontent.com /etc/hosts
        if [ $? -ne 0 ]; then
            echo "185.199.108.133 raw.githubusercontent.com" >> /etc/hosts
        fi

        exit 0
      args:
        executable: /bin/bash

    - name: nnn
      ansible.builtin.shell: |
        wget -c https://github.com/jarun/nnn/releases/download/v4.6/nnn-v4.6.tar.gz
        tar xvf nnn-v4.6.tar.gz
        cd nnn-4.6
        sudo make strip install

        exit 0
      args:
        executable: /bin/bash

    - name: oh-my-zsh
      ansible.builtin.shell: |
        wget -c https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh
        runuser -u {{ansible_user}} -- bash install.sh --unattended
        usermod -s /usr/bin/zsh {{ansible_user}}

        exit 0
      args:
        executable: /bin/bash

    - name: aliases
      ansible.builtin.copy:
        src: /home/{{ansible_user}}/.oh-my-zsh/custom/aliases.zsh
        dest: /home/{{ansible_user}}/.oh-my-zsh/custom/aliases.zsh
        owner: "{{ansible_user}}"
        group: "{{ansible_user}}"
        mode: '0644'

    - name: fzf
      ansible.builtin.shell: |
        git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
        ~/.fzf/install

        exit 0
      args:
        executable: /bin/bash

    - name: Get vimrc
      ansible.builtin.git:
        repo: 'https://github.com/amix/vimrc'
        depth: 1
        dest: /home/{{ansible_user}}/.vim_runtime

    - name: Install vimrc
      ansible.builtin.shell: |
        sh /home/{{ansible_user}}/.vim_runtime/install_awesome_vimrc.sh

        exit 0
      args:
        executable: /bin/bash

    - name: vimrc customziation
      ansible.builtin.copy:
        src: /home/{{ansible_user}}/.vim_runtime/my_configs.vim
        dest: /home/{{ansible_user}}/.vim_runtime/my_configs.vim
        owner: "{{ansible_user}}"
        group: "{{ansible_user}}"
        mode: '0644'

    - name: Get Barrier
      ansible.builtin.git:
        repo: 'https://github.com/debauchee/barrier'
        depth: 1
        dest: /tmp/barrier

    - name: Install Barrier
      ansible.builtin.shell: |
        cd /tmp/barrier
        git submodule update --init --recursive
        ./clean_build.sh
        cd build
        sudo make install

        exit 0
      args:
        executable: /bin/bash

    - name: Get Auto Flash Utility
      ansible.builtin.git:
        repo: 'git@gitlab.baylabs.cc:{{ansible_user}}/autoflash'
        dest: /home/{{ansible_user}}/workdir/autoflash
        version: master
        accept_hostkey: yes
        key_file: /home/{{ansible_user}}/.ssh/id_rsa

The last step is to execute ansible-playbook command as below (add -v as needed):

ansible-playbook -i inventory playbook.yml -u baifudong -K [-vvv]