【问题标题】:Convert chef cookbook part to ansible将厨师食谱部分转换为 ansible
【发布时间】:2021-04-24 11:48:20
【问题描述】:

你能帮我把这部分厨师食谱转换成ansible吗?

node['my']['domains'].each do |domain|
  execute 'install cert' do
    command "/root/bin/my_install.sh --domains #{domain}"
    not_if { File.exists?("/home/my/#{domain}/fulltext.txt") }
  end
end

我试着玩这个:

- name: Check that the fulltext.txt exists
  stat:
    path: "/home/my/{{ item }}/fulltext.txt"
  loop: "{{ my_domains }}"
  register: stat_result

- name: install cript
  command: "/root/bin/my_install.sh --domains {{ item }}"
  loop: "{{ my_domains }}"
  when: not stat_result.stat.exists

但没有成功。

提前致谢!

【问题讨论】:

  • without success => 你能说得更具体点吗?是什么让结果与您的预期不同?
  • 这里是错误消息:“条件检查'not stat_result.stat.exists'失败。错误是:评估条件时出错(不是stat_result.stat.exists):'str object'有没有属性“统计”
  • 这应该在您的问题的编辑中,而不是在评论中。

标签: ansible chef-infra


【解决方案1】:

您将希望第二个 loop: 遍历 stat_result.results 列表,因为该列表的每个成员都包含两个相关字段:item,包含原始迭代键,然后是您创建的 stat 结构正在期待(看起来像这样,您可以通过- debug: var=stat_result 自己查看)

ok: [localhost] => {
    "stat_result": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": false,
                        "get_mime": true,
                        "path": "/home/my/alpha/fulltext.txt"
                    }
                },
                "item": "alpha",
                "stat": {
                    "exists": false
                }
            },
...

因此:

- name: install cript
  command: "/root/bin/my_install.sh --domains {{ item.item }}"
  loop: "{{ stat_result.results }}"
  when: not item.stat.exists

如果您发现表达式 item.item 令人困惑,您可以通过 loop_control: 将循环变量重命名为 stat_item(或任何对您有意义的名称)

【讨论】:

    【解决方案2】:

    creates 属性使任务具有幂等性。引用

    “如果匹配的文件已经存在,则不会运行此步骤。”

    例如

    - name: install cript
      command:
        cmd: "/root/bin/my_install.sh --domains {{ item }}"
        creates: "/home/my/{{ item }}/fulltext.txt"
      loop: "{{ my_domains }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2021-11-01
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多