I have nginx upstream with multiple backends. I use pre_tasks
in playbook for disabling backends in the upstream config during deployment:
- name: Deploy
hosts: '{{ list_hosts }}'
serial: 4
pre_tasks:
- name: Disable hosts in nginx upstream
replace:
path: /etc/nginx/conf.d/upstream.conf
regexp: '^ server {{ LAN }}:'
line: ' #server {{ LAN }}:5001;'
delegate_to: "{{ item }}"
with_items: "{{groups['nginx_api']}}"
...
It works, but not always correct. Sometimes it disables not all 4 hosts (serial: 4)
I think it happens because it tries to replace 4 lines at the same time.
Is it possible to do each iteration one by one and keep serial: 4
?
Update:
Sorry, my last test was with replace
To regexp added ^
(thanks @bviktor)
- name: Deploy
hosts: '{{ list_hosts }}'
serial: 4
pre_tasks:
- name: Disable server in nginx upstream
lineinfile:
path: /etc/nginx/conf.d/upstream.conf
regexp: '^ server {{ LAN }}:5001;'
line: ' #server {{ LAN }}:5001;'
delegate_to: "{{ item }}"
with_items: "{{groups['nginx_api']}}"
In inventory
[nginx_api]
api1 ansible_host=x.x.y.1 LAN=10.x.y.1
api2 ansible_host=x.x.y.2 LAN=10.x.y.2
api3 ansible_host=x.x.y.3 LAN=10.x.y.3
api4 ansible_host=x.x.y.4 LAN=10.x.y.4
[app]
app1 ansible_host=x.x.x.1 LAN=10.x.x.1
app2 ansible_host=x.x.x.2 LAN=10.x.x.2
app3 ansible_host=x.x.x.3 LAN=10.x.x.3
app4 ansible_host=x.x.x.4 LAN=10.x.x.4
app5 ansible_host=x.x.x.5 LAN=10.x.x.5
...
Upstream conf before run:
upstream app {
least_conn;
server 10.x.x.1:5001;
server 10.x.x.2:5001;
server 10.x.x.3:5001;
server 10.x.x.4:5001;
server 10.x.x.5:5001;
...
}
Upstream conf after run:
upstream app {
least_conn;
server 10.x.x.1:5001;
#server 10.x.x.2:5001;
server 10.x.x.3:5001;
server 10.x.x.4:5001;
server 10.x.x.5:5001;
...
}
Expected result:
upstream app {
least_conn;
#server 10.x.x.1:5001;
#server 10.x.x.2:5001;
#server 10.x.x.3:5001;
#server 10.x.x.4:5001;
server 10.x.x.5:5001;
...
}
Just when I run the playbook several times it disables 4 needed hosts in upstream