gpt4 book ai didi

Ansible - 在角色的剧本条件下使用

转载 作者:行者123 更新时间:2023-12-05 05:03:43 28 4
gpt4 key购买 nike

我正在尝试在我的 ansible-playbook 中添加一个 when 条件。但是,我无法弄清楚执行此操作的确切语法/方法,因为我的角色正在使用其他参数。下面提到的是我的剧本

---

- name: Deploying application code
hosts: uat-aegis

roles:
- { role: roles/send_slack, slack_message_text: "*`Started : Deploying code for {{ module_name }} on {{ inventory_hostname }}`*"}
- { role: roles/application_code_backup, backup_directory_name: "NAME", repo_directory: "/path/to/repo" }
- { role: roles/application_copy_config, repo_directory: "/path/to/repo"}
- { role: roles/application_git_pull, repo_url: "git@code.git", repo_directory: "/path/to/repo", branch_name: "BRANCH_NAME"}
- { role: roles/application_pm2_restart, process_name: "NAME" }
- { role: roles/send_slack, slack_message_text: "*`=== Completed : Deploying code for {{ module_name }} on {{ inventory_hostname }} ===`*"}

这里我只需要在某个变量值为真时才执行roles/application_copy_config。从 Jenkins 执行 ansible-playbook 时,此变量值作为 --extra-vars 传递(执行脚本中的 bool 值参数)。我试过使用下面的代码,但它不起作用。

---

- name: Deploying application code
hosts: uat-aegis

roles:
- { role: roles/send_slack, slack_message_text: "*`Started : Deploying code for {{ module_name }} on {{ inventory_hostname }}`*"}
- { role: roles/application_code_backup, backup_directory_name: "NAME", repo_directory: "/path/to/repo" }
- { role: roles/application_copy_config
when: "copy_config=true", repo_directory: "/path/to/repo"}
- { role: roles/application_git_pull, repo_url: "git@code.git", repo_directory: "/path/to/repo", branch_name: "BRANCH_NAME"}
- { role: roles/application_pm2_restart, process_name: "NAME" }
- { role: roles/send_slack, slack_message_text: "*`=== Completed : Deploying code for {{ module_name }} on {{ inventory_hostname }} ===`*"}

ansible-playbook命令如下

ansible-playbook plays/deploy_application/code.yml --extra-vars "module_name=$MODULE_NAME config_copy_path=$CURRENT_PATH/ env=$ENVIRONMENT copy_config=$COPY_CONFIG"

请帮助我使用 ansible-playbook 中的 when 条件。这是必需的,因为我需要执行 role: roles/application_copy_config 仅当变量值为 true 时。我用过this reference用于在角色中添加 when 条件

最佳答案

能够使用 bool 过滤器为 bool 值应用 when 条件,如下所述

---

- name: Deploying application code
hosts: uat-aegis

roles:
- { role: roles/send_slack, slack_message_text: "*`Started : Deploying code for {{ module_name }} on {{ inventory_hostname }}`*"}
- { role: roles/application_code_backup, backup_directory_name: "NAME", repo_directory: "/path/to/repo", when: var1 | bool }
- { role: roles/application_copy_config, repo_directory: "/path/to/repo"}
- { role: roles/application_git_pull, repo_url: "git@code.git", repo_directory: "/path/to/repo", branch_name: "BRANCH_NAME", when: var2 | bool}
- { role: roles/application_pm2_restart, process_name: "NAME", when: not var3 | bool }
- { role: roles/send_slack, slack_message_text: "*`=== Completed : Deploying code for {{ module_name }} on {{ inventory_hostname }} ===`*"}

使用下面的代码来判断真假

when: var1 | bool ----> true
when: not var2 | bool ----> false

关于Ansible - 在角色的剧本条件下使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509794/

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