首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >{{ ansible_hostname }在直接使用Ansible 2.3调用时不工作

{{ ansible_hostname }在直接使用Ansible 2.3调用时不工作
EN

Stack Overflow用户
提问于 2017-06-30 04:39:45
回答 1查看 11.2K关注 0票数 2

是否有人在直接调用剧本任务时遇到过"{{ ansible_hostname }}"的下列问题?

请建议是否有其他方法可以这样做,或者如果我在这里做错了什么,我已经在lineinfilereplace模块中尝试过了:

代码语言:javascript
复制
---
- name: Playbook to Install CollectD
  hosts: servercast01
  gather_facts: False
  remote_user: root
  become: true
  tasks:
  - name: Replacing hostname entry
    lineinfile:
      dest: "/tmp/collectd/etc/collectd.conf"
      regexp: '#Hostname  "myvm01"'
      line: 'Hostname  "{{ ansible_hostname }}"'

2)使用replace模块:

代码语言:javascript
复制
---
- name: Playbook to Install CollectD
  hosts: servercast01
  gather_facts: False
  remote_user: root
  become: true
  tasks:
  - name: Replacing hostname entry
    replace:
      dest: /tmp/collectd/etc/collectd.conf
      regexp: '#Hostname  "myvm01"'
      replace: 'Hostname  "{{ ansible_hostname }}"'
      backup: yes

下面是执行剧本时的错误。

代码语言:javascript
复制
fatal: [servercast01]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible_hostname' is undefined\n\nThe error appears to have been in '/etc/ansible/lineinfile3.yml': line 10, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: Replacing hostname entry\n    ^ here\n"}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-30 05:02:13

ansible_hostname是一个事实,您已经显式地禁用了收集事实(gather_facts: False),所以没有定义它。

把绳子移开。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44838690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档