I'd like to change ansible_default_ipv4 to point to eth1 instead of eth0. Can I do this in either the playbook or via the --extra-vars option?
相关问题
- Access ansible.cfg variable in task
- Unable to get “exclude” option working with unarch
- Ansible: find file and loop over paths
- Generating tuples variables in ansible templates
- Ansible: setting user on dynamic ec2
相关文章
- Ansible create postgresql user with access to all
- Difference between shell and command in ansible
- How to make Ansible run one certain task only on o
- clone a specific branch from git through ansible p
- Check if a list contains an item in Ansible
- Defining host as variable in Ansible hosts file
- ansible/jinja2 get unique subelements
- Custom credentials in Ansible Tower with Custom Py
Another option is always reference your interface explicitly. Different cloud providers have different conventions weather to put the private (usually 10.x.y.z) management network on eth0 or eth1.
Instead of referencing
You can always define the iface you want (say in group_vars/all)
iface: eth0
and reference it in tasks as
{{ 'ansible_'+iface['ipv4']['address'] }}
A simple grep will show you all references. A global find and replace on your project will let you change this.
grep -RH ansible_default_ipv4 .
Then when you want to switch cloud providers you simply adapt the iface as needed.
If you prefer something less verbose, and more explicit define
and only use
ansible_default_ipv4
when you want to refer to either private or public ip that would be used to get to 8.8.8.8 (the internet in general).
BTW, i tried Eron Wright's suggestion for route add -net 8.8.8.8 netmask 255.255.255.255 eth1. It didn't seem to work for me, and i was unable to ping google (though pinging any other public ip worked).
ansible uses command
ip -4 route get 8.8.8.8
to get the default ipv4 interface. You can change your ip/routing tables to makeeth1
the default route and it'll return it.Or you can use a custom fact.
PS: using
set_fact
to override theansible_default_ipv4
fact, but it has it's own pitfalls due to caching, scope, ...ip -4 route get 8.8.8.8
was not working on my server.I created this work around.
- name: find default ipv4... this is a bit of a hack. shell: ifconfig $(route | grep default | awk '{print $(NF)}') | grep 'inet' | awk '{ print $2}' register: ipv4_address
Now I can use
ipv4_address
wherever needed!