{{ansible_hostname}}在使用Ansible 2.3直接调用时无法正常工作

1 ansible ansible-2.x ansible-facts

有人"{{ ansible_hostname }}"在直接调用playbook任务时遇到以下问题吗?

请建议是否有其他方法可以做到这一点,或者如果我在这里做错了什么,我已经尝试了两个lineinfile&replace模块:

---
- 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 }}"'
Run Code Online (Sandbox Code Playgroud)

2)带replace模块:

---
- 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
Run Code Online (Sandbox Code Playgroud)

以下是执行剧本时的错误..

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"}
Run Code Online (Sandbox Code Playgroud)

tec*_*raf 8

ansible_hostname 是一个事实,你明确禁止收集事实(gather_facts: False),所以它没有定义.

删除该行.