如何获得地址'来自' ansible_default_ipv4'使用ansible 2.5和python 2/3

时间:2018-06-17 10:42:28

标签: ansible jinja2

我尝试使用ansible创建一个/etc/hosts文件。这听起来像一个简单的任务,但我被ansible_default_ipv4值/字典/数组所困扰。

我已经在这个要点https://gist.github.com/rothgar/8793800中尝试了几种解决方案,但它们都没有帮助我解决这个问题。

我想要的是创建这样的主机行

  

ansible_default_ipv4.address inventory_hostname inventory_hostname_short

我试过这个模板

{# {{ ansible_facts.ansible_default_ipv4.address }} {{ ansible_facts.ansible_nodename }} {{ ansible_hostname }} #}

{% for hostname in hostvars %}
  {%- if hostvars[hostname].inventory_hostname_short != hostvars[hostname].ansible_forks -%}

    {{ hostvars[hostname] |pprint(verbose=True) }}

>>ansible_default_ipv4: {{ hostvars[hostname].ansible_default_ipv4 | pprint(verbose=True)  }}


>>inventory_hostname_short: {{ hostvars[hostname].inventory_hostname_short }}

>>ansible_forks: {{ hostvars[hostname].ansible_forks }}

>>playbook_dir: {{ hostvars[hostname].playbook_dir }}

  {% endif %}

{% endfor %}

创建此输出。

{u'ansible_all_ipv4_addresses': [u'xxx.xxx....'],
 u'ansible_all_ipv6_addresses': [u'...',
                                 u'...'],
 u'ansible_apparmor': {u'status': u'enabled'},
 u'ansible_architecture': u'x86_64',
 u'ansible_bios_date': u'03/06/2017',
 u'ansible_bios_version': u'2.0',
 'ansible_check_mode': False,
....
 'inventory_dir': u'ansible',
 'inventory_file': u'ansible/hosts',
 'inventory_hostname': u'MyHOSTNAME.DOMAIN',
 'inventory_hostname_short': u'MyHOSTNAME',
 u'module_setup': True,
 'omit': '__omit_place_holder__f460a1c747507f208ec73347e62d44db31e2a427',
 'playbook_dir': u'ansible/playbooks'}

>>ansible_default_ipv4: {u'address': u'xxx.xxx...',
 u'alias': u'enp1s0f0',
 u'broadcast': u'...',
 u'gateway': u'...',
 u'interface': u'enp1s0f0',
 u'macaddress': u'...',
 u'mtu': 1500,
 u'netmask': u'255.255.255.0',
 u'network': u'...',
 u'type': u'ether'}


>>inventory_hostname_short: MyHOSTNAME

>>ansible_forks: 50

>>playbook_dir: ansible/playbooks

现在我太天真了,并且考虑使用{{ hostvars[hostname].ansible_default_ipv4.address }}这会引发此错误

fatal: [MyHOSTNAME.DOMAIN]: FAILED! => {
    "changed": false,
    "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_default_ipv4'"
}

嘿,这看起来像一个字典,所以尝试dict版本{{ hostvars[hostname].ansible_default_ipv4['address] }}

fatal: [MyHOSTNAME.DOMAIN]: FAILED! => {
    "changed": false,
    "msg": "AnsibleError: template error while templating string: unexpected char u\"'\" at 778. String: # {{ ansible_managed }}\n\n127.0.0.1       localhost\n\n# The following lines are desirable for IPv6 capable hosts\n::1     ip6-localhost   ip6-loopback\nfe00::0 ip6-localnet\nff00::0 ip6-mcastprefix\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\nff02::3 ip6-allhosts\n\n\n# Network nodes as generated through Ansible.\n\n{# {{ ansible_facts.ansible_default_ipv4.address }} {{ ansible_facts.ansible_nodename }} {{ ansible_hostname }} #}\n\n{% for hostname in hostvars %}\n  {%- if hostvars[hostname].inventory_hostname_short != hostvars[hostname].ansible_forks -%}\n\n    {{ hostvars[hostname] |pprint(verbose=True) }}\n\n>>ansible_default_ipv4: {{ hostvars[hostname].ansible_default_ipv4.address | pprint(verbose=True)  }} \n>>ansible_default_ipv4.address: {{ hostvars[hostname].ansible_default_ipv4['address] }} \n\n\n>>inventory_hostname_short: {{ hostvars[hostname].inventory_hostname_short }} \n\n>>ansible_forks: {{ hostvars[hostname].ansible_forks }}\n\n>>playbook_dir: {{ hostvars[hostname].playbook_dir }}\n\n  {% endif %}\n\n{% endfor %}\n\n"
}

u'看起来像这里描述的字符串前缀https://docs.python.org/3/reference/lexical_analysis.html#literals

现在我迷路了,希望有人能帮助我解决这个问题。我最后的假设是它也可能是python2 =>的一个问题。 python3混合设置,可能吗?

ansible和python on ansible执行主机

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:        16.04
Codename:       xenial

ansible-playbook 2.5.3
  config file = ansible/ansible.cfg
  configured module search path = [u'/home/al/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
节点主机上的

python

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:        18.04
Codename:       bionic
python3 --version
Python 3.6.5

0 个答案:

没有答案