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

15 views
Skip to first unread message

Heinz Meier

unread,
Feb 14, 2020, 5:19:18 AM2/14/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Feb 14, 2020, 5:25:48 AM2/14/20
to ansible...@googlegroups.com
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

>
> Michael
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/be31775b-3cfc-4641-bb9b-127a95a4feae%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/be31775b-3cfc-4641-bb9b-127a95a4feae%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Heinz Meier

unread,
Feb 14, 2020, 5:32:23 AM2/14/20
to Ansible Project
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/be31775b-3cfc-4641-bb9b-127a95a4feae%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/be31775b-3cfc-4641-bb9b-127a95a4feae%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.


Thanks. Will try that.
Reply all
Reply to author
Forward
0 new messages