Project for automatic server setup using ansible.
Tested on Ubuntu Server 18.04.2 LTS and ansible 2.8.0.
-
Create config file
~/.ansible.cfg:[defaults] inventory = ~/.ansible.hosts vault_password_file = ~/.ansible.vault.pass [ssh_connection] ssh_args = -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s pipelining = TrueFew notes:
- Pipelining can make significant performance improvement when enabled, but have
incompatibility
with
requirettyin/etc/sudoers. - We will use
ssh-agentand agent forwarding to exploit ssh keys from local machine on remotes. So don't forget to runssh-addbefore running scripts.
- Pipelining can make significant performance improvement when enabled, but have
incompatibility
with
-
Create hosts file
~/.ansible.hosts.
For example:[web] user@1.1.1.1 -
Create file
~/.ansible.vault.passwith password for ansible vault.
For example:somesecretpassformyvault
-
Download project:
git clone https://github.com/artslob/vps-setup cd vps-setup -
Create file
secrets.ymlin project root directory with this template:vault_user_password: somepass vault_user_salt: somesalt vault_cf_key: deadbeaf01010101010101010101010101fff vault_cf_email: user@example.comThis file contains secrets for user creation, cloudflare tokens for ssl setup (acme).
-
Encrypt it:
ansible-vault encrypt secrets.ymlContents of secrets file should be something like this (run
cat secrets.yml):$ANSIBLE_VAULT;1.1;AES256 31643131623866643738666533313633366533633133353534633461626355366230623339616437 ...
-
Run playbook to create user on your server:
ansible-playbook 01-create-user.yml -e "host_env=ec2 root_user=ubuntu"Flag
-e(or--extra-vars) provides additional environment variables, which override default values in playbook.
Contents of encryptedsecrets.ymlparsed by ansible automatically.Few notes:
- This playbook will not create ssh keys on remote machine. Reason for this is because you should use SSH agent forwarding to exploit your keys from local machine on remotes.
- Playbook setup switching to
sudomode without password for default user. - Also setup for all
sudoerspreserving ofSSH_AUTH_SOCKenvironment variable to exploit SSH agent forwarding insudomode.