Run Ansible Playbook based on the Redundancy State.

We have two nodes in our PRODUCTION environment that are setup as active-standby pair. Generally i have to login, see the status and run the playbook manually on the device that is currently active.

But is there a way to make Ansible to figure our what is the active node and run Playbook on the Active node automatically?

Thanks in advance!

Any help on this would be appreciated! Thanks!

Do you not use a floating IP address to log into the devices ? The floating IP is associated with the active F5 and it is easier to know for sure that you are accessing the active F5. You can also try to execute a “show” command to identify the active F5, if you want to pursue that route.

You may have already figured it out based on previous comments, but for posterity, below is an example of using the failover_state bigip fact as a condition for performing a task which is hopefully what you were after. The bigip_node task in this playbook contains a when statement so it only runs when the value of the failover_state fact is HA_STATE_ACTIVE; therefore, the task runs on the active device and not the standby. As you probably know if you create sync’able objects on all devices in the DG with a playbook you’ll encounter a change conflict between the DG members. This playbook avoids that.

Update: Removing my example and instead pointing to:

K10531487: Running Ansible tasks on the active BIG-IP in a device group

Which AskF5 has published based on this Q&A. They’ve done this for other Q&As on DC as well.

This article contains more detailed instructions for creating this playbook and you can leave feedback at the bottom of the page.

You may have already figured it out based on previous comments, but for posterity, below is an example of using the failover_state bigip fact as a condition for performing a task which is hopefully what you were after. The bigip_node task in this playbook contains a when statement so it only runs when the value of the failover_state fact is HA_STATE_ACTIVE; therefore, the task runs on the active device and not the standby. As you probably know if you create sync’able objects on all devices in the DG with a playbook you’ll encounter a change conflict between the DG members. This playbook avoids that.

Update: Removing my example and instead pointing to:

K10531487: Running Ansible tasks on the active BIG-IP in a device group

Which AskF5 has published based on this Q&A. They’ve done this for other Q&As on DC as well.

This article contains more detailed instructions for creating this playbook and you can leave feedback at the bottom of the page.

I get an error when running this playbook. Error is “msg”: “module (bigip_facts) is missing interpreter line”. I’m using the bigip_facts.py from the f5-ansible repo on github

I get an error when running this playbook. Error is “msg”: “module (bigip_facts) is missing interpreter line”. I’m using the bigip_facts.py from the f5-ansible repo on github

That error does not appear to be specific to F5 Ansible modules. Looks like an error that occurs when Ansible can’t find the module due to a naming conflict. The following issues offer clues:

similar issue 1

similar issue 2

Does running the playbook with high verbosity, the -vvvv flag, give you more details about the error?

That error does not appear to be specific to F5 Ansible modules. Looks like an error that occurs when Ansible can’t find the module due to a naming conflict. The following issues offer clues:

similar issue 1

similar issue 2

Does running the playbook with high verbosity, the -vvvv flag, give you more details about the error?

I see what your saying but I don’t understand why is doing this. It’s looking for the module in “Using module file /media/sf_f5_ansible_modules/f5_playbooks/bigip_facts.yml” but this is not the module file and the directory for the module in my ansible.cfg is /media/sf_f5_ansible_modules. Can you explained why this might be happening?

I see what your saying but I don’t understand why is doing this. It’s looking for the module in “Using module file /media/sf_f5_ansible_modules/f5_playbooks/bigip_facts.yml” but this is not the module file and the directory for the module in my ansible.cfg is /media/sf_f5_ansible_modules. Can you explained why this might be happening?

ok I figured it out I had a bigip_facts.yml file in my playbooks directory. I removed that .yml file and now I get a much more involved error message.

“msg”: “received exception: nonnumeric port: ‘None’\ntraceback: Traceback (most recent call last):\n File \”/tmp/ansible_gnN7y2/ansible_module_bigip_facts.py\“, line 1715, in main\n saved_active_folder = ()\n File \”/tmp/ansible_gnN7y2/ansible_module_bigip_facts.py\“, line 139, in get_active_folder\n return self.api.System.Session.get_active_folder()\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 379, in __getattr__\n client = self._client_creator(‘%s.%s’ % (self._name, attr))\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 183, in _create_client\n self._timeout,self._port)\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 284, in get_client\n cache=cachedir, transport=transport, timeout=timeout)\n File \”/usr/local/lib/python2.7/dist-packages/suds/client.py\“, line 115, in __init__\n self.wsdl = reader.open(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 150, in open\n d = self.fn(url, self.options)\n File \”/usr/local/lib/python2.7/dist-packages/suds/wsdl.py\“, line 136, in __init__\n d = reader.open(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 74, in open\n d = self.download(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 92, in download\n fp = self.options.transport.open(Request(url))\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/https.py\“, line 62, in open\n return HttpTransport.open(self, request)\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/http.py\“, line 67, in open\n return self.u2open(u2request)\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/http.py\“, line 132, in u2open\n return url.open(u2request, timeout=tm)\n File \”/usr/lib/python2.7/urllib2.py\“, line 429, in open\n response = self._open(req, data)\n File \”/usr/lib/python2.7/urllib2.py\“, line 447, in _open\n ‘_open’, req)\n File \”/usr/lib/python2.7/urllib2.py\“, line 407, in _call_chain\n result = func(*args)\n File \”/usr/lib/python2.7/urllib2.py\“, line 1241, in https_open\n context=self._context)\n File \”/usr/lib/python2.7/urllib2.py\“, line 1167, in do_open\n h = http_class(host, timeout=req.timeout, **http_conn_args)\n File \”/usr/lib/python2.7/httplib.py\“, line 1243, in __init__\n source_address)\n File \”/usr/lib/python2.7/httplib.py\“, line 736, in __init__\n (self.host, self.port) = self._get_hostport(host, port)\n File \”/usr/lib/python2.7/httplib.py\“, line 777, in _get_hostport\n raise InvalidURL(\“nonnumeric port: ‘%s’\” % host[i+1:])\nInvalidURL: nonnumeric port: ‘None’\n” }

ok I figured it out I had a bigip_facts.yml file in my playbooks directory. I removed that .yml file and now I get a much more involved error message.

“msg”: “received exception: nonnumeric port: ‘None’\ntraceback: Traceback (most recent call last):\n File \”/tmp/ansible_gnN7y2/ansible_module_bigip_facts.py\“, line 1715, in main\n saved_active_folder = ()\n File \”/tmp/ansible_gnN7y2/ansible_module_bigip_facts.py\“, line 139, in get_active_folder\n return self.api.System.Session.get_active_folder()\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 379, in __getattr__\n client = self._client_creator(‘%s.%s’ % (self._name, attr))\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 183, in _create_client\n self._timeout,self._port)\n File \”/usr/local/lib/python2.7/dist-packages/bigsuds.py\“, line 284, in get_client\n cache=cachedir, transport=transport, timeout=timeout)\n File \”/usr/local/lib/python2.7/dist-packages/suds/client.py\“, line 115, in __init__\n self.wsdl = reader.open(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 150, in open\n d = self.fn(url, self.options)\n File \”/usr/local/lib/python2.7/dist-packages/suds/wsdl.py\“, line 136, in __init__\n d = reader.open(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 74, in open\n d = self.download(url)\n File \”/usr/local/lib/python2.7/dist-packages/suds/reader.py\“, line 92, in download\n fp = self.options.transport.open(Request(url))\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/https.py\“, line 62, in open\n return HttpTransport.open(self, request)\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/http.py\“, line 67, in open\n return self.u2open(u2request)\n File \”/usr/local/lib/python2.7/dist-packages/suds/transport/http.py\“, line 132, in u2open\n return url.open(u2request, timeout=tm)\n File \”/usr/lib/python2.7/urllib2.py\“, line 429, in open\n response = self._open(req, data)\n File \”/usr/lib/python2.7/urllib2.py\“, line 447, in _open\n ‘_open’, req)\n File \”/usr/lib/python2.7/urllib2.py\“, line 407, in _call_chain\n result = func(*args)\n File \”/usr/lib/python2.7/urllib2.py\“, line 1241, in https_open\n context=self._context)\n File \”/usr/lib/python2.7/urllib2.py\“, line 1167, in do_open\n h = http_class(host, timeout=req.timeout, **http_conn_args)\n File \”/usr/lib/python2.7/httplib.py\“, line 1243, in __init__\n source_address)\n File \”/usr/lib/python2.7/httplib.py\“, line 736, in __init__\n (self.host, self.port) = self._get_hostport(host, port)\n File \”/usr/lib/python2.7/httplib.py\“, line 777, in _get_hostport\n raise InvalidURL(\“nonnumeric port: ‘%s’\” % host[i+1:])\nInvalidURL: nonnumeric port: ‘None’\n” }

I guess an httplib.py function received nothing as an argument for the port parameter. The default port is 443. Are you using a non-default port? Can you show me your bigip_facts task and a snippet from your hosts file showing the hosts for this playbook? Be sure to change IP addrs and hostnames and any other sensitive info to something generic.

I guess an httplib.py function received nothing as an argument for the port parameter. The default port is 443. Are you using a non-default port? Can you show me your bigip_facts task and a snippet from your hosts file showing the hosts for this playbook? Be sure to change IP addrs and hostnames and any other sensitive info to something generic.

Playbook:

  • name: Display BIG-IP details hosts: test_cli gather_facts: no connection: local

tasks: - name: get bigip facts bigip_facts: server: “{{ inventory_hostname }}” user: “admin” password: “secret” include: vlan delegate_to: localhost

HOSTS:

[test_cli] 10.1.1.1

As far as I know I’m using the default port. Port 443 for REST and 22 SSH.

Playbook:

  • name: Display BIG-IP details hosts: test_cli gather_facts: no connection: local

tasks: - name: get bigip facts bigip_facts: server: “{{ inventory_hostname }}” user: “admin” password: “secret” include: vlan delegate_to: localhost

HOSTS:

[test_cli] 10.1.1.1

As far as I know I’m using the default port. Port 443 for REST and 22 SSH.

Nothing wrong there…(except connection: local and delegate_to: localhost is redundant, but that won’t cause the issue. More on that here) seems like something more under the hood. So before going any further since the f5-ansible repository is “an incubator” and “The modules in this repository may be broken due to experimentation or refactoring.”

I suggest trying the stable releases that are included in Ansible itself:

/usr/lib/python2.7/dist-packages/ansible/modules/network/f5

or if they have been removed for some reason, you can download them from the ansible github repository as opposed to f5-ansible. The link goes directly to 2.5, but you can pick your ansible version from the Branch dropdown.

Nothing wrong there…(except connection: local and delegate_to: localhost is redundant, but that won’t cause the issue. More on that here) seems like something more under the hood. So before going any further since the f5-ansible repository is “an incubator” and “The modules in this repository may be broken due to experimentation or refactoring.”

I suggest trying the stable releases that are included in Ansible itself:

/usr/lib/python2.7/dist-packages/ansible/modules/network/f5

or if they have been removed for some reason, you can download them from the ansible github repository as opposed to f5-ansible. The link goes directly to 2.5, but you can pick your ansible version from the Branch dropdown.

Same error so it looks like its not related to the module