Ansible寄存器check_path并在with_dict循环中使用它

时间:2017-06-21 15:01:45

标签: ansible

我有以下网站的哈希/字典结构:

sites:   
  example.com:
    site: example.com
    mail: info@example.com
    site_alias: www.example.com
  example.fi:
    site: example.fi
    mail: info@example.fi
    site_alias: example.fi

...

我为每个网站注册一个值,如果还有一个文件夹。打印结果。

name: "Check if path already exists so it is the first time."
  stat: path={{ cert_files }}/{{ item.value.site }}
  register: check_path
  with_dict: "{{ sites }}"
debug: var=check_path.results
# No need to print the whole dictionary, all results are already there. 
# with_dict: "{{ sites }}"

所以我得到了类似的东西:

TASK [letsencrypt : debug]     *****************************************************
ok: [78.47.67.114] => (item={'key': u'example.com', 'value': {u'mail': u'mail@example.com', u'site_alias': u'www.example.com', u'site': u'example.com'}}) => {
"check_path.results": [
    {
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "_ansible_parsed": true, 
        "changed": false, 
        "invocation": {
            "module_args": {
                "checksum_algorithm": "sha1", 
                "follow": false, 
                "get_checksum": true, 
                "get_md5": true, 
                "mime": false, 
                "path": "/etc/letsencrypt/live/example.com"
            }, 
            "module_name": "stat"
        }, 
        "item": {
            "key": "example.com", 
            "value": {
                "mail": "info@example.com", 
                "site": "example.com", 
                "site_alias": "www.example.com"
            }
        }, 
        "stat": {
            "atime": 147869032.3522692, 
            "ctime": 149636484.0226028, 
            "dev": 2049, 
            "executable": true, 
            "exists": true, 
            "gid": 0, 
            "gr_name": "root", 
            "inode": 15725, 
            "isblk": false, 
            "ischr": false, 
            "isdir": true, 
            "isfifo": false, 
            "isgid": false, 
            "islnk": false, 
            "isreg": false, 
            "issock": false, 
            "isuid": false, 
            "mode": "0755", 
            "mtime": 14632684.026028, 
            "nlink": 2, 
            "path": "/etc/letsencrypt/live/example.com", 
            "pw_name": "root", 
            "readable": true, 
            "rgrp": true, 
            "roth": true, 
            "rusr": true, 
            "size": 4096, 
            "uid": 0, 
            "wgrp": false, 
            "woth": false, 
            "writeable": true, 
            "wusr": true, 
            "xgrp": true, 
            "xoth": true, 
            "xusr": true
        }
    }, 
    {
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "_ansible_parsed": true, 
        "changed": false, 
        "invocation": {
            "module_args": {
                "checksum_algorithm": "sha1", 
                "follow": false, 
                "get_checksum": true, 
                "get_md5": true, 
                "mime": false, 
                "path": "/etc/letsencrypt/live/example.com"
            }, 
            "module_name": "stat"
        }, 
        "item": {
            "key": "example.fi", 
            "value": {
                "mail": "info@example.fi", 
                "site": "example.fi", 
                "site_alias": "www.example.fi"
            }
        }, 
        "stat": {
            "atime": 1493734857.9738503, 
            "ctime": 1485960159.8090317, 
            "dev": 2049, 
            "executable": true, 
            "exists": true, 

如果我想再次通过{{sites}}进行迭代,如何使用或获取值“check_path.results.stats.exists”下一个任务中的最后一个值?

我尝试过这样的事情但没有成功。

- name: Make a certificate the first time.
  command: /bin/bash /opt/letsencrypt/letsencrypt-auto certonly --    standalone --email "{{ item.value.mail }}" --agree-tos --keep-until-    expiring -d "{{ item.value.site }}" -d "{{ item.value.site_alias }}"
  with_items: check_path
  when: check_path.results.stat.exists == false

- name: Make a certificate the first time.
  command: /bin/bash /opt/letsencrypt/letsencrypt-auto certonly standalone --email "{{ item.value.mail }}" --agree-tos --keep-until-expiring -d "{{ item.value.site }}" -d "{{ item.value.site_alias }}"
  with_dict: "{{ sites }}"
  when: check_path.results.stat.exists == false

1 个答案:

答案 0 :(得分:1)

你应该迭代结果,而不是原始列表:

- name: Make a certificate the first time.
  command: /bin/bash /opt/letsencrypt/letsencrypt-auto certonly standalone --email "{{ item.item.value.mail }}" --agree-tos --keep-until-expiring -d "{{ item.item.value.site }}" -d "{{ item.item.value.site_alias }}"
  with_items: "{{ check_path.results }}"
  when: not item.stat.exists

此处item.item是原始列表中的项目。