import tasks depending on fact: import task name substitution possible?

Hi,

I want to execute a task on my minions. The problem is, that there are different tasks depending on facts of the devices. How can ansible deal with it?

Basically I want to upgrade the operating system on Cisco network components. Different network models (fact: ansible_net_model) have different upgrade tasks.

At the moment I have

name: Upgrade IOS
block:
(…)
when: (ansible_net_version != compliant_ios_version) and
(ansible_net_model is regex(“2960”))

name: Upgrade IOSXE
block:
(…)
when: (ansible_net_version != compliant_ios_version) and
(ansible_net_model is regex(“9200”))

Intead of the block: when: construction I thought about a

  • import_task: upgrade-{{ ansible_net_model }}.yaml

and files called upgrade-c2960.yaml, upgrade-x9200.yaml, …

Is than possible?

Michael

Hi,

I want to execute a task on my minions. The problem is, that there are different tasks depending on facts of the
devices. How can ansible deal with it?

Basically I want to upgrade the operating system on Cisco network components. Different network models (fact:
ansible_net_model) have different upgrade tasks.

At the moment I have

name: Upgrade IOS
block:
(...)
when: (ansible_net_version != compliant_ios_version) and
(ansible_net_model is regex("2960"))

name: Upgrade IOSXE
block:
(...)
when: (ansible_net_version != compliant_ios_version) and
(ansible_net_model is regex("9200"))

Intead of the block: when: construction I thought about a

- import_task: upgrade-{{ ansible_net_model }}.yaml

and files called upgrade-c2960.yaml, upgrade-x9200.yaml, ...

Is than possible?

Yes, but you need to use include_tasks module (as the name of the task file is dynamic):

    - include_tasks: "upgrade-{{ ansible_net_model }}.yaml"

Regards
          Racke

Thanks. Will try that.