2

My ansible inventory contains a group that has dynamically generated IP addresses.

[asg]
10.15.100.13
10.15.100.14

Is it possible to remove the line after [asg]? (i.e. 10.15.100.13)

The IP addresses in this group refers to the ec2 instances in an auto scaling group.

lineinfile module doesn't have a removeafter option.

I'd like to know if there are other alternative ways to remove the line after [asg].

regexp option doesn't work. Because the IP addresses change frequently.

Brian
  • 12,145
  • 20
  • 90
  • 153
  • Maybe you should consider using dynamic inventory (http://docs.ansible.com/ansible/intro_dynamic_inventory.html) instead of changing a static inventory with ansible itself. There are EC2 example in ansible source code. – zigarn Jun 25 '17 at 06:51
  • So every single playbook execution will delete one line until `[asg]` is the last one in a file? That doesn't seem like a task you should employ Ansible for. – techraf Jun 25 '17 at 10:03
  • Playbook1 adds the ip address under `[asg]`. Playbook2 uses this address. Playbook3 deletes the ip address under `[asg]`. That's the way I use ansible. – Brian Jun 25 '17 at 11:46
  • I'm afraid you dealing with an [XY Problem](https://meta.stackexchange.com/a/66378/305121). Even if you have to stick to inventory files, why don't you simply write the entry to a separate file in the inventory directory and delete the file afterwards? – techraf Jun 25 '17 at 12:20
  • @Brian I'm really curious if you have tested [the answer](https://stackoverflow.com/a/44747868/2947502) before accepting it, because it is impossible that it replaces two lines, as it claims. At best it will add two lines to the end of the file. – techraf Jun 26 '17 at 14:55
  • @techraf you are right as `lineinfile` does not use multi-line mode. My intention was to use `replace` but I wasn't aware I was following the OP title when typing the answer... – Chris Lam Jun 26 '17 at 15:44

2 Answers2

3

Totally possible with replace as long as regexp is supported.

- replace:
    path: /path/to/file
    regexp: '\[asg\]\n[^\n]+' # Matches [asg] and next line
    replace: '[asg]'          # Replace both lines with [asg]
Chris Lam
  • 3,526
  • 13
  • 10
0

I am not sure it is possible using lineinfile or blockinfile, but you can achieve it using sed. WARNING: This solution is not idempotent.

- shell: /bin/sed -i.bak '/\[asg\]/{n;d}' my_inventory_file

The command will delete the line after [asg]. It will automatically create a backup file with .bak extension. Depending on your OS, the path to sed and the arguments may vary. You can refine the regexp further by:

- shell: sed -i.bak '/^\s*\[asg\]\s*$/{n;d}' my_inventory_file

{n;d} or {n;d;} for MacOS - read the line (n)ext to pattern and (d)elete it

helloV
  • 50,176
  • 7
  • 137
  • 145
  • I got `extra characters at the end of d command` error with this solution. I think it's because the version of my sed is BSD sed. – Brian Jun 25 '17 at 12:40
  • `- shell: /bin/sed -i.bak '/\[asg\]/{n;d;}' my_inventory_file` works on my MacBook Pro. – Brian Jun 25 '17 at 12:44
  • But I still don't understand the meaning of `n;` in this command. – Brian Jun 25 '17 at 12:46
  • I tried it on Ubuntu, it worked. You are correct, it needs a little tweak for MacOs. @Brian I have added the meaning of `n` – helloV Jun 25 '17 at 13:45