Skip to content

Commit 5dfbb04

Browse files
fix: add check if file is already encrypted to prevent ansible vault from throwing an error
1 parent 04c0b1c commit 5dfbb04

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

solutions/standard-openshift/ansible/main.tf

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ resource "terraform_data" "execute_playbooks" {
122122
# Decrypt ocp config if it already exists
123123
provisioner "remote-exec" {
124124
inline = [
125-
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file; fi",
126-
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault decrypt ~/.powervs/config.json --vault-password-file password_file; fi"
125+
"if [ -f \"/root/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file; fi",
126+
"if [ -f \"/root/.powervs/config.json\" ]; then ansible-vault decrypt /root/.powervs/config.json --vault-password-file password_file; fi"
127127
]
128128
}
129129

@@ -145,8 +145,12 @@ resource "terraform_data" "execute_playbooks" {
145145
# Encrypt ocp config if it already exists
146146
provisioner "remote-exec" {
147147
inline = [
148-
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file; fi",
149-
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file; fi",
148+
"if [ -f \"/root/.powervs/config.json\" ]; then",
149+
" if ! ( head -n 1 | grep -q '^\\$ANSIBLE_VAULT' ); then",
150+
" echo ${var.ansible_vault_password} > password_file",
151+
" ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file",
152+
" fi",
153+
"fi",
150154
"rm -f password_file"
151155
]
152156
}
@@ -226,7 +230,7 @@ resource "terraform_data" "execute_playbooks_with_vault" {
226230
# Decrypt ocp config if it already exists
227231
provisioner "remote-exec" {
228232
inline = [
229-
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault decrypt ~/.powervs/config.json --vault-password-file password_file; fi"
233+
"if [ -f \"/root/.powervs/config.json\" ]; then ansible-vault decrypt /root/.powervs/config.json --vault-password-file password_file; fi"
230234
]
231235
}
232236

@@ -241,8 +245,12 @@ resource "terraform_data" "execute_playbooks_with_vault" {
241245
# Encrypt ocp config if it already exists
242246
provisioner "remote-exec" {
243247
inline = [
244-
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file; fi",
245-
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file; fi",
248+
"if [ -f \"/root/.powervs/config.json\" ]; then",
249+
" if ! ( head -n 1 | grep -q '^\\$ANSIBLE_VAULT' ); then",
250+
" echo ${var.ansible_vault_password} > password_file",
251+
" ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file",
252+
" fi",
253+
"fi",
246254
"rm -f password_file"
247255
]
248256
}

solutions/standard-openshift/ansible/templates-ansible/deploy-openshift-cluster/ansible_exec.sh.tftpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export IBMCLOUD_API_KEY=$${IBMCLOUD_API_KEY}
2424
unbuffer ansible-playbook -i $${ansible_inventory} $${ansible_playbook} --extra-vars "IBMCLOUD_API_KEY=$IBMCLOUD_API_KEY"
2525
## On failure:
2626
if [ $? -ne 0 ]; then
27-
if [ -f "~/.powervs/config.json" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file
27+
if [ -f "/root/.powervs/config.json" ]; then ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file; fi
2828
rm -f password_file
2929
rm -rf $${ansible_private_key_file}
3030
exit 1
3131
fi
3232
echo \"Playbook command successful\"
3333
rm -rf $${ansible_private_key_file}
34-
if [ -f "~/.powervs/config.json" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file
34+
if [ -f "/root/.powervs/config.json" ]; then ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file; fi
3535
rm -f password_file

solutions/standard-openshift/ansible/templates-ansible/deploy-openshift-cluster/ansible_exec_vault.sh.tftpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export IBMCLOUD_API_KEY=$${IBMCLOUD_API_KEY}
2424
unbuffer ansible-playbook -i $${ansible_inventory} $${ansible_playbook} --extra-vars "IBMCLOUD_API_KEY=$IBMCLOUD_API_KEY" --vault-password-file password_file
2525
## On failure:
2626
if [ $? -ne 0 ]; then
27-
if [ -f "~/.powervs/config.json" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file
27+
if [ -f "/root/.powervs/config.json" ]; then ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file; fi
2828
rm -f password_file
2929
rm -rf $${ansible_private_key_file}
3030
exit 1
3131
fi
3232
echo \"Playbook command successful\"
3333
rm -rf $${ansible_private_key_file}
34-
if [ -f "~/.powervs/config.json" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file
34+
if [ -f "/root/.powervs/config.json" ]; then ansible-vault encrypt /root/.powervs/config.json --vault-password-file password_file; fi
3535
rm -f password_file

0 commit comments

Comments
 (0)