简体   繁体   中英

Ansible with multiline variables in shell script

This might seems weird, But I'm trying to create 2 shell scripts, one with variables which I can source, and another, with a function to run.

core.vars

#/bin/bash
# list of playbooks to source

# Core db initialization playbooks

core_db_init_play_books=$(cat <<EOL
../ansible/test.yaml -e "val1=hi val2=by"
../ansible/provision.yml --skip-tags "postgresql-slave,log-es"
../ansible/postgresql-data-update.yml
../ansible/es-mapping.yml --extra-vars "indices_name=all ansible_tag=run_all_index_and_mapping"
../ansible/cassandra-deploy.yml -e "cassandra_jar_path=$ansible_path/ansible cassandra_deploy_path=/home/{{ansible_ssh_user}}" -v
EOL
)

install_script.sh

#!/bin/bash

## Ansible Runner
function ansible_runner() {
    playbooks=$1
    local IFS=$'\n' # seperate playbooks by newline
    for playbook in ${playbooks}; do
        ansible-playbook -i ../ansible/inventory/env ${playbook}
    done
}
source core.vars
ansible_runner core_db_init_play_books

But when you execute the install script, ansible will complain for files with extra-args

playbook not found ../ansible/provision.yml --skip-tags "postgresql-slave,log-es"

Think, it has something to do with the way I pass the file. Couldn't figure it out though. Great minds please ... :)

Would you try the following:

install_script.sh

#!/bin/bash

## Ansible Runner
ansible_runner() {
    while IFS= read -r line; do
        declare -a playbook="($line)"   # split $line into tokens
        ansible-playbook -i ../ansible/inventory/env "${playbook[@]}"
    done <<< "$1"
}
source core.vars
ansible_runner "$core_db_init_play_books"

Now the option lines are properly split into an array of tokens with the declare statement.

The problem is with IFS . You need the spaces when you don't want ../ansible/provision.yml --skip-tags "postgresql-slave,log-es" to be one long filename.
Processing the variable by line and having spaces becomes hard with a for-loop .
Try this:

function ansible_runner() {
    while IFS= read -r playbook; do
        echo ansible-playbook -i ../ansible/inventory/env ${playbook}
    done <<< "$1"
}
ansible_runner "${core_db_init_play_books}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM