From adfbfffa18f59f418cf93d79f91d79b07e9463ff Mon Sep 17 00:00:00 2001 From: eva Date: Thu, 23 Nov 2023 19:25:51 -0500 Subject: [PATCH 1/3] set and unset variables --- README.md | 16 +++++++++------- av | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4030267..365c9c0 100644 --- a/README.md +++ b/README.md @@ -241,13 +241,15 @@ Commands: resume Resume periodic runs after a pause Host actions: - host add Add the current host to the inventory - host add-module Add the module to this host in the inventory - host remove-module Remove the module from this host in the inventory - host add-role Add the role to this host in the inventory - host remove-role Remove the role from this host in the inventory - host diff See what has changed in the local inventory - host push Push local inventory changes up to the git origin + host add Add the current host to the inventory + host add-module Add the module to this host in the inventory + host remove-module Remove the module from this host in the inventory + host add-role Add the role to this host in the inventory + host remove-role Remove the role from this host in the inventory + host set-variable Set a variable to some value on this host in the inventory + host unset-variable Unset a variable to some value on this host in the inventory + host diff See what has changed in the local inventory + host push Push local inventory changes up to the git origin ``` ## Why Bash? diff --git a/av b/av index d052534..cd2546a 100755 --- a/av +++ b/av @@ -59,13 +59,15 @@ Commands: list-roles List all available roles Host actions: - host [host...] Show attributes for the host(s) - host add Add the host to the inventory - host remove Remove the host from the inventory - host add-module Add the module to the host in the inventory - host remove-module Remove the module from the host in the inventory - host add-role Add the role to the host in the inventory - host remove-role Remove the role from the host in the inventory + host [host...] Show attributes for the host(s) + host add Add the host to the inventory + host remove Remove the host from the inventory + host add-module Add the module to the host in the inventory + host remove-module Remove the module from the host in the inventory + host add-role Add the role to the host in the inventory + host remove-role Remove the role from the host in the inventory + host set-variable Set a variable to some value on this host in the inventory + host unset-variable Unset a variable to some value on this host in the inventory EOF } @@ -299,6 +301,25 @@ function host_add_role { echo $role >> $roles_file } +function host_set_variable { + local host=$1 + local variable=$2 + local value=$3 + vars_file=$inventory_dir/hosts/$host/variables + + mkdir -p $inventory_dir/hosts/$host + echo "$(grep -v ^$variable= $vars_file)\n$variable='$value'" > $vars_file +} + +function host_unset_variable { + local host=$1 + local variable=$2 + vars_file=$inventory_dir/hosts/$host/variables + + mkdir -p $inventory_dir/hosts/$host + echo "$(grep -v ^$variable=)" > $vars_file +} + function check_roles { for role in $(ls -1 $inventory_dir/roles); do if [[ ! -e $inventory_dir/roles/$role/modules ]]; then @@ -469,6 +490,12 @@ case $command in remove-role) host_remove_role $host $subcommand_arg ;; + set-variable) + host_set_variable $host $subcommand_arg + ;; + unset-variable) + host_unset_variable $host $subcommand_arg + ;; *) for host in $command_arg; do entry=$(host_entry $host) From 1977fd38653eeda339b55b1b350231876a89de0e Mon Sep 17 00:00:00 2001 From: eva Date: Thu, 23 Nov 2023 19:33:33 -0500 Subject: [PATCH 2/3] read out the value since it's all in $2 --- av | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/av b/av index cd2546a..79f4df0 100755 --- a/av +++ b/av @@ -303,8 +303,7 @@ function host_add_role { function host_set_variable { local host=$1 - local variable=$2 - local value=$3 + read variable value<<<$2 vars_file=$inventory_dir/hosts/$host/variables mkdir -p $inventory_dir/hosts/$host From f04ae8533e21155724a139b8ca759c08b701b72b Mon Sep 17 00:00:00 2001 From: eva Date: Thu, 23 Nov 2023 19:33:51 -0500 Subject: [PATCH 3/3] make sure the file exists --- av | 2 ++ 1 file changed, 2 insertions(+) diff --git a/av b/av index 79f4df0..f0be819 100755 --- a/av +++ b/av @@ -307,6 +307,7 @@ function host_set_variable { vars_file=$inventory_dir/hosts/$host/variables mkdir -p $inventory_dir/hosts/$host + touch $vars_file echo "$(grep -v ^$variable= $vars_file)\n$variable='$value'" > $vars_file } @@ -316,6 +317,7 @@ function host_unset_variable { vars_file=$inventory_dir/hosts/$host/variables mkdir -p $inventory_dir/hosts/$host + touch $vars_file echo "$(grep -v ^$variable=)" > $vars_file }