这是我的剧本的内容:
我也会尝试简化一些事情,以下是我提出的一些改变:
slurp_var
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 你后续任务中的条款..
when
希望能帮助到你!