我正在尝试通过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,使用:605693959,uisuid:False,uisreg: 正确,upw_name:uroot,ugid:0,uischr:False,uwusr:True,uxoth: False,uislnk:False,取消链接: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}]“”], “ delta”:“ 0:00:00.026909”,“ end”:“ 2018-10-30 06:48:00.362616”,“ msg”: “非零返回码”,“ rc”:94,“开始”:“ 2018-10-30 06:48:00.335707“,” stderr“:”错误:DeployError:无法安装, 配置并启动服务:无法解压气隙工件: 无法打开安装捆绑文件/ 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,取消链接: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}]:打开/ 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,取消链接: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}]:文件名太长”, “ stderr_lines”:[“错误:DeployError:无法安装,配置和 启动服务:无法打开气隙工件的包装:无法打开 安装捆绑文件/ home / ec2-user / [{uuid:0,uwoth:False,umtime: 1540882069.3233995,uinode:82,uisgid:False,使用:605693959,uisuid:False,uisreg:True,upw_name:uroot,ugid:0,uischr:False, uwusr:对,uxoth:对,uislnk:对,取消链接:1,uissock:对, urgrp:正确,ugr_name:uroot,健康: 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}]:打开/ 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,取消链接: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}]:文件名太长“],” stdout“: “ \ n正在安装工件”,“ stdout_lines”:[“”,“正在安装工件”]}
如果我使用:
- 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:”,“ 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:”,“ 1540883969.7361338”,“ uisdir:”, “ False”,“ uctime:”,“ 1540883975.674141”,“ uisblk:”,“ False”, “ uwgrp:”,“ False”,“ uxgrp:”,“ False”,“ udev:”,“ 64768”,“ uroth:”, “ True”,“ uisfifo:”,“ False”,“ umode:”,“ u0644”,“ urusr:”, “ True}]”]“,” delta“:” 0:00:00.014548“,” end“:” 2018-10-30 07:19:41.288141“,” msg“:”非零返回码“,” rc“:113,”开始“: “ 2018-10-30 07:19:41.273593”,“ stderr”:“错误:UnknownError:接受 在0和1 arg之间,收到62“,” stderr_lines“:[”错误: UnknownError:接受0到1之间的arg,已收到62“],” stdout“: “”,“ stdout_lines”:[]}重试,请使用:--limit @ / home / ec2-user / ANSIBLE / clusterOps / roles / chef-automatev2 / defaults / main.retry
能够获取字典项:
- 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}}"
答案 0 :(得分:1)
如果您要阅读错误消息,您会发现any.default()
不包含文件名列表,而是包含python对象列表,可能是调用string.replace
的结果>
file_automate.files
如果您只想要文件名列表,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/
中该对象的完全限定。