0

I have a problem with passing parameters to the Ansible:

Bash script:

#!/usr/bin/env bash

set -e +x

# Variables
LINUX_HOST="ubuntu"
INSTALL_ANSIBLE_TARGET_PATH="/tmp/ansible-inst"
ANSIBLE_CONFIG_DIR="inventory"
ANSIBLE_CONFIG_FILENAME="hosts.yml"
ANSIBLE_PLAYBOOKS_DIR="playbooks"
ANSIBLE_PREPARE_PLAYBOOK_FILENAME="linux-prepare-deploy.yml"
ANSIBLE_POSTGRES_PLAYBOOK_FILENAME="postgresql-server-install.yml"
ANSIBLE_POSTGRES_PLAYBOOK_PARAMETER=" -e \"POSTGRES_cluster_name=ansiblecluster\""


# Utils
run_playbook() {
  LOG_MSG="Running playbook ${1}"
  if [ "${2}" != "" ]; then
    LOG_MSG="${LOG_MSG} with additional parameters ${2}"
  fi

  ANSIBLE_CONFIG_FILE="${INSTALL_ANSIBLE_TARGET_PATH}/${ANSIBLE_CONFIG_DIR}/${ANSIBLE_CONFIG_FILENAME}"
  log_i "${LOG_MSG} using main config ${ANSIBLE_CONFIG_FILE} file."

  sudo ansible-playbook -i "${ANSIBLE_CONFIG_FILE}" -l "${LINUX_HOST}" "${1}" "${2}"

  log_i "Ansible playbook ${1} execution was finished."
}

# Main functions
_ansible_run() {
  cd "${INSTALL_ANSIBLE_TARGET_PATH}"

  PLAYBOOKS_DIRECTORY="${INSTALL_ANSIBLE_TARGET_PATH}/${ANSIBLE_PLAYBOOKS_DIR}"

  run_playbook "${PLAYBOOKS_DIRECTORY}/${ANSIBLE_PREPARE_PLAYBOOK_FILENAME}" ""
  run_playbook "${PLAYBOOKS_DIRECTORY}/${ANSIBLE_POSTGRES_PLAYBOOK_FILENAME}" "${ANSIBLE_POSTGRES_PLAYBOOK_PARAMETER}"
}

But after execute _ansible_run I got this output:

[ANSIBLE] 2023-07-06 10:54:04 INFO : Running playbook /tmp/ansible-inst/playbooks/prepare-deploy.yml using main config /tmp/ansible-inst/inventory/hosts.yml file.
ERROR! the playbook:  could not be found

It looks like the parameters are not passed, but I don't understand how to fix this. Can you help me please?

Michael Linker
  • 118
  • 3
  • 8
  • 1
    Not related to your problem, but [`set -e` is generally considered a bad idea](https://mywiki.wooledge.org/BashFAQ/105#Exercises); it makes accurate desk-checking much harder, by making error-handling behavior highly context-dependent (whether `set -e` triggers when an error happens in a function can't be analyzed just by reading the text of that function, f/e; one has to look at every context that function is ever called in to be sure). – Charles Duffy Jul 06 '23 at 15:41
  • 3
    And all-caps variable names are used for variables meaningful to the OS and shell, while lowercase names are reserved for applications like your script and guaranteed not to cause unexpected changes in shell behavior; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a regular shell variable modifies any preexisting, like-named environment variable. – Charles Duffy Jul 06 '23 at 15:44
  • 1
    As for debugging your script, though -- I'd recommend running it with `set -x` enabled, finding the individual line that fails, and looking at whether the line is calling ansible with the arguments you expect and intend. If the arguments are correct and the behavior is wrong, you can factor out the bash script and be asking a question _only_ about ansible itself; if the arguments are wrong, you can factor out ansible and ask a question _only_ about bash-the-language. – Charles Duffy Jul 06 '23 at 15:45
  • 1
    The way you're using `ANSIBLE_POSTGRES_PLAYBOOK_PARAMETER` is definitely buggy. See [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy Jul 06 '23 at 15:47
  • (why would you start a script with `set +x`? That doesn't change anything about default behavior, but means you can't use `bash -x yourscript` to run it with tracing enabled). – Charles Duffy Jul 06 '23 at 16:21
  • @CharlesDuffy Hello! Thanks for the tips! I just refactored the code according to your tips. Actually this bash code is executed via Make, so tracing with `+x` has been disabled, but of course I turn it on when I need to debug. – Michael Linker Jul 06 '23 at 16:27
  • 1
    BTW, I advised against using `set -e`, but didn't say what to do instead: On a command that should cause the whole script to abort if it fails, append `|| exit`; on a command that should cause the current function to fail if it fails but _not_ the whole script, append `|| return`. (http://shellcheck.net/ will warn if not doing one or the other of those things for `cd`) – Charles Duffy Jul 06 '23 at 16:28

0 Answers0