AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1054042
Accepted
Hiran Chaudhuri
Hiran Chaudhuri
Asked: 2021-02-18 13:11:49 +0800 CST2021-02-18 13:11:49 +0800 CST 2021-02-18 13:11:49 +0800 CST

Ansible - 检查项目列表

  • 772

我有一个字符串列表,我期望在某些命令的输出中。如何创建一个可测试的 ansible 脚本 - 如果一个或多个条目不包含 - 执行任务?

所以我的 ansible 脚本可能看起来像:

vars:
  musthave:
    - value1
    - value2
tasks:
- name: Check the configured values
  command: "cat configfile"
  register: current_configuration
  changed_when: false

- set a variable if one or more of the musthave's are missing in current_configuration.stdout
  ...

- name: Execute task to reconfigure system
  command: "reconfigure..."
  when: true = variable

那么有没有类似的东西

variable = false
for s in musthave:
    if not s in current_configuration.stdout:
        variable |= true
ansible
  • 1 1 个回答
  • 1370 Views

1 个回答

  • Voted
  1. Best Answer
    Hiran Chaudhuri
    2021-02-19T21:57:37+08:002021-02-19T21:57:37+08:00

    虽然使用建议的社区模块可能更容易解决,但我更喜欢不意味着要安装更多库的解决方案。这是我想出的解决方案。

    我最初的请求是检查是否存在可能出现在命令输出中某处的特定值。为差异准备工作列表是一个缺点,副作用是您可以计算两个方向的差异。

    所以现在我不仅可以检查丢失的条目,还可以检查那些太多的条目。

    - name: print hostvars
      debug:
        var: hostvars[{{ ansible_host }}]['certificate_domains']
    
    - name: print certificate
      delegate_to: silver.fritz.box
      command: "openssl x509 -in /home/hiran/homeserver.crt -noout -text"
      register: certificate
    
    - name: grab subject's common name and alternative names
      set_fact:
        commonName: "{{ certificate.stdout | regex_search('Subject.*CN\\s=\\s([\\.a-zA-Z]+)', '\\1') }}"
        altNames: "{{ certificate.stdout | regex_findall('DNS:([\\.a-zA-Z]+)') }}"
    - name: prepare the lists so they can be subtracted from each other
      set_fact:
        contained: "{{ (commonName + altNames) | unique }}"
        musthave: "{{ hostvars[{{ ansible_host }}]['certificate_domains'] | unique }}"
    - name: calculate differences
      set_fact:
        missing: "{{ musthave | difference(contained) }}"
        toomuch: "{{ contained | difference(musthave) }}"
    
    - name: show difference
      debug:
        msg: Something is wrong
      when: (missing | length)>0 or (toomuch | length)>0
    
    • 0

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve