如果文件大小为零 (0),Ansible playbook 如何失败

问题描述 投票:0回答:2

这个剧本应该如何运作?当文件大小为零时,我希望剧本失败

---
- hosts: localhost
  gather_facts: no

  tasks:
    - name: "Get stats of a file"
      stat:
        path: files/file1.txt
      register: file1

    - name: "Fail if the filesize 0"
      fail:
        msg: "error: filesize is 0"
      when: file1.stat.size == '0'

在实践中,任务只是被跳过:

ansible-playbook 1.yml -i inventory-it1 --diff

PLAY [localhost] ********************************************************************************************************************************************************************

TASK [Get stats of a file] **********************************************************************************************************************************************************
ok: [localhost]

TASK [Fail if the filesize 0] *******************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

预计剧本会失败

ansible filesize stat
2个回答
0
投票

size
已经是
int
类型。你的条件语句试图做类似
when: int == 'string'
的事情,但永远不会
True

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: "Get stats of a file"
    stat:
      path: test.file
    register: test

  - name: "Show size and type"
    debug:
      msg: "size is {{ test.stat.size}} bytes and of type {{ test.stat.size | type_debug }}"

  - name: "Fail if the filesize 0"
    fail:
      msg: "error: filesize is 0"
    when: test.stat.size == 0

将产生

的输出
TASK [Get stats of a file] ************************************************
ok: [localhost]

TASK [Show size and type] *************************************************
ok: [localhost] =>
  msg: size is 0 bytes and of type int

TASK [Fail if the filesize 0] *********************************************
fatal: [localhost]: FAILED! => changed=false
  msg: 'error: filesize is 0'

PLAY RECAP ****************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1

由于 Ansible 可以转换数据类型,因此可能会更简单

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    int: 0

  tasks:

  - fail:
      msg: "is 0"
    when: not int # is zero

类似的问答


0
投票

例如,

shell> cat pb.yml
- hosts: localhost

  tasks:

    - assert:
        that: lookup('pipe', cmd)|int != 0
        fail_msg: '[ERR] file is empty.'
        success_msg: '[OK]  file is not empty.'
      vars:
        cmd: stat -c %s /tmp/file1.txt

如果文件为空则播放失败

shell> cat /dev/null > /tmp/file1.txt 
shell> ansible-playbook pb.yml

PLAY [localhost] ******************************************

TASK [assert] *********************************************
fatal: [localhost]: FAILED! => changed=false 
  assertion: lookup('pipe', cmd)|int != 0
  evaluated_to: false
  msg: '[ERR] file is empty.'
  ...

,否则

shell> cp /etc/passwd /tmp/file1.txt 
shell> ansible-playbook pb.yml

PLAY [localhost] ****************************************

TASK [assert] *******************************************
ok: [localhost] => changed=false 
  msg: '[OK]  file is not empty.'
  ...
© www.soinside.com 2019 - 2024. All rights reserved.