我正在运行ansible并尝试使此任务正常工作,但它失败并出现此错误:
任务执行期间发生异常。看到满满的 回溯,使用-vvv。错误是:AttributeError:'list'对象有 没有属性'startswith'致命:[test-1]:失败! => {“failed”:true,“msg”:“模块执行期间出现意外故障。”, “stdout”:“”} msg:模块执行期间意外失败。
代码是:
- name: Register env Type
shell: facter configured_setup
register: setup
- name: transparency tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- name: fetch group_vars
fetch:
src:
- { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
“fetch group_vars”是始终失败的任务,任何想法如何才能起作用?
我想要做的是添加更多的源文件来从不同的设置器中获取。
所以我希望在“src:”下有更多行,说- { filename, when setup.stdout =="something else" }
完整错误是:
任务执行期间发生异常。完整的追溯是: Traceback(最近一次调用最后一次): 运行文件“/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py”,第96行 item_results = self._run_loop(items) 在_run_loop中输入文件“/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py”,第252行 res = self._execute(variables = task_vars) _execute中的文件“/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py”,第446行 result = self._handler.run(task_vars = variables) 运行文件“/usr/lib/python2.7/site-packages/ansible/plugins/action/fetch.py”,第62行 source = self._remote_expand_user(source) 文件“/usr/lib/python2.7/site-packages/ansible/plugins/action/init.py”,第460行,在_remote_expand_user中 如果不是path.startswith('〜'):#FIXME:Windows路径可能以“〜而不是〜”开头 AttributeError:'list'对象没有属性'startswith'
致命:[test-1]:失败! => {“失败”:是的,“msg”: “模块执行期间出现意外故障。”,“stdout”:“”} msg: 模块执行期间意外失败。
答案 0 :(得分:0)
获取,获取文件而不是文件列表的ansible documentation clearly states。虽然可以编写应用程序来处理从YAML文档加载的标量和序列,但这不是自动的,几乎肯定会反映在文档中。
由于你已经有一个更高级别的序列,只需扩展它。
- name: Register env Type
shell: facter configured_setup
register: setup
- name: transparency tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- name: fetch group_vars
fetch:
src: { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
- name: fetch group_vars2
fetch:
src: { filename, when setup.stdout =="something else" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
您可以通过使用YAML的锚点merge
来减少重复性:
- name: Register env Type
shell: facter configured_setup
register: setup
- name: transparency tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- &fetchtask
name: fetch group_vars
fetch: &fetchsrc
src: { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
- <<: *fetchtask
name: fetch group_vars2
fetch:
<<: *fetchsrc
src: { filename, when setup.stdout =="something else" }
Ansible可能会在将文档传递给YAML解析器之前扩展{{...}}
,否则在&#34;透明度任务中的shell
值为#34}。会抛出错误。但你可能仍然可以像对dest
答案 1 :(得分:0)
所以我最终做了这个(这是有效的):
- name: fetch group_vars test
fetch:
src: "{{ item }}"
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
when: setup.stdout == "something" and {{something_else}} == True
我还注意到ansible中可能存在与寄存器相关的错误。 使用“when”语句时,即使不满足条件,寄存器语句也会生效:
- name: test tasks something enabled
shell: /tmp/{{populate_script}} -u -a {{hosts}} -r
register: variable
when: setup.stdout == "test" and {{something}} == True
- name: test tasks something disabled
shell: /tmp/{{populate_script}} -u -a {{hosts}}
register: variable
when: setup.stdout == "test" and {{something}} == False
只有满足其中一个条件,如果满足第一个条件,第二个条件将覆盖“变量”