- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Ansible 代码工作正常,并且当传递的文件名没有空格时能够逐行读取。
问题是当 Jenkins 在 source_file
参数中传递包含空格的文件名时,如下所示。
以下是 ansible 的调用方式:
sh "ansible-playbook -i /web/playbooks/filecopy/allmwhosts.hosts /web/playbooks/filecopy/copyfiles.yml -e source_host=$source_host -e '{ source_file: $source_file }'
[Pipeline] sh (hide)
+ ansible-playbook -i /web/filecopy/allmwhosts.hosts /web/playbooks/filecopy/copyfiles.yml -e source_host=usurb31as30v -e '{ source_file: /web/lib/hello.txt
/web/lib/Policy Form_LifeAgents_new.pdf
/web/lib/Policy Form_LifeAgents_old.pdf }'
下面的 Ansible 代码:
- debug:
msg: "FILES: {{ files_list }}"
- debug:
msg: "The NEWLINE files are {{ item }}"
with_items:
- "{{ source_file.split() }}"
- debug:
msg: "The NEWLINE1 files are {{ item }}"
with_items:
- "{{ source_file.splitlines() }}"
- set_fact:
source_file_new: "{{ item | quote + '\n' + source_file | default() }}"
with_items:
- "{{ source_file.split('\n') }}"
- debug:
msg: "AFTER QUOTE: {{ source_file_new }}"
- debug:
msg: "QUOTED NEWLINE files are {{ item }}"
with_items:
- "{{ source_file_new.split('\n') }}"
输出:
TASK [debug] *******************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:18
Saturday 20 August 2022 22:59:50 -0500 (0:00:00.077) 0:00:00.118 *******
ok: [localhost] => {
"msg": "FILES: /web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf"
}
TASK [debug] *******************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:21
Saturday 20 August 2022 22:59:50 -0500 (0:00:00.032) 0:00:00.150 *******
ok: [localhost] => (item=/web/lib/hello.txt) => {
"msg": "The NEWLINE files are /web/lib/hello.txt"
}
ok: [localhost] => (item=/web/lib/Policy) => {
"msg": "The NEWLINE files are /web/lib/Policy"
}
ok: [localhost] => (item=Form_LifeAgents_new.pdf) => {
"msg": "The NEWLINE files are Form_LifeAgents_new.pdf"
}
ok: [localhost] => (item=/web/lib/Policy) => {
"msg": "The NEWLINE files are /web/lib/Policy"
}
ok: [localhost] => (item=Form_LifeAgents_old.pdf) => {
"msg": "The NEWLINE files are Form_LifeAgents_old.pdf"
}
TASK [debug] *******************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:21
Sunday 21 August 2022 12:16:24 -0500 (0:00:00.021) 0:00:00.101 *********
ok: [localhost] => (item=/web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf /web/lib/Policy Specimen_MLA_Everest_new.pdf /web/lib/Policy Specimen_MLA_Everest_old.pdf) => {
"msg": "The NEWLINE1 files are /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf /web/lib/Policy Specimen_MLA_Everest_new.pdf /web/lib/Policy Specimen_MLA_Everest_old.pdf"
}
TASK [set_fact] ****************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:26
Saturday 20 August 2022 22:59:50 -0500 (0:00:00.069) 0:00:00.219 *******
ok: [localhost] => (item=/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf) => {
"ansible_facts": {
"source_file_new": "'/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf'\n/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf"
},
"ansible_loop_var": "item",
"changed": false,
"item": "/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf"
}
TASK [debug] *******************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:31
Saturday 20 August 2022 22:59:50 -0500 (0:00:00.080) 0:00:00.300 *******
ok: [localhost] => {
"msg": "AFTER QUOTE: '/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf'\n/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf"
}
TASK [debug] *******************************************************************
task path: /web/playbooks/filecopy/copyfiles.yml:35
Saturday 20 August 2022 22:59:50 -0500 (0:00:00.031) 0:00:00.331 *******
ok: [localhost] => (item='/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf') => {
"msg": "QUOTED NEWLINE files are '/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf'"
}
ok: [localhost] => (item=/web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf) => {
"msg": "QUOTED NEWLINE files are /web/lib/hello.txt /web/lib/Policy Form_LifeAgents_new.pdf /web/lib/Policy Form_LifeAgents_old.pdf"
}
预期 ansible 应该读取三行,但是 split() 或 split('\n') 都没有帮助
最佳答案
您正在通过 shell 启动 Ansible playbook。这里的问题是一个引用问题。 (ba)sh 将新行和空格解释为参数分隔符。因此,您需要正确引用所有内容才能:
-e
参数在列表中的最后一个文件之后结束一旦解决了这个问题,其余的处理就变得微不足道了,因为您只需 source_file.splitlines()
您的值即可获取单个文件的列表。
请注意,Jenkins 有一个 Ansible 插件,可以在这种情况下简化您在 gui 上的生活。如果您添加“调用 ansible playbook”构建步骤并单击“高级”,则只需输入键名称和值(可以是变量)即可添加额外的变量。启 Action 业时,您可以在结果中看到正确的安全引用。
不幸的是,这个方便的帮助在管道语法帮助器中不可用,您还必须自己构建额外参数的字符串。
长话短说:这就是您的 shell 脚本命令应该如何编写,以安全地引用所有 ansible 参数:
ansible-playbook -i /web/playbooks/filecopy/allmwhosts.hosts \
/web/playbooks/filecopy/copyfiles.yml \
-e "source_host='$source_host'" \
-e "source_file='$source_file'"
作为概念证明,这里是一个一体化演示声明式 Jenkins 管道,您可以将其粘贴到管道作业中,并通过 shell 和 Ansible 插件成功运行声明的 playbook。
请注意,由于我在管道内声明了作业参数,因此第一次构建将系统性地失败。您只能在第二次构建时以交互方式提供参数。默认值应该足以重现您问题中描述的情况。
pipeline {
agent any
stages {
stage('Setup parameters') {
steps {
script {
properties([
parameters([
text(
defaultValue: "/a/file\n/an/other file\n/one/more/file",
description: "Enter absolute path of files on target one on each line",
name: "source_file"
)
])
])
}
}
}
stage('Create playbook') {
steps {
sh '''
cat <<"EOF" > playbook.yml
---
- hosts: localhost
gather_facts: false
vars:
file_list: "{{ source_file.splitlines() }}"
tasks:
- name: Show content of processed incoming extra var
debug:
var: file_list
- name: Just proove it works
debug:
msg: "received individual file {{ item }}"
loop: "{{ file_list }}"
EOF
'''
}
}
stage('Run playbook through shell'){
steps {
sh '''
ansible-playbook playbook.yml -e "source_file='$source_file'"
'''
}
}
stage('Run playbook through ansible plugin'){
steps {
ansiblePlaybook extras: '-e "source_file=\'$source_file\'"', playbook: 'playbook.yml'
}
}
}
}
这给出了(第二次运行并使用默认值):
Started by user Olivier Clavel
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/testpipe
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Setup parameters)
[Pipeline] script
[Pipeline] {
[Pipeline] properties
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Create playbook)
[Pipeline] sh
+ cat
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run playbook through shell)
[Pipeline] sh
+ ansible-playbook playbook.yml -e source_file='/a/file
/an/other file
/one/more/file'
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Show content of processed incoming extra var] ****************************
ok: [localhost] => {
"file_list": [
"/a/file",
"/an/other file",
"/one/more/file"
]
}
TASK [Just proove it works] ****************************************************
ok: [localhost] => (item=/a/file) => {
"msg": "received individual file /a/file"
}
ok: [localhost] => (item=/an/other file) => {
"msg": "received individual file /an/other file"
}
ok: [localhost] => (item=/one/more/file) => {
"msg": "received individual file /one/more/file"
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run playbook through ansible plugin)
[Pipeline] ansiblePlaybook
[testpipe] $ ansible-playbook playbook.yml -e "source_file='/a/file
/an/other file
/one/more/file'"
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Show content of processed incoming extra var] ****************************
ok: [localhost] => {
"file_list": [
"/a/file",
"/an/other file",
"/one/more/file"
]
}
TASK [Just proove it works] ****************************************************
ok: [localhost] => (item=/a/file) => {
"msg": "received individual file /a/file"
}
ok: [localhost] => (item=/an/other file) => {
"msg": "received individual file /an/other file"
}
ok: [localhost] => (item=/one/more/file) => {
"msg": "received individual file /one/more/file"
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
关于jenkins - Ansible 无法读取 Jenkins 多行参数传递的带有空格的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73431604/
尽管用户是 sudo 用户,但远程主机在运行 Ansible playbook 时仍会引发错误。 "/usr/bin/python: can't open file '/home/ludd/.ansi
只是想了解 ansible 和 ansible-galaxy 之间的核心区别是什么?从文档中,ansible-galaxy 用于管理角色(创建和发布)那么ansible是做什么用的呢?只运行剧本? 我
只是想了解 ansible 和 ansible-galaxy 之间的核心区别是什么?从文档中,ansible-galaxy 用于管理角色(创建和发布)那么ansible是做什么用的呢?只运行剧本? 我
我有一个定义如下的剧本: - name: install percona rpms hosts: imdp roles: - role1 - role2 - role3
Ansible 版本:2.4.2.0 来自 here和 here ,我可以写下面的剧本 $ cat test.yml - name: Finding Master VMs hosts: all-c
我正在运行这个 Ansible 剧本: - name: Set String set_fact: string: item loop: "{{some_var|filter()}}"
我无法在我的输出变量中搜索我用于 when 语句的指定字符串。下面的代码应该检查输出变量中的字符串“distribute-list”,但是在运行 playbook 时会出现错误。 fatal: [19
在 Ansible 中是否可以仅当处理程序出现在该播放中时才有条件地通知处理程序? 例如:我有一个多主机配置的剧本。有些主机使用 Apache 运行 PHP,有些使用 PHP-FPM。根据主机,修改
我是 ansible 的新手,正在学习 ansible 并致力于定义 ansible 脚本以在服务器上安装 confluent。在查看有关如何定义和运行 ansible 脚本的示例时,一些示例使用“a
是否可以列出Ansible角色中的任务? 即除了grepping - name以外,还有没有现成的方法可以做到这一点? 最佳答案 创建一个指定角色的简单剧本,并使用ansible-playbook选项
所以ansible-playbook有 --ask-pass和 --ask-sudo-pass .有没有办法让 ssh 先尝试没有密码,然后只有在无密码登录失败时才提示输入密码?同样,ansible
Ansible 提供了许多过滤器和条件。据我所知;应该可以实现一个 Ansible playbook 来执行一组任务,这些任务实现与图灵完备语言相同的结果。那么,图灵完备吗? 最佳答案 我觉得是这样的
我一直在寻找,但没有太多关于什么 Ansible 保险库密码文件应该是什么样的。 例如我想做: ANSIBLE_VAULT_PASSWORD_FILE=./pwdfile ansible-vault
我在运行完整的剧本时遇到了麻烦,因为后来的剧本所依赖的一些事实在早期的剧本中被修改了,但是 ansible 不会在运行中更新事实。 运行ansible somehost -m setup当整个剧本开始
我对Ansible很陌生 是否可以使用Ansible检查文件中是否存在字符串。 我要检查的是用户有权访问服务器。 这可以使用cat /etc/passwd | grep username在服务器上完成
我正在尝试使用 jinja2 通过 ansible 模板循环字典以创建多个数据源,但收到此错误 [{'msg': "AnsibleUndefinedVariable: One or more unde
我正在尝试将文件复制到/ etc。但是当我运行剧本时,我收到“msg:目标/ etc不可写”的消息。这是我的Playbook任务部分。非常感谢您的帮助。 任务: - name: copy rsyslo
我正面临这个恼人的错误:Ansible hosts are randomly unreachable #18188 . 有没有办法告诉 Ansible 如果 SSH 连接失败,再试一次?还是2倍? 根
我正在编写一个简单的任务来创建用户。作为此任务的一部分,我想从defaults / main.yml中读取密码。 默认值/ main.yml test_user:testuser test_group
当我在Ansible中使用lineinfile时,它不会写',"字符 lineinfile: 'dest=/home/xyz state=present line="CACHES="default""
我是一名优秀的程序员,十分优秀!