条件为真时跳过跳过

时间:2019-03-07 10:06:56

标签: ansible

如果未安装磁盘,我想格式化磁盘。为此,我有以下任务:

- name: Check if drives are already mounted
  shell: df | grep "{{item.drive}}" | wc -l
  with_items: "{{disks}}"
  register: disks_checked
  changed_when: False

- name: output debug
  debug: var=disks_checked

- name: Make filesystem
  become: yes
  filesystem:
    fstype: ext4
    dev: "{{item.item.drive}}"
    opts: -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard"
  when: "item.stdout == 0"
  with_items: '{{disks_checked.results}}'

这是剧本运行:

TASK [install-cassandra : Check if drives are already mounted] *********************************************************************************************************************************
ok: [10.142.0.3] => (item={u'directory': u'/var/lib/mysql', u'mount_point': u'/mnt/disks', u'drive': u'/dev/sdb'})
TASK [install-cassandra : output debug] ********************************************************************************************************************************************************
ok: [10.142.0.3] => {
    "disks_checked": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_ignore_errors": null, 
                "_ansible_item_label": {
                    "directory": "/var/lib/mysql", 
                    "drive": "/dev/sdb", 
                    "mount_point": "/mnt/disks"
                }, 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": false, 
                "cmd": "df | grep \"/dev/sdb\" | wc -l", 
                "delta": "0:00:00.005613", 
                "end": "2019-03-07 09:48:50.293829", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "df | grep \"/dev/sdb\" | wc -l", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "warn": true
                    }
                }, 
                "item": {
                    "directory": "/var/lib/mysql", 
                    "drive": "/dev/sdb", 
                    "mount_point": "/mnt/disks"
                }, 
                "rc": 0, 
                "start": "2019-03-07 09:48:50.288216", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "0", 
                "stdout_lines": [
                    "0"
                ]
            }
        ]
    }
}

TASK [install-cassandra : Make filesystem] *****************************************************************************************************************************************************
skipping: [10.142.0.3] => (item={'_ansible_parsed': True, 'stderr_lines': [], u'changed': False, u'stdout': u'0', '_ansible_item_result': True, u'delta': u'0:00:00.005613', 'stdout_lines': [u'
0'], '_ansible_item_label': {u'directory': u'/var/lib/mysql', u'mount_point': u'/mnt/disks', u'drive': u'/dev/sdb'}, u'end': u'2019-03-07 09:48:50.293829', '_ansible_no_log': False, 'failed': 
False, u'cmd': u'df | grep "/dev/sdb" | wc -l', 'item': {u'directory': u'/var/lib/mysql', u'mount_point': u'/mnt/disks', u'drive': u'/dev/sdb'}, u'stderr': u'', u'rc': 0, u'invocation': {u'mod
ule_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'df | grep "/dev/sdb" | wc -l', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdi
n': None}}, u'start': u'2019-03-07 09:48:50.288216', '_ansible_ignore_errors': None})   

如您所见,标准输出为“ 0”,但是在上一个任务中评估条件when: "item.stdout == 0"时,它会跳过,就好像条件为假。
如果我将条件更改为when: "item.stdout == '0'",则对于上一个任务,此错误会变得很奇怪:

TASK [install-cassandra : Make filesystem] *****************************************************************************************************************************************************
failed: [10.142.0.3] (...) "module_stderr": "Shared connection to 10.142.0.3 closed.\r\n"(...)

为什么未按预期评估条件?为什么在第二种情况下会断开连接?

1 个答案:

答案 0 :(得分:1)

好吧,正如CERN_Fan指出的那样,第二个解决方案应该可以工作...问题是opts行末的引号...我不敢相信我花了2个小时来寻找问题所在其他地方。

我保留答案和问题是因为错误(连接已关闭)与问题无关,其他人可能会碰到这个问题。