Score:-2

Lineinfile sometimes doesn't replace lines

gb flag

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

cl flag
We'd have to see the files to give you any meaningful advice...
cl flag
Also, you use "replace", not "lineinfile", as the title suggests.
artful avatar
gb flag
@bviktor I've updated question
br flag
[edit] the question and make it [Minimal, Complete, and Verifiable Example](https://dba.stackexchange.com/help/minimal-reproducible-example). Where does the group *nginx_api* come from? What is the example of *list_hosts*? What is the origin of *upstream.conf*? And what is the expected result? ...
br flag
https://idownvotedbecau.se/nomcve/ and asked to close.
artful avatar
gb flag
@VladimirBotka Thanks for the comment. I've edited the question
Score:2
cl flag

You give us very little to work with, but in general, it's not a very good practice to replace a pattern with something that is a superset of such pattern. Why? Because on consecutive runs, it will match over and over, since it includes the original pattern. So server {{ LAN }} becomes #server {{ LAN }}, then ##server {{ LAN }}, then ###server {{ LAN }}, and so on. Use ^ or some other pattern to indicate the preceding pattern. For example, if you use something like

regexp: '^server {{ LAN }}:'

It will only match if the line starts with server, i.e. it doesn't have any # preceding it, and therefore it won't put any more # characters before it upon consecutive runs.

artful avatar
gb flag
Thanks for the comment! I've updated my question
Score:-1
gb flag

The solution is to use a throttle

- 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 }}"
    throttle: 1
    with_items: "{{groups['nginx_api']}}"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.