SSH, Forks & Rolling Updates
Against real servers, Ansible connects with OpenSSH, so everything from SSH Keys & ssh-agent applies: keys in an agent, ~/.ssh/config, bastion hosts with ProxyJump, host key checking.
Connection settings
| Variable / setting | Purpose |
|---|---|
ansible_user / remote_user |
login user |
ansible_host, ansible_port |
address and port |
ansible_ssh_private_key_file |
key to use (prefer the agent) |
ansible_ssh_common_args: '-o ProxyJump=bastion.example.com' |
reach private hosts through a bastion |
host_key_checking = False |
only for throwaway lab VMs; in production, pre-load known_hosts |
pipelining = True |
fewer SSH round trips per task (big speed-up) |
ssh_args = -o ControlMaster=auto -o ControlPersist=60s |
reuse connections |
An inventory for a private subnet behind a bastion:
[web]
web1 ansible_host=10.0.1.10
web2 ansible_host=10.0.1.11
[web:vars]
ansible_user=ada
ansible_ssh_common_args='-o ProxyJump=ada@bastion.example.com'Forks: how many hosts at once
forks (default 5) is the number of hosts Ansible works on in parallel within a task. With 200 hosts and forks = 5, each task runs in 40 waves. Raise it (-f 50, or in ansible.cfg) as long as the control node's CPU and memory keep up; each fork is a Python process plus an SSH connection.
serial: rolling updates
By default a play runs each task on all hosts before the next. For a deployment that restarts the app, that means every server restarts at the same moment: an outage. serial turns a play into batches: the whole play runs on the first batch, then the next.
cat > ansible.cfg <<'INI'
[defaults]
inventory = hosts.ini
interpreter_python = auto_silent
forks = 1
INI
printf '[web]\nweb[1:5] ansible_connection=local\n' > hosts.ini
cat > rolling.yml <<'YML'
- name: Rolling deploy
hosts: web
gather_facts: false
serial: [1, 2, "100%"] # a canary, then 2, then the rest
max_fail_percentage: 0 # any failure stops the rollout
tasks:
- name: Take out of the load balancer
ansible.builtin.debug:
msg: "drain {{ inventory_hostname }}"
- name: Deploy and restart
ansible.builtin.debug:
msg: "deploy {{ inventory_hostname }}"
YML
ansible-playbook rolling.yml | grep -E '^PLAY|"msg"'PLAY [Rolling deploy] **********************************************************
"msg": "drain web1"
"msg": "deploy web1"
PLAY [Rolling deploy] **********************************************************
"msg": "drain web2"
"msg": "drain web3"
"msg": "deploy web2"
"msg": "deploy web3"
PLAY [Rolling deploy] **********************************************************
"msg": "drain web4"
"msg": "drain web5"
"msg": "deploy web4"
"msg": "deploy web5"
PLAY RECAP *********************************************************************
Each batch is its own PLAY [...] section. If a host in the canary batch fails, max_fail_percentage: 0 stops the run before the other four hosts are touched.
Taking hosts out of the load balancer: delegate_to
The tasks that drain a host run against another machine (the load balancer, or an API from the control node). delegate_to runs one task elsewhere, with the current host's variables:
- name: Rolling deploy behind HAProxy
hosts: web
serial: 1
become: true
tasks:
- name: Disable in HAProxy
community.general.haproxy:
state: disabled
host: "{{ inventory_hostname }}"
backend: linkly
delegate_to: "{{ item }}"
loop: "{{ groups['lb'] }}"
- name: Deploy the new release
ansible.builtin.unarchive:
src: "linkly-{{ release }}.tar.gz"
dest: /srv/linkly/releases/
notify: Restart linkly
- name: Restart now, not at the end of the play
ansible.builtin.meta: flush_handlers
- name: Wait until healthy
ansible.builtin.uri:
url: "http://{{ ansible_host }}:8080/healthz"
register: health
until: health.status == 200
retries: 20
delay: 3
- name: Enable in HAProxy
community.general.haproxy:
state: enabled
host: "{{ inventory_hostname }}"
backend: linkly
delegate_to: "{{ item }}"
loop: "{{ groups['lb'] }}"
handlers:
- name: Restart linkly
ansible.builtin.systemd_service:
name: linkly
state: restarteddelegate_to: localhost is common for API calls (cloud, DNS, chat notifications), and run_once: true runs a task for only one host of the batch (a database migration, say).
Strategies
| Strategy | Behaviour |
|---|---|
linear (default) |
every host finishes a task before any host starts the next |
free |
each host runs through the play as fast as it can; output interleaves |
host_pinned |
like free, but a fork stays with its host until it is done |
free speeds up plays on hosts with very different speeds, but makes the output harder to read and serial/run_once behave differently. Most teams stay with linear.
Timeouts and long tasks
async + poll run a long task (a backup, a big package upgrade) without holding the SSH connection open; wait_for and wait_for_connection wait for a port or for a host to come back after ansible.builtin.reboot.
Test yourself
serial: [1, 2, "100%"] on 5 hosts gives which batches?
- 5
- 1, 2, 2
- 1, 2, 5
- 1, 1, 1, 1, 1
What does delegate_to: lb1 change?
- The task runs on lb1, with the current host's variables
- The play's hosts
- The become user
- The inventory
What are forks?
- Git branches of a playbook
- The number of hosts handled in parallel within a task
- Copies of a role
- Retries