Get IP address facts after configuring DHCP

Does anyone have a good method of getting the IP address leased to an interface in the middle of a play?

I have some RHEL 6 servers that use DHCP. I want to configure source-based routing on each so I need to know the actual IP address acquired by the DHCP lease. i.e. my playbook ensures BOOTPROTO=dhcp in /etc/sysconfig/network-scripts/ifcfg-{{ interface }}, then writes /etc/sysconfig/network-scripts/rule-{{ interface}} from the template:

unicast from {{ ip_address }} table {{ interface }}

A couple of ways I thought about doing it include:

* Run twice. The first time the interface is configured to use DHCP and brought up, and then the second time I can pull the IP address from ansible_facts. That kind of sucks, but it works.

* Create a handler that re-runs the setup module to get the facts about the new interface, have the interface config action notify that handler, and then call flush_handlers. That works and avoids having to run the playbook twice, but it would be better if I could only flush the one handler (I might not actually want to flush ALL handlers everytime). Is flush_handlers parameterized? I haven't checked the code yet...

* I could skip the handler & flush_handles and just insert an action that always calls the setup module. That avoids potentially flushing handlers I don't want to flush at the expense of having to document & remember why it's important that action "always must come after the interface configuration and before the rule & routes configuration"

Are there any other alternatives? Is there a best way? All advice greatly appreciated! :slight_smile:

I’d just do:

  • setup

after the other step.