Ansible setup
https://github.com/ansible/ansible
Original guide
How to install apps remotely with Ansible - TechRepublic
Related articles
Ansible // Bodacious Blog
1
2
3
4
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Set up ssh

Get the IP of the instance

1
2
aws ec2 describe-instances --output json |
    jq -r '.Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp'
54.206.107.77

Set the Host options

1
2
3
4
Host littlefella
    Hostname 54.206.107.77
    User ec2-user
    IdentityFile /home/shane/.ssh/ids/amazonlinuxuser

Set up passwordless SSH using ssh keys

1
2
pvt2pub ~/.ssh/ids/amazonlinuxuser
ssh-copy-id -oBatchMode=no -i ~/.ssh/ids/amazonlinuxuser ec2_user@littlefella

Set up ansible host

1
2
sudo mkdir /etc/ansible
suvi /etc/ansible/hosts

/etc/ansible/hosts

1
2
[amazonlinux_servers]
littlefella ansible_user=ec2-user
  • You must use the ssh host, not the IP address

Run the one-off installation command

1
ansible amazonlinux_servers -b --become-user=root -m yum -a "name=tmux state=present"

Create the playbook

  • Declare the hosts
  • Gain sudo privileges
  • Install the latest version of tmux
  • Update the apt cache

apt.yml

1
2
3
4
5
6
7
8
9
- hosts: amazonlinux_servers
  tasks:
    - name: Install tmux to azl
      become: true
      become_user: root
      apt:
        name: tmux
        state: present
        update_cache: true

Run the playbook

1
ansible-playbook apt.yml --ask-become-pass

This is what multiple package installation looks like

1
2
3
4
5
6
apt:
    name:
    - zip
    - wget
    state: present
    update_cache: true

Resources

https://github.com/ansible/ansible-examples

Create windows user

https://github.com/ansible/ansible-examples/blob/master/windows/create-user.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
---
- name: Add a user
  hosts: all
  gather_facts: false
  tasks:
    - name: Add User
      win_user:
        name: ansible
        password: "@ns1bl3"
        state: present

Install Internet Information Services (ISS)

https://github.com/ansible/ansible-examples/blob/master/windows/enable-iis.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
---
# This playbook installs and enables IIS on Windows hosts

- name: Install IIS
  hosts: all
  gather_facts: false
  tasks:
    - name: Install IIS
      win_feature:
        name: "Web-Server"
        state: present
        restart: yes
        include_sub_features: yes
        include_management_tools: yes

Supporting scripts

pvt2pub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash
export TTY

( hs "$(basename "$0")" "$@" "#" "<==" "$(ps -o comm= $PPID)" 0</dev/null ) &>/dev/null

# Private key path
fp="$1"
rp="$(realpath "$fp")"
dn="$(dirname "$rp")"
bn="$(basename "$fp")"
fn="${bn%.*}"

set -xv
ssh-keygen -y -f "$fp" > "$dn/$fn.pub"

Create a script which automtates ssh with the one-off command

1
ansible amazonlinux_servers -b --become-user=root -m yum -a "name=tmux state=present"

Figure out how to specify ssh host instead of ansible host when running a one-off command

1
egr ansible specify ssh host instead of ansible host