gpt4 book ai didi

ansible - 使用 "when"在 ansible 中的单个任务上检查多个条件

转载 作者:行者123 更新时间:2023-12-03 08:37:59 25 4
gpt4 key购买 nike

我想使用 when 评估 ansible 中的多个条件,这是我的剧本:

- name: Check that the SSH Key exists
local_action:
module: stat
path: "/home/{{ login_user.stdout }}/{{ ssh_key_location }}"
register: sshkey_result

- name: Generating a new SSH key for the current user it's not exists already
local_action:
module: user
name: "{{ login_user.stdout }}"
generate_ssh_key: yes
ssh_key_bits: 2048
when: sshkey_result.rc == 1 and ( github_username is undefined or github_username |lower == 'none' )

这是我的 var 文件以供引用:
---
vpc_region: eu-west-1
key_name: my_github_key
ssh_key_location: .ssh/id_rsa.pub

当我尝试执行此剧本时,出现此错误:
TASK: [test | Check that the SSH Key exists] **********************************
ok: [localhost -> 127.0.0.1]

TASK: [test | Generating a new SSH key for the current user it's not exists already] ***
fatal: [localhost] => error while evaluating conditional: sshkey_result.rc == 1 and ( github_username is undefined or github_username |lower == 'none' )

FATAL: all hosts have already failed -- aborting

有人可以指出我们如何在单个任务中使用 ansible 的多个条件。

谢谢

最佳答案

你可以这样使用。

when: condition1 == "condition1" or condition2 == "condition2"

官方文档链接: The When Statement .

另外请引用这个要点:
https://gist.github.com/marcusphi/6791404

关于ansible - 使用 "when"在 ansible 中的单个任务上检查多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com