arillso / ansible.hosts Goto Github PK
View Code? Open in Web Editor NEWAnsible role that dynamically creates the hosts file.
License: MIT License
Ansible role that dynamically creates the hosts file.
License: MIT License
Hey,
I just cloned the master
branch and discovered that the deployment of the hosts file failed:
I execute the role as the following:
# ansible-playbook playbook/hostsfile.yml -e "hosts_inventory_to_hosts=true"
The Error:
TASK [system_hosts : Edits the hosts file in /etc] ****************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'
fatal: [host1.example.com]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'
fatal: [host2.example.com]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'"}
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1100, in do_template
res = j2_concat(rf)
File "<template>", line 108, in root
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 265, in wrapper
ret = func(*args, **kwargs)
File "/opt/ansible/collections/ansible_collections/ansible/netcommon/plugins/filter/ipaddr.py", line 560, in ipaddr
elif not value or value is True:
File "/usr/local/lib/python3.6/site-packages/jinja2/runtime.py", line 903, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'ipv4'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py", line 146, in run
resultant = templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1137, in do_template
raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'
fatal: [host1.example.com]: FAILED! => {
"changed": false,
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ipv4'"
}
Removing cached facs or direktly trigger ansible.builtin.setup
as pre_tasks
changed nothing.
Proof that the dict object exists:
# ansible -m setup --tree facts/ GROUP
# cat facts/host1.example.com | jq . -C | grep '"ipv4"' -A 5
"ipv4": {
"address": "172.28.20.41",
"broadcast": "172.28.20.255",
"netmask": "255.255.255.0",
"network": "172.28.20.0"
},
--
"ipv4": {
"address": "127.0.0.1",
"broadcast": "",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
# ansible --version
ansible [core 2.11.6]
python version = 3.6.8 (default, Sep 12 2021, 04:40:35) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
jinja version = 3.0.3
libyaml = True
Can anyone reproduce this error?
There is no way to backup the existing hosts file without modifying the code.
I noticed that the IPv6 entries are not getting created although the IPv6 connection is correctly recognized in the "Gathering Facts" step. But the IPv6 connection is not filed in the ansible_lo
variable. Is this the root cause?
Versions:
ansible: 2.9.13
arillso.hosts: 1.7.3
python: 3.8.5
contos: 8.2-2.2004.0.1.el8.x86_64
Facts:
....
"default_ipv4": {
"address": "XXX.91.XXX.XX",
"alias": "eth0",
"broadcast": "XXX.91.XXX.255",
"gateway": "XXX.91.XXX.1",
"interface": "eth0",
"macaddress": "00:XX:XX:XX:XX:b7",
"mtu": 1500,
"netmask": "255.255.192.0",
"network": "XXX.91.XXX.0",
"type": "ether"
},
"default_ipv6": {
"address": "XXXX:XXXX:3006:XXXX::1",
"gateway": "fe80::1",
"interface": "eth0",
"macaddress": "00:XX:XX:XX:xx:b7",
"mtu": 1500,
"prefix": "64",
"scope": "global",
"type": "ether"
},
...
ansible_lo:
{
"ansible_lo": {
"active": true,
"device": "lo",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "on [fixed]",
"netns_local": "on [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tls_hw_record": "off [fixed]",
"tls_hw_rx_offload": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on [fixed]",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "on [fixed]",
"tx_checksumming": "on",
"tx_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_segmentation": "off [fixed]",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off [fixed]",
"tx_scatter_gather": "on [fixed]",
"tx_scatter_gather_fraglist": "on [fixed]",
"tx_sctp_segmentation": "on",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_udp_segmentation": "off [fixed]",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"vlan_challenged": "on [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "127.0.0.1",
"broadcast": "",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"mtu": 65536,
"promisc": false,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "loopback"
}
}
vars.yml
hosts_ipv6: true
/etc/hosts:
# Ansible managed
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 test.mydomain.com test
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
XXX.91.XXX.XX test.mydomain.com test
Please let me know if you need further information.
Thanks,
Philipp
I just tried the module on a host, which has no default ipv6 configured, but has ipv6 enabled on the loopback interface. (which is needed for a software)
The result was, that the ipv6 localhost entries were removed.
That happened because ansible_default_ipv6.address are not defined.
I would suggest that you create an "force" option for that, or at least check also if "ansible_lo.ipv6" is defined or not.
Thank you.
It will be great to be able to use custom IP instead of the default ipv4 return by Ansible fact.
Example:
For some of our servers, the default IPv4 is the eth0 which is a public IP.
In the /etc/hosts we prefer to use the inventory IP which is also the management interface.
templates/etc/hosts.j2 is missing newline between items of hosts_dns_hostname
Hi,
Got the following error when deploying this role:
"
TASK [Apply roles] *************************************************************
179:
180:
TASK [etc-hosts : Edits the hosts file in /etc] ********************************
181:
fatal: [server]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: no filter named 'ansible.netcommon.ipaddr'. String: # {{ ansible_managed }}\n\n# The following lines are desirable for IPv4 capable hosts\n{% if hosts_hostname_loopback %}\n127.0.0.1 {{ inventory_hostname | lower }} {{ inventory_hostname_short | lower }}\n{% endif %}\n127.0.0.1 localhost.localdomain localhost\n127.0.0.1 localhost4.localdomain4 localhost4\n{{ hosts_ipv4_address }} {{ inventory_hostname | lower }} {{ inventory_hostname_short | lower }}\n{% if ansible_lo.ipv6 is defined and hosts_ipv6 %}\n\n# The following lines are desirable for IPv6 capable hosts\n{% if hosts_hostname_loopback %}\n::1 {{ inventory_hostname | lower }} {{ inventory_hostname_short | lower }}\n{% endif %}\n::1 localhost.localdomain localhost\n::1 localhost6.localdomain6 localhost6\n::1 ip6-localhost ip6-loopback\n{% if hostvars[inventory_hostname]['ansible_default_ipv6']['address'] is defined %}\n{{ hostvars[inventory_hostname]['ansible_default_ipv6']['address'] }} {{ inventory_hostname | lower }} {{ inventory_hostname_short | lower }}\n{% endif %}\nfe00::0 ip6-localnet\nff00::0 ip6-mcastprefix\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\nff02::3 ip6-allhosts\n{% endif %}\n\n{% if hosts_inventory_to_hosts %}\n# The following lines are generated from the ansible inventory\n{% for host in groups['all'] %}\n{% if hostvars[host]['ansible_interfaces'] is defined %}\n{% for interface in hostvars[host]['ansible_interfaces'] %}\n{% if interface | regex_search('^((?!' + hosts_exludes_interfaces | join('|') + ').)$') %}\n{% if hostvars[host]['ansible_' + interface]['ipv4']['address'] | ansible.netcommon.ipaddr('private') and hosts_all_private %}\n{{ hostvars[host]['ansible_' + interface]['ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }}{{ " " if hostvars[host]['hosts_aliases'] is defined }}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{{ alias }}{{ " " if not loop.last }}{% endfor %}{% endif %}{{ '\n' -}}\n{% elif hostvars[host]['ansible_' + interface]['ipv4']['address'] | ansible.netcommon.ipaddr('public') and hosts_all_public %}\n{{ hostvars[host]['ansible_' + interface]['ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }}{{ " " if hostvars[host]['hosts_aliases'] is defined }}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{{ alias }}{{ " " if not loop.last }}{% endfor %}{% endif %}{{ '\n' -}}\n{% elif not hosts_all_private and not hosts_all_public %}\n{{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }}{{ " " if hostvars[host]['hosts_aliases'] is defined }}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{{ alias }}{{ " " if not loop.last }}{% endfor %}{% endif %}{{ '\n' -}}\n{% endif %}\n{% endif %}\n{% endfor %}\n{% endif %}\n{% endfor %}\n{% if hosts_ipv6 %}\n{% for host in groups['all'] %}\n{% if hostvars[host]['ansible_interfaces'] is defined %}\n{% for interface in hostvars[host]['ansible_interfaces'] %}\n{% if interface | regex_search('^((?!' + hosts_exludes_interfaces | join('|') + ').)$') %}\n{% if hostvars[host]['ansible_' + interface]['ipv6']['address'] | ansible.netcommon.ipaddr('private') and hosts_all_private %}\n{{ hostvars[host]['ansible_' + interface]['ipv6']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }} {% set aliases=[] %}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{% if alias != hostvars[host]['internel_ansible_host'] %}{{ aliases.append(alias) }}{% endif %}{% endfor %}{% endif %}\n{% elif hostvars[host]['ansible_' + interface]['ipv6']['address'] | ansible.netcommon.ipaddr('public') and hosts_all_public %}\n{{ hostvars[host]['ansible_' + interface]['ipv6']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }} {% set aliases=[] %}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{% if alias != hostvars[host]['internel_ansible_host'] %}{{ aliases.append(alias) }}{% endif %}{% endfor %}{% endif %}\n{% elif not hosts_all_private and not hosts_all_public %}\n{{ hostvars[host]['ansible_default_ipv6']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ host }} {% set aliases=[] %}{% if hostvars[host]['hosts_aliases'] is defined %}{% for alias in hostvars[host]['hosts_aliases'] %}{% if alias != hostvars[host]['internel_ansible_host'] %}{{ aliases.append(alias) }}{% endif %}{% endfor %}{% endif %}\n{% endif %}\n{% endif %}\n{% endfor %}\n{% endif %}\n{% endfor %}\n{% endif %}\n{% endif %}\n\n{% if hosts_dns_hostname is defined %}\n# The following lines are generated by ansible with hosts_dns_hostname variable\n{% for item in hosts_dns_hostname %}\n{{ item.address }} {{ item.hostname }}{% if item.aliases is defined %}{% for alias in item.aliases %} {{ alias }}{% endfor %}{% endif %}\n{% if not loop.last %}\n\n{% endif %}\n{% endfor %}\n{% endif %}\n"}
182:
PLAY RECAP *********************************************************************
183:
server : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
184:
Exit status: 2
"
In my case interface docker0 don't have configured ipv6:
"ansible_docker0": {
"active": false,
"device": "docker0",
"id": "8000.0242d12d2f01",
"interfaces": [],
"ipv4": {
"address": "172.17.0.1",
"broadcast": "172.17.255.255",
"netmask": "255.255.0.0",
"network": "172.17.0.0"
},
"macaddress": "02:42:d1:2d:2f:01",
"mtu": 1500,
"promisc": false,
"stp": false,
"type": "bridge"
},
And when run role i have error:
fatal: [xxxxxxx]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'address'"}
I spent some time solving this problem, and it turned out that the code assumes that the interfaces are configured for ipv4 and ipv6
https://github.com/arillso/ansible.hosts/blob/main/templates/etc/hosts.j2#L46-L62
Workaround:
hosts_ipv6: false
When hosts_aliases in inventory file is not define ansible return error:
fatal: [ansible-target1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'hosts_aliases'"}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.