[英]Passing Jenkins Environment variable with spaces to ansible command ran via groovy file
我無法將帶有空格的變量從Jenkinsfile傳遞給通過groovy運行的ansible命令。
我嘗試使用引號(雙精度和單精度)退格鍵和各種其他組合都無濟於事。 不幸的是,我無法重命名以包含-或_。 在Linux中將其作為一個襯套運行時,會將變量導出到帶有雙引號的shell中。 從Jenkins傳遞時,第一個空格中斷代碼,字符串中的第二個單詞被視為一個單獨的字符串。
Jenkins文件中有問題的變量...
env.COMPUTER_NAME = "HELLO WORLD"
在Ansible中
String computer_args ="ansible-playbook deploy.yml -e COMPUTER_NAME=${env.COMPUTER_NAME}"
這將導致錯誤...
Kernel \r on an \m
ERROR! the playbook: WORLD could not be found
Connection to XX closed.
script returned exit code 1
但是,在linux shell中運行,它可以工作
export COMPUTER_NAME="HELLO WORLD"
ansible-playbook deploy.yml -e COMPUTER_NAME=${env.COMPUTER_NAME}
TASK [computer_name_gather_facts : fail if Computer name was not found] ***************************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
在這里,嘗試這個,
/* Groovy script to run ansible script*/
COMPUTER_NAME="Hello World"
def computer_args=["/bin/bash", "-c", /ansible-playbook -i hosts main.yml -e COMPUTER_NAME="'${COMPUTER_NAME}'"/]
print(computer_args)
def proc = computer_args.execute()
proc.waitFor()
println "Process exit code: ${proc.exitValue()}"
Ansible劇本
---
- hosts: all
gather_facts: no
tasks:
- name: Add a line to a file if the file does not exist, without passing regexp
debug:
msg: "{{ COMPUTER_NAME }}"
輸出量
[root@localhost ~]# groovy check.groovy
[/bin/bash, -c, ansible-playbook -i hosts main.yml -e COMPUTER_NAME="'Hello World'"]Process exit code: 0
Std Err:
Std Out:
PLAY [all] *********************************************************************
Monday 09 September 2019 15:41:36 +0000 (0:00:00.054) 0:00:00.054 ******
TASK [Add a line to a file if the file does not exist, without passing regexp] ***
ok: [192.168.5.206] => {
"msg": "Hello World"
}
PLAY RECAP *********************************************************************
192.168.5.206 : ok=1 changed=0 unreachable=0 failed=0
如您所見,COMPUTER_NAME已成功傳遞到ansible腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.