这是我的剧本的内容:
- name: Download checksum
get_url:
url: "{{ item.value.url }}.sha1"
dest: "/VAR/{{ ansible_user }}/tmp/{{ item.value.dest }}.sha1"
mode: 0640
timeout: 300
force: yes
with_dict: "{{ packages.list }}"
- name: slurp checksum
slurp:
src: "/VAR/{{ ansible_user }}/tmp/{{ item.value.dest }}.sha1"
register: {{ item.value.dest }}
with_dict: "{{ packages.list }}"
- name: debug
debug:
msg: "sha1:{{ ['item']['value']['dest']['content'] | b64decode }}"
with_dict: "{{ packages.list }}"
在我的变量中,我有:
packages:
list:
package_name1:
url: http://domaine.com/package_name_src1
dest: package_name_dest1
package_name2:
url: http://domaine.com/package_name_src2
dest: package_name_dest2
.....
.....
and so on
我要执行的操作是下载位于http://package_url.sha1中的校验和,并将其放入变量中,然后使用debug(msg)打印。最后的目的是在下载后使用get_url中的checksome变量检查校验和。而如果我想简化事情的主要问题是我必须达到这样的目的
debug:
msg: {{ {{ item.value.dest}}.content }}
我知道Ansible语法有误,也许您可以向我建议解决方法
当前,每次出现以下错误:
fatal: [server]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'value'\n\nThe error appears to have been in 'test.yml': line 54, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: debug\n ^ here\n"
}
答案 0 :(得分:0)
我还将尝试简化操作,这是我建议的一些更改:
slurp_var
。每个循环中的Ansible不会覆盖您的变量,每个循环都将结果添加到slurp_var.results
的列表数组中(以下示例)。with_items
循环。这是修改后的PB以演示结果:
---
- hosts: localhost
gather_facts: false
vars:
packages:
list:
package_name1:
url: http://domaine.com/package_name_src1
dest: file1
package_name2:
url: http://domaine.com/package_name_src2
dest: file2
tasks:
- name: slurp checksum
slurp:
src: "/tmp/{{ item.value.dest }}.sha1"
register: slurp_var
with_dict: "{{ packages.list }}"
- name: debug
debug:
msg: "sha1: {{ item.content | b64decode }}"
with_items: "{{ slurp_var.results }}"
演示运行:
[http_offline@greenhat-29 tests]$ cat /tmp/file1.sha1
c1b016a6506775492a1968a1fa703781c6d066c8
[http_offline@greenhat-29 tests]$ cat /tmp/file2.sha1
6ab7a14cf0e04c09f1a2f0f572e7d67e35fa37bd
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [slurp checksum] **************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}})
ok: [localhost] => (item={'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}})
TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'content': 'YzFiMDE2YTY1MDY3NzU0OTJhMTk2OGExZmE3MDM3ODFjNmQwNjZjOAo=', 'source': '/tmp/file1.sha1', 'encoding': 'base64', 'invocation': {'module_args': {'src': '/tmp/file1.sha1'}}, '_ansible_parsed': True, '_ansible_no_log': False, 'failed': False, 'changed': False, 'item': {'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}}, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': {'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}}}) => {
"msg": "sha1: c1b016a6506775492a1968a1fa703781c6d066c8\n"
}
ok: [localhost] => (item={'content': 'NmFiN2ExNGNmMGUwNGMwOWYxYTJmMGY1NzJlN2Q2N2UzNWZhMzdiZAo=', 'source': '/tmp/file2.sha1', 'encoding': 'base64', 'invocation': {'module_args': {'src': '/tmp/file2.sha1'}}, '_ansible_parsed': True, '_ansible_no_log': False, 'failed': False, 'changed': False, 'item': {'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}}, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': {'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}}}) => {
"msg": "sha1: 6ab7a14cf0e04c09f1a2f0f572e7d67e35fa37bd\n"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
[http_offline@greenhat-29 tests]$
需要引起注意的一件事是,在尝试“抓取”的文件丢失时检查slurp_var
,然后在后续任务中添加一些when
子句。.
希望有帮助!