Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible when condition:仅在命令失败时运行脚本_Ansible - Fatal编程技术网

Ansible when condition:仅在命令失败时运行脚本

Ansible when condition:仅在命令失败时运行脚本,ansible,Ansible,我希望bash脚本仅在命令出现错误时运行,但我不太熟悉如何使用when条件,有人能帮忙吗 gaiacli status ERROR: Status: Post "http://localhost:26657": dial tcp 127.0.0.1:26657: connect: connection refused 您需要做两件事,因此必须将它们分为两个单独的任务: -name:运行脚本 命令:gaiacli状态 当:false时失败 注册号:gaia_status 更

我希望bash脚本仅在命令出现错误时运行,但我不太熟悉如何使用when条件,有人能帮忙吗

gaiacli status

ERROR: Status: Post "http://localhost:26657": dial tcp 127.0.0.1:26657: connect: connection refused

您需要做两件事,因此必须将它们分为两个单独的任务:

-name:运行脚本
命令:gaiacli状态
当:false时失败
注册号:gaia_status
更改时间:false
-名称:脚本失败时初始化
变成:真的
shell:sh init.sh#您还可以使用命令模块
时间:gaia_status.rc!=0

您想做两件事,因此必须将它们分成两个单独的任务:

-name:运行脚本
命令:gaiacli状态
当:false时失败
注册号:gaia_status
更改时间:false
-名称:脚本失败时初始化
变成:真的
shell:sh init.sh#您还可以使用命令模块
时间:gaia_status.rc!=0

Ansible的'when'命令或多或少类似于您日常使用的条件运算符,只是更简洁了一点

让我们以Ansible的官员为例

tasks:
  - name: Register a variable, ignore errors and continue
    ansible.builtin.command: /bin/false
    register: result
    ignore_errors: true

  - name: Run only if the task that registered the "result" variable fails
    ansible.builtin.command: /bin/something
    when: result is failed

  - name: Run only if the task that registered the "result" variable succeeds
    ansible.builtin.command: /bin/something_else
    when: result is succeeded

  - name: Run only if the task that registered the "result" variable is skipped
    ansible.builtin.command: /bin/still/something_else
    when: result is skipped
在第一个任务中,Ansible使用其命令模块执行shell命令,并将其输出注册到名为结果的变量中

之后,您可以使用结果查看下一个任务,并在时使用检查它。有趣的是,跳过/成功/失败是官方关键词

您可以在代码段上使用类似的方法。 另一种方法是在bash命令失败时生成一个变量,将其注册到Ansible,然后在中使用它,如下面的示例所示

- name: Test play
  hosts: all

  tasks:

      - name: Register a variable
        ansible.builtin.shell: cat /etc/motd
        register: motd_contents

      - name: Use the variable in conditional statement
        ansible.builtin.shell: echo "motd contains the word hi"
        when: motd_contents.stdout.find('hi') != -1

让我知道它是否有帮助,或者您是否需要额外的帮助。

Ansible的'when'命令或多或少类似于您的日常条件运算符,只是更简洁一点

让我们以Ansible的官员为例

tasks:
  - name: Register a variable, ignore errors and continue
    ansible.builtin.command: /bin/false
    register: result
    ignore_errors: true

  - name: Run only if the task that registered the "result" variable fails
    ansible.builtin.command: /bin/something
    when: result is failed

  - name: Run only if the task that registered the "result" variable succeeds
    ansible.builtin.command: /bin/something_else
    when: result is succeeded

  - name: Run only if the task that registered the "result" variable is skipped
    ansible.builtin.command: /bin/still/something_else
    when: result is skipped
在第一个任务中,Ansible使用其命令模块执行shell命令,并将其输出注册到名为结果的变量中

之后,您可以使用结果查看下一个任务,并在时使用检查它。有趣的是,跳过/成功/失败是官方关键词

您可以在代码段上使用类似的方法。 另一种方法是在bash命令失败时生成一个变量,将其注册到Ansible,然后在中使用它,如下面的示例所示

- name: Test play
  hosts: all

  tasks:

      - name: Register a variable
        ansible.builtin.shell: cat /etc/motd
        register: motd_contents

      - name: Use the variable in conditional statement
        ansible.builtin.shell: echo "motd contains the word hi"
        when: motd_contents.stdout.find('hi') != -1

如果有帮助请告诉我,或者您是否需要其他帮助。

谢谢Lester!如果我想检查go是否已安装,这是否有效?例如命令:/usr/local/go/bin/go版本寄存器:go\u状态shell:sh init-go.sh when:go\u status.rc=0?
rc
表示返回代码。这相当于在服务器上运行相同的命令,然后回显
$?
。这里是一个专业提示:在执行
go version
任务之后,添加一个调试任务来打印变量
go\u status
,以查看命令返回的所有内容。谢谢Lester!如果我想检查go是否已安装,这是否有效?例如命令:/usr/local/go/bin/go版本寄存器:go\u状态shell:sh init-go.sh when:go\u status.rc=0?
rc
表示返回代码。这相当于在服务器上运行相同的命令,然后回显
$?
。以下是一个专业提示:在执行
go version
任务后,添加一个调试任务以打印变量
go\u status
,查看命令返回的所有内容。