Does include_role and delegate_to work together in Ansible 2.9 as I'm trying to execute the following playbook by running a role and delegating it to host 2 (code below)?
Ansible playbook
- name: top level playbook
  hosts: ["host1", "host2"]
  connection: local
  gather_facts: true
  ignore_errors: no
  tasks:
    - set_fact:
        playbook_dir: /Users/OneDrive 
        validation_overall: 'pass'
        result: {}
        all_hosts: "{{ ansible_play_hosts }}"
    - name: import hostvars
      include_vars:
        dir: '{{ playbook_dir }}/test_env_vars/hostvars'
        files_matching: '{{ inventory_hostname }}.*'
    - name: initialise required input variables
      set_fact:
        input_interfaces: "{{ e_input_interfaces }}"  
    # delegate role to host2
    - name: "call validate_rtr_state role with host '{{ansible_hostname}}' for hosts in '{{ansible_play_hosts}}'"
      include_role:
        name: validate_rtr_state
        tasks_from: cisco-ios-xr_ping.yml
      apply:
        delegate_to: "{{all_hosts[1]}}"
      loop: "{{ansible_play_hosts}}"
      loop_control:
        loop_var: all_hosts[1]
The error msg I receive is as follows:
ERROR! conflicting action statements: apply, include_role
The error appears to be in '/home/bbann/Ansible-Networking/ha_failover_top_level_reload.yml': line 46, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
    # delegate role to tusredrweca908
    - name: "call validate_rtr_state role with host '{{ansible_hostname}}' for hosts in '{{ansible_play_hosts}}'"
      ^ here
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:
with_items:
  - {{ foo }}
Should be written as:
with_items:
  - "{{ foo }}"
Any ideas why this is failing?
Q: "ERROR! conflicting action statements: apply, include_role"
A: The indentation of apply is wrong. It's a parameter of the task include_role
# delegate role to host2
    - name: "call validate_rtr_state role with host '{{ ansible_hostname }}' for hosts in '{{ ansible_play_hosts }}'"
      include_role:
        name: validate_rtr_state
        tasks_from: cisco-ios-xr_ping.yml
        apply:
          delegate_to: "{{ all_hosts[1] }}"
      loop: "{{ ansible_play_hosts }}"
      loop_control:
        loop_var: all_hosts[1]
(not tested)
There are other weirdos:
loop_var is the name of the 2nd host? i.e. Does the role implement the name of the 2nd host as a variable?
Are there 2 hosts in the play both with connection: local?
Why apply delegate_to: xy instead of delegate_to: xy the task?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With