ansible魔术变量不返回值

ansible magic variables not returning values

我正在尝试构建一个 /etc/hosts 文件;然而,当 运行 编写 playbook 时,我似乎无法让 hostvars 显示出来,但是使用命令行它在一定程度上起作用。

这是我的 ansible.cfg 文件:

[defaults]
ansible_managed = Please do not change this file directly since it is managed by Ansible and will be overwritten
library = ./library
module_utils = ./module_utils
action_plugins = plugins/actions
callback_plugins = plugins/callback
filter_plugins = plugins/filter
roles_path = ./roles
# Be sure the user running Ansible has permissions on the logfile
log_path = $HOME/ansible/ansible.log
inventory = hosts

forks = 20
host_key_checking = False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = $HOME/ansible/facts
fact_caching_timeout = 7200
nocows = 1
callback_whitelist = profile_tasks
stdout_callback = yaml
force_valid_group_names = ignore
inject_facts_as_vars = False

# Disable them in the context of https://review.openstack.org/#/c/469644
retry_files_enabled = False

# This is the default SSH timeout to use on connection attempts
# CI slaves are slow so by setting a higher value we can avoid the following error:
# Timeout (12s) waiting for privilege escalation prompt:
timeout = 60

[ssh_connection]
# see: https://github.com/ansible/ansible/issues/11536
control_path = %(directory)s/%%h-%%r-%%p
ssh_args = -o ControlMaster=auto -o ControlPersist=600s
pipelining = True

# Option to retry failed ssh executions if the failure is encountered in ssh itself
retries = 10

这是我的剧本:

- name: host file update
  hosts: baremetal
  become: true
  gather_facts: true 
  
  vars:
    primarydomain: "cephcluster.local"

  tasks:
    - name: print ipv4 info
      debug:
        msg: "IPv4 addresses: {{ansible_default_ipv4.address }}"

    - name: update host file
      lineinfile:
        dest: /etc/hosts
        regexp: '{{ hostvars[item].ansible_default_ipv4.address }}.*{{ item }} {{item}}.{{primarydomain}}$'
        line: "{{ hostvars[item].ansible_default_ipv4.address }} {{item}} {{item}}.{{primarydomain}}"
        state: present
        with_items: "{{ groups.baremetal }}"

这是我的清单文件:

[baremetal]
svr1 ansible_host=192.168.59.10
svr2 ansible_host=192.168.59.11
svr3 ansible_host=192.168.59.12

当我 运行 我得到的剧本时:

FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: 'ansible_default_ipv4' is undefined

然而当我运行

ansible baremetal -m gather_facts -a "filter=ansible_default_ipv4"

我得到:

| SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.59.20", 
            "alias": "eno1", 
            "broadcast": "192.168.59.255", 
            "gateway": "192.168.59.1", 
            "interface": "eno1", 
            "macaddress": "80:18:44:e0:4b:a4", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.59.0", 
            "type": "ether"
        }
    }, 
    "changed": false, 
    "deprecations": [], 
    "warnings": []
}

但是;如果我 运行

ansible baremetal -m gather_facts -a "filter=ansible_default_ipv4.address"

我在 return 中一无所获:

SUCCESS => {
    "ansible_facts": {}, 
    "changed": false, 
    "deprecations": [], 
    "warnings": []
}

我什至在剧本中尝试过:

- debug:
    msg: "IPv4 addresses: {{ansible_default_ipv4 }}"

什么都没有 returned。

不确定我错过了什么。

你应该习惯使用和滥用debug模块。

假设 ansible ad-hoc 命令让你知道事实在 ansible_facts 之下,你可以在你的剧本中完成一个简单的任务:

- debug:
    var: ansible_facts

由此,您会发现您要查找的事实嵌套在字典 ansible_facts 中的键 default_ipv4 下。并且您可以使用 属性 address.

访问其地址

所以,你的调试任务最终是:

- debug:
    msg: "IPv4 addresses: {{ ansible_facts.default_ipv4.address }}"