问题:访问名称在循环中变化的变量
Problem : accessing a variable with name that changes in a loop
这是我剧本的内容:
- 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 中的校验和并将其放入变量中,然后使用调试 (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"
}
我也会尽量简化事情,这里有一些我建议的改变:
- 在slurp任务中,注册一个变量,比如
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" 的文件丢失时检查 slurp_var
,然后在您的后续任务中添加一些 when
子句。
希望对您有所帮助!
这是我剧本的内容:
- 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 中的校验和并将其放入变量中,然后使用调试 (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"
}
我也会尽量简化事情,这里有一些我建议的改变:
- 在slurp任务中,注册一个变量,比如
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" 的文件丢失时检查 slurp_var
,然后在您的后续任务中添加一些 when
子句。
希望对您有所帮助!