D
D
Dmitry Kalinnikov2018-12-27 09:27:26
linux
Dmitry Kalinnikov, 2018-12-27 09:27:26

How does the lineinfile Ansible module work?

Greetings.
I'm trying to add a line to the network interface config using Ansible.
Ansible version: 2.7.2
Config:

# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=10.22.33.123
NETMASK=255.255.255.0
DEVICE=eth1
PEERDNS=no
#VAGRANT-END

Task:
- name: Configure Internal zone interface
  lineinfile:
    path: /etc/sysconfig/network-scripts/ifcfg-{{ interface }}
    regexp: '^ZONE'
    line: 'ZONE="internal"'
  notify: "restart network"
  become: true

That is, judging by the description of the module, if I don’t have a line , the line from line^ZONE= should be substituted . At the same time, during some runs on a fresh machine, it substitutes not just ZONE= . For some it works. Already tried:
ZONE="internal"
  • leave internal without quotes
  • change quotes around line
  • do runs without restarting the network
  • append to the beginning of the file withinsertbefore: BOF
  • change interfaces - edited both files of custom interfaces of vagrant and default

Writes a full line for 2-3 times (line replaced), with some runs - at the first one (line added).
Magic :S

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kalinnikov, 2018-12-28
@Growman

The problem turned out not to be from Ansible at all.
The zone is being rubbed either by a firewall, or by NetworkManager, after restarting the network, and semi-randomly
I did not find any solutions, except to remove NM from the system.

P
Puma Thailand, 2018-12-28
@opium

Well, turn on debugging after minus three V and minus diff, why guess on coffee grounds

S
Shoolcs, 2018-12-27
@noob1

When using lineinfile - ansinble searches for the given line in the file . If there is no line, then it writes, if there is a line, it changes it. Re-adding a line may be due to an invalid regexp value. This line was not found, so I added it again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question