ansible - 使用 Ansible 命令模块时文件名太长
问题描述
我正在尝试使用带有ansible的厨师栖息地安装厨师自动化。
这是 main.yml 的相关部分
- name: get aib filename
find:
paths: /home/ec2-user
file_type: file
use_regex: yes
patterns:
- '^automate.*'
register: file_automate
- name: deploy chef-automatev2
command: "sudo chef-automate deploy '/home/ec2-user/config.toml' --accept-terms-and-mlsa --skip-preflight --airgap-bundle '/home/ec2-user/{{ file_automate.files }}'"
但出现错误:
任务 [部署 chef-automatev2] ******************************************* ************************************致命:[10.1.1.2]:失败!=> {“更改”:true,“cmd”:[“sudo”,“chef-automate”,“deploy”,“/home/ec2-user/config.toml”,“--accept-terms-and- mlsa", "--skip-preflight", "--airgap-bundle", "/home/ec2-user/[{uuid: 0, uwoth: False, umtime: 1540882069.3233995, uinode: 82, uisgid: False, usize : 605693959, uisuid: False, uisreg: True, upw_name: uroot, ugid: 0, uischr: False, uwusr: True, uxoth: False, uislnk: False, unlink: 1, uissock: False, urgrp: True, ugr_name: uroot , upath: u/home/ec2-user/automate-20181020020209.aib, uxusr: False, uatime: 1540882068.8493989, uisdir: False, uctime: 1540882074.7854095, uisblk: False, uwgrp: False,
如果我使用:
- name: deploy chef-automatev2
command: "chef-automate deploy config.toml --accept-terms-and-mlsa --skip-preflight --airgap-bundle {{ file_automate.files }}"
我得到错误:
任务 [部署 chef-automatev2] ******************************************* ************************************ 致命:[10.1.1.20]:失败!=> {“更改”:true,“cmd”:[“chef-automate”,“deploy”,“config.toml”,“--accept-terms-and-mlsa”,“--skip-preflight”, “--airgap-bundle”、“[{uuid:”、“0”、“uwoth:”、“False”、“umtime:”、“1540883970.2111344”、“uinode:”、“83”、 “uisgid:”、“假”、“usize:”、“605693959”、“uisuid:”、“假”、“uisreg:”、“真”、“upw_name:”、“uroot”、 “ugid:”、“0”、“uischr:”、“假”、“uwusr:”、“真”
能够获得字典项目:
- hosts: chefclusterautomatev2
remote_user: ec2-user
become: yes
become_method: sudo
connection: ssh
gather_facts: true
tasks:
- name: get aib filename
find:
paths: /home/ec2-user
register: res
- name: deploy chef-automatev2
debug:
msg: printing aib filename
with_items: "{{ res.files | map(attribute='path') | first}}"
感谢马修, 我参加了剧本运行:
TASK [deploy chef-automatev2] *************************************************************************
ok: [1.1.1.17] => (item=/home/ec2-user/automate-20181019225406.aib) => {
"msg": "printing aib filename"
}
现在的问题是如何在“命令”或“外壳”ansible 模块中使用这个对象
以下是我如何解决在 shell 命令中使用对象的问题:
- hosts: localhost
remote_user: a3x52zz
connection: ssh
gather_facts: true
tasks:
- name: get aib filename
find:
paths: /Users/a3x52zz/ansible-test
file_type: "file"
patterns: "automate*.aib"
register: files_matched
- debug:
msg: "{{files_matched.files[1].path}}"
- name: cat filename
command: cat "{{files_matched.files[1].path}}"
解决方案
如果您要阅读错误消息,您会看到它file_automate.files
不包含文件名列表,而是包含 python 对象列表,可能是调用的结果stat:
"cmd": [
"sudo",
"chef-automate",
"deploy",
"/home/ec2-user/config.toml",
"--accept-terms-and-mlsa",
"--skip-preflight",
"--airgap-bundle",
"/home/ec2-user/[{uuid: 0, uwoth: False, umtime: 1540882069.3233995, uinode: 82, uisgid: False, usize: 605693959, uisuid: False, uisreg: True, upw_name: uroot, ugid: 0, uischr: False, uwusr: True, uxoth: False, uislnk: False, unlink: 1, uissock: False, urgrp: True, ugr_name: uroot, upath: u/home/ec2-user/automate-20181020020209.aib, uxusr: False, uatime: 1540882068.8493989, uisdir: False, uctime: 1540882074.7854095, uisblk: False, uwgrp: False, uxgrp: False, udev: 64768, uroth: True, uisfifo: False, umode: u0644, urusr: True}]"
],
如果您只需要文件名列表,{{ file_automate.files | map(attribute="path") | list }}
则应将其提供给您,尽管您希望省略前导/home/ec2-user/
,因为正如您所看到的,path
该对象的字段files
已经完全限定。
推荐阅读
- python - Python 生成器和 yield - 在 while 循环中递增到 next() 调用
- sql - 使用 LINQ 和 List 作为参数调用存储过程
- python - 哪个函数可以在 python 中使用 INDEX+MATCH(excel) 进一步计算
- java - 如何在保持事件运行的同时运行循环?
- reactjs - 如何使用 WordPress 处理 UI 和 ReactJS 处理前端逻辑来制作网站
- python - 使用词向量嵌入后如何提高 LSTM 训练的准确性?
- gnu-parallel - GNU Parallel 更频繁地检查节点列表
- javascript - 引导切换按钮错误-“无法在'文档'上执行'querySelector':'/navbarSupportedContent'不是有效的选择器。”
- c# - C#聚合函数定义重载#1解释
- python - 如何使用 Plotly 将两个图组合成一个图形?