我有一个从S3下载文件列表的攻略,这个列表可以通过使用with_items和|default([])动态设置。
拉出此命令后,我需要获取存储文件的目的地列表,并执行其他操作。我注册了var output,并看到调用module_args具有值"Dest“,这就是我想要访问的内容。
我试过这样的东西:
debug: msg="{{ item }}"
with_items: "{{ output.results }}或者甚至访问output.invocation但得到未定义的变量
任务:
- name: "Download Apps from S3"
aws_s3:
bucket: "{{ resource_bucket_name }}"
object: "{{ s3_apps[item].src }}"
dest: "{{ s3_apps[item].dest }}"
mode: get
with_items: "{{ s3_apps_decl |default([]) }}"
register: output我的带debug的输出变量:
"msg": [
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"bucket": "bucket-resources",
"debug_botocore_endpoint_logs": false,
"dest": "/tmp/test_app.tgz",
"dualstack": false,
"ec2_url": null,
"encrypt": true,
"encryption_kms_key_id": null,
"encryption_mode": "AES256",
"expiry": 600,
"headers": null,
"ignore_nonexistent_bucket": false,
"marker": "",
"max_keys": 1000,
"metadata": null,
"mode": "get",
"object": "test_app.tgz",
"overwrite": "always",
"permission": [
"private"
],
"prefix": "",
"profile": null,
"region": null,
"retries": 0,
"rgw": false,
"s3_url": null,
"security_token": null,
"src": null,
"validate_certs": true,
"version": null
}
},
"item": "test_app",
"msg": "GET operation complete"
},
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"bucket": "bucket-resources",
"debug_botocore_endpoint_logs": false,
"dest": "/tmp/testanotherapp.spl",
"dualstack": false,
"ec2_url": null,
"encrypt": true,
"encryption_kms_key_id": null,
"encryption_mode": "AES256",
"expiry": 600,
"headers": null,
"ignore_nonexistent_bucket": false,
"marker": "",
"max_keys": 1000,
"metadata": null,
"mode": "get",
"object": "testanotherapp.spl",
"overwrite": "always",
"permission": [
"private"
],
"prefix": "",
"profile": null,
"region": null,
"retries": 0,
"rgw": false,
"s3_url": null,
"security_token": null,
"src": null,
"validate_certs": true,
"version": null
}
},
"item": "testanotherapp",
"msg": "GET operation complete"
}
]
}我期望的输出是定义一个变量,该变量输出:['/tmp/testanotherapp.spl'.'/tmp/test_app.tgz']
我已经用与上面任务类似的语法尝试了set_fact,但是它只保存最后一个值...
发布于 2020-05-21 16:20:37
你在output中所期望的就是你所得到的,因为你正在接收你是registering in a loop的return values from an aws_s3 module call。
现在,如果您只想获取已保存在目标主机上的所有dest的路径列表,则必须从数据结构中执行extract the corresponding attribute。
- name: Show a list of dest paths from previous run
debug:
msg: "{{ output.results | map(attribute='invocation.module_args.dest') | list }}"https://stackoverflow.com/questions/61927366
复制相似问题