首页 > 解决方案 > 如何使用 ansible playbook 将变量放入 bash 脚本中

问题描述

我需要一个带有 Ansible 剧本的脚本中的 put 变量。我从 Jenkins 'build with parameter' 获取变量,但我不能在我的脚本中放置一个“tag”变量。我怎样才能做到这一点?

这是我的脚本:

export JAVA_HOME=/home/asd/products/app/java8

tag=$(tag)

hzpid=$(ps aux  |grep /home/asd/products/app/hzcluster/ |grep -v grep |  awk '{print $2}' | sed -r 's/%/ /g')

echo $hzpid

if [[ -z $hzpid ]]; then

echo "no running applications"

else

kill -9 $hzpid &&  echo "running process terminated."

fi


nohup $JAVA_HOME/bin/java -Dspring.profiles.active=dxl-dev -jar /home/asd/products/app/hzcluster/new/hzcluster-$tag-SNAPSHOT.jar > $LOG_DIR/hzcluster.log &

exit 0 

这是我的 ansible 剧本:

- hosts: '{{ hosts }}'
  gather_facts: no
  tasks:
    - name: Run new hzcluster.jar
      shell: sh  '{{ sh_file }}'start-hzcluster2.sh '{{ tag }}'

标签: bashshelljenkinsgroovyansible

解决方案


在您的 shell 脚本中从 更改tag=$(tag)tag=$1

剧本:

- hosts: '{{ hosts }}'
  gather_facts: no
  tasks:
    - name: Run new hzcluster.jar
      shell: sh  '{{ sh_file }}'start-hzcluster2.sh '{{ tag }}'

推荐阅读