mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-23 02:35:38 +01:00
Preserve host attributes when updating ansible_host
This commit is contained in:
parent
d5f7859f63
commit
b9412672ff
1 changed files with 50 additions and 3 deletions
|
|
@ -259,9 +259,56 @@ _ans_i_host_add() {
|
||||||
if grep -q "^[[:space:]]*$host:" "$inv"; then
|
if grep -q "^[[:space:]]*$host:" "$inv"; then
|
||||||
_ans_i_echo "host exists: $host -> updating ansible_host"
|
_ans_i_echo "host exists: $host -> updating ansible_host"
|
||||||
awk -v h="$host" -v ip="$ip" '
|
awk -v h="$host" -v ip="$ip" '
|
||||||
$0 ~ "^[[:space:]]*"h":" {print; getline; print " ansible_host: "ip; skip=1; next}
|
function indent_len(line) {
|
||||||
skip==1 { if ($0 ~ /ansible_host:/) { skip=0; next } }
|
match(line, /^[[:space:]]*/)
|
||||||
{print}
|
return RLENGTH
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
if (inhost) {
|
||||||
|
cur_indent = indent_len($0)
|
||||||
|
|
||||||
|
if ($0 ~ /^[[:space:]]*$/) {
|
||||||
|
print
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cur_indent > host_indent) {
|
||||||
|
temp = $0
|
||||||
|
sub(/^[[:space:]]*/, "", temp)
|
||||||
|
if (temp ~ /^ansible_host:[[:space:]]*/) {
|
||||||
|
printf "%sansible_host: %s\n", attr_spaces, ip
|
||||||
|
replaced = 1
|
||||||
|
next
|
||||||
|
}
|
||||||
|
print
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!replaced) {
|
||||||
|
printf "%sansible_host: %s\n", attr_spaces, ip
|
||||||
|
replaced = 1
|
||||||
|
}
|
||||||
|
inhost = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($0 ~ "^[[:space:]]*" h ":") {
|
||||||
|
print
|
||||||
|
inhost = 1
|
||||||
|
host_indent = indent_len($0)
|
||||||
|
attr_spaces = sprintf("%*s", host_indent + 2, "")
|
||||||
|
replaced = 0
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
print
|
||||||
|
}
|
||||||
|
|
||||||
|
END {
|
||||||
|
if (inhost && !replaced) {
|
||||||
|
printf "%sansible_host: %s\n", attr_spaces, ip
|
||||||
|
}
|
||||||
|
}
|
||||||
' "$inv" > "$inv.tmp" && mv "$inv.tmp" "$inv"
|
' "$inv" > "$inv.tmp" && mv "$inv.tmp" "$inv"
|
||||||
else
|
else
|
||||||
_ans_i_echo "adding host: $host ($ip)"
|
_ans_i_echo "adding host: $host ($ip)"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue