Reputation: 13593
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.
Upvotes: 2
Views: 6965
Reputation: 3614
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]
Upvotes: 3
Reputation: 52433
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
Upvotes: 0