gpt4 book ai didi

ssh - 仅当满足条件并在 Ansible Playbook 中重新启动 SSH 服务时才在 Ansible 循环中处理项目

转载 作者:行者123 更新时间:2023-12-02 14:21:50 24 4
gpt4 key购买 nike

我在 Ansible 剧本中有几个简单的任务:

tasks:

- name: Reset firewall
action: shell ufw --force reset

- name: Manage firewall ports
ufw: rule=allow port={{ item }} proto=tcp
with_items:
- "{{ ssh_port }}"
- "{{ 80 if myvar == 'yes' else '' }}"
- "{{ 8080 if myvar == 'no' else '' }}"

- name: Enable firewall
ufw: state=enabled
notify: restart ssh

handlers:

- name: restart ssh
service:
name=sshd
state=restarted
enabled=yes

以及关于此任务的 2 个简单问题:
  • 是否可以完全忽略item如果条件不满足,在上面的循环中?
  • 如何“正确”重启sshd使用 ufw 管理端口后使用 Ansible 提供服务(因为在我的情况下,我可以理解地在尝试重新启动时遇到 Ansible 的连接错误 sshd )
  • 最佳答案

    is it possible to completely ignore item in above loop if condition is not met?



    我只是将其更改为:
    - name: Manage firewall ports
    ufw: rule=allow port={{ item }} proto=tcp
    with_items:
    - "{{ ssh_port }}"
    - "{{ 80 if myvar == 'yes' else 8080 }}"

    ...这会得到相同的结果。在其他情况下,您可以使用 when 控制哪些项目触发操作。限定词:
    - debug: msg="using item {{item}}"
    with_items:
    - one
    - two
    - three
    when: "'o' in item"

    结果是:
    TASK: [debug msg="using item {{item}}"] *************************************** 
    ok: [localhost] => (item=one) => {
    "item": "one",
    "msg": "using item one"
    }
    ok: [localhost] => (item=two) => {
    "item": "two",
    "msg": "using item two"
    }
    skipping: [localhost] => (item=three)

    how to "properly" restart sshd service with Ansible after managing ports with ufw (since in my case I understandably get an Ansible's connection error when trying to restart sshd)



    重启 sshd服务不应中断现有的 ssh session 。如果它阻止了 ansible 的后续连接,您可以使用 Ansible 的 wait_for模块延迟,直到 ssh 准备好接受连接。

    关于ssh - 仅当满足条件并在 Ansible Playbook 中重新启动 SSH 服务时才在 Ansible 循环中处理项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944957/

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