gpt4 book ai didi

ansible - {{ ansible_hostname }} 在直接使用 Ansible 2.3 调用时不起作用

转载 作者:行者123 更新时间:2023-12-05 08:14:55 26 4
gpt4 key购买 nike

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

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

---
- 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 模块:

---
- 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

Below is the error while executing the playbook..

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"}

最佳答案

ansible_hostname 是事实,并且您已明确禁用收集事实 (gather_facts: False),因此未定义。

删除该行。

关于ansible - {{ ansible_hostname }} 在直接使用 Ansible 2.3 调用时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838690/

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