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 / 问题 / 1112457
Accepted
inframan
inframan
Asked: 2022-10-07 22:58:09 +0800 CST2022-10-07 22:58:09 +0800 CST 2022-10-07 22:58:09 +0800 CST

Ansible 在使用查找函数进行模板化时发生未处理的异常

  • 772

我目前正在构建一个剧本来测试是否存在一些 conf 文件,然后检查内容。文件如下

  • /etc/resolv.conf - 然后检查名称服务器是否配置正确
  • /etc/systemd/timesyncd.conf - 检查是否已配置某些内容
  • /etc/ntp.conf - 还要检查是否已配置某些内容

.yml 代码如下,您可以看到每次检查的任务都是相同的,如果需要,只需重新配置文件路径和正则表达式部分。

  tasks:
    # RESOLV.CONF
    - name: Check if resolv.conf exist
      stat:
        path: /etc/resolv.conf
      register: resolv_conf

    - name: Retrieve nameservers
      debug:
        msg: "{{ contents }}"
      vars:
        contents: "{{ lookup('file', '/etc/resolv.conf') | regex_findall('\\s*nameserver\\s*(.*)') }}"
      when: resolv_conf.stat.exists == True

    # NTP.CONF
    - name: check if ntp.conf exists
      stat:
        path: /etc/ntp.conf
      register: ntp_conf

    - name: retrieve ntp conf server content
      debug:
        msg: "{{ contents }}"
      vars:
        contents: "{{ lookup('file', '/etc/ntp.conf') | regex_search('^server.*') }}"
      when: ntp_conf.stat.exists == True

    # TIMESYNC.CONF
    - name: check if timesyncd
      stat:
        path: /etc/systemd/timesyncd.conf 
      register: timesyncd_conf 

    - name: Affiche le contenu de timesyncd.conf s'il est configure
      debug:
        msg: "{{ contents }}"
      vars:
        contents: "{{ lookup('file', '/etc/systemd/timesyncd.conf') | regex_search('^NTP=.*') }}"
      when: timesyncd_conf.stat.exists == True

除了关于 NTP.CONF 检查失败并显示以下内容的任务外,这些任务运行良好:

vendredi 07 octobre 2022  08:28:07 +0200 (0:00:00.509)       0:00:05.115 ******
[WARNING]: Unable to find '/etc/ntp.conf' in expected paths (use -vvvvv to see paths)
fatal: [my_server]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('file', '/etc/ntp.conf') | regex_search('^server.*') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /etc/ntp.conf. could not locate file in lookup: /etc/ntp.conf"}

我不明白为什么它会失败,因为我使用相同的功能,用户和文件获得了相同的权限,在 /etc/. 此外,我很快尝试对“cat”做同样的事情,它有效:

 - name: check ntp.conf content  
      command: "cat /etc/ntp.conf"
      register: ntp_conf_contenu
    - debug:
        msg:
        - " {{ ntp_conf_contenu  | regex_findall ('server') }}"

你知道它为什么失败吗?

非常感谢 !

ansible
  • 1 1 个回答
  • 51 Views

1 个回答

  • Voted
  1. Best Answer
    Gerald Schneider
    2022-10-07T23:36:47+08:002022-10-07T23:36:47+08:00

    查找不在远程主机上执行,而是在本地执行。

    从文档中:

    与所有模板一样,查找在 Ansible 控制机器上执行和评估。

    因此,您检查文件是否存在于远程机器上,然后从执行剧本的本地机器上读取它。

    要从远程机器读取文件,您可以使用slurp 模块。

    • 1

相关问题

  • Ansible:将字符串转换为字典

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