简体   繁体   English

通过groovy文件将带有空格的Jenkins Environment变量传递给ansible命令

[英]Passing Jenkins Environment variable with spaces to ansible command ran via groovy file

I am unable to pass a variable with spaces from a Jenkinsfile to an ansible command ran via groovy. 我无法将带有空格的变量从Jenkinsfile传递给通过groovy运行的ansible命令。

I have tried using quotes (double and single) backspaces and various other combinations to no avail. 我尝试使用引号(双精度和单精度)退格键和各种其他组合都无济于事。 Unfortunately I cannot rename to include - or _. 不幸的是,我无法重命名以包含-或_。 Running it as a one liner in linux works with the variable exported to the shell with double quotes. 在Linux中将其作为一个衬套运行时,会将变量导出到带有双引号的shell中。 When passed from Jenkins the first space breaks the code and the second word in the string is treated like a seperate string. 从Jenkins传递时,第一个空格中断代码,字符串中的第二个单词被视为一个单独的字符串。

The variable in question in the Jenkins file... Jenkins文件中有问题的变量...

env.COMPUTER_NAME = "HELLO WORLD"

In the ansible 在Ansible中

String computer_args ="ansible-playbook deploy.yml -e COMPUTER_NAME=${env.COMPUTER_NAME}"

This would result in the error... 这将导致错误...

Kernel \r on an \m

ERROR! the playbook: WORLD could not be found

Connection to XX closed.

script returned exit code 1

However, running in a linux shell, it works 但是,在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"
}

Here it is, try this one, 在这里,尝试这个,

/* 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 Playbook 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 }}"

Output 输出量

[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   

As you can see, the COMPUTER_NAME is successfully passed to the ansible script. 如您所见,COMPUTER_NAME已成功传递到ansible脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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