将厨师食谱部分转换为 ansible

Convert chef cookbook part to ansible

你能帮我把这部分厨师食谱转换成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

但没有成功。

提前致谢!

您将希望第二个 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(或任何对您有意义的名称)

属性creates使任务幂等。引用

"If a matching file already exists, this step will not be run."

例如

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