如何检查/etc/yum.repos.d中是否存在某些存储库

时间:2020-04-20 12:53:42

标签: ansible repository

我已经写过这本剧本,目的是检查/etc/yum.repos.d/中是否存在某些存储库,但是我不确定它是否正确。正确吗?

- name: Check that the repos exists
  stat:
          path: /etc/yum.repos.d/{{ item }}
          with_items:
            - "rhel-mc.repo"
            - "epel-mc.repo"
            - "redhat.repo"
          register: stat_result
  debug:
            msg: "Repo file exists..."
            when: stat_result.stat.exists
  debug:
            msg: "Repo file not found"
            when: stat_result.stat.exists == False

1 个答案:

答案 0 :(得分:0)

这几乎是完美的!

  • 确保只有模块选项是在模块内部对齐的,其他所有内容都应在模块外部。这些包括:
    • with_items
    • 何时
    • 注册
  • 您需要确保将{item}用单引号引起来,在此处将整个路径都可以使用。
  • 您忘记了调试的破折号。
  • 最后,如果您将True或False进行比较,则ansible棉绒将失败,因此,在条件之前,我会
    - name: Check that the repos exists
      stat:
        path: '/etc/yum.repos.d/{{ item }}'
      with_items:
        - "rhel-mc.repo"
        - "epel-mc.repo"
        - "redhat.repo"
      register: stat_result

    - debug:
        msg: "Repo file exists..."
      when: stat_result.stat.exists

    - debug:
        msg: "Repo file not found"
      when: not stat_result.stat.exists