【问题标题】:How to set a value in register with set_facts in ansible?如何在ansible中使用set_facts在寄存器中设置一个值?
【发布时间】:2018-01-23 10:44:38
【问题描述】:

当一个任务根据条件被跳过并且寄存器的结果也不同时,另一个任务失败的原因是什么。

- name: Check if the partition is already mounted
  shell: df | grep "{{item.partition}}" | wc -l
  with_items:
    - "{{ ebs_vol }}"
  register: ebs_checked
  when: ebs_vol is defined

- name: Make filesystem of the partition
  filesystem: fstype=ext4 dev={{item.item.partition}} force=no
  when:  ( ebs_vol is defined and "{{item.stdout}} == True" )
  changed_when: True
  with_items:
    - ebs_checked.results

【问题讨论】:

    标签: ansible


    【解决方案1】:

    使用default 过滤器处理极端情况:

    - name: Make filesystem of the partition
      filesystem: fstype=ext4 dev={{item.item.partition}} force=no
      when:  item.stdout | bool
      changed_when: True
      with_items: "{{ ebs_checked.results | default([]) }}"
    

    如果ebs_checked 中没有results,这将遍历空列表(读作“什么都不做”)。

    此外,您不应该检查ebs_vol is defined,因为循环任务中的when 语句是在循环内应用的,并且请记住,您在上一个任务中检查ebs_vol is defined,因此在循环内不需要进行此检查。

    【讨论】:

    • 在上述解决方案中 - 时:item.stdout | bool 是 bool 我们需要使用的数据类型或变量吗?
    • 这是一个过滤器,将字符串转换为适合when 条件的布尔类型。 docs.ansible.com/ansible/latest/playbooks_filters.html
    • 它给出了语法错误。 dict 对象没有属性 stdout。
    • 查看ebs_checked.results中的内容
    • 当我没有定义 ebs_vol 并且输出低于结果时,我被卡住了,因为结果中没有标准输出。调试 msg={{ ebs_checked.results}}] ************************ ok: [10.254.18.117] => { "msg": " [{你跳过':“}
    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2018-02-24
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    相关资源
    最近更新 更多