19 Ansible
Ansible is a configuration management tool that can be used to configure bare metal servers with the right software.
Ansible has fundametal differences to Terraform, a provisioning tool.
19.1 Preparation
Create user ansible
.
sudo adduser \
--gecos ansible,,, \
ansible
sudo usermod -aG sudo ansible
sudo adduser \
--gecos ansible,,, \
ansible
sudo usermod -aG sudo ansible
Create SSH key for user ansible
:
su ansible
ssh-keygen \
-t ed25519 \
-N '' \
-C "ansible@notebooks.gesis.org" \
-f ~/.ssh/id_ed25519
ssh-copy-id ansible@194.95.75.9
exit
Nothing to do here.
Disable password authentication for user ansible
:
Add
Match User ansible
PasswordAuthentication no
to /etc/ssh/sshd_config
and restart the SSH server:
sudo systemctl restart ssh
Add
Match User ansible
PasswordAuthentication no
to /etc/ssh/sshd_config
and restart the SSH server:
sudo systemctl restart ssh
19.2 Installation
We will use GitLab CI to deploy changes to the machines.
19.3 Inventory
Ansible requires a list of servers that it can access. Edit ansible/inventories/production
to be like
[notebooks_gesis_org]
194.95.75.9
Check that your inventory is correct:
ansible \
-i ansible/inventories/production \
--list-hosts all
hosts (1):
194.95.75.9
Check that the servers are accessible:
ansible \
-i ansible/inventories/stage \
--user ansible \
--private-key ~/.ssh/id_ed25519 \
-m ping all
194.95.75.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
19.4 Playbook
Playbook is the rules that Ansible uses to configure the servers. For example,
- name: My first play
hosts: notebooks_gesis_org
tasks:
- name: Ping my hosts
ansible.builtin.ping:
- name: Print message
ansible.builtin.debug:
msg: Hello world
The instructions in the playbook can be executed by running
ansible-playbook playbook.yaml
where playbook.yaml
is the file storing the playbook.