Score:0

Execute a task from a host in a role and delegate other task to other host in other role

ph flag
azk


Context:

I have a project with two roles.
I've reduced the number of tasks to the problem ones for better understanding.


Problem:
I run a task from a role1 in server1 and trie to delegate a task to a vmware virtualizer from condition in the first server1 role task. And have a host fail because it wants to excecute the second role task (Vmware) in the server1.


The error:

fatal: [testhost]: FAILED! => {"reason": "conflicting action statements: hosts, 
gather_facts\n\nThe error appears to be in '/home/ancible/proyects/extend_fs-role/roles/vmwaretaks/tasks/addvmwaredisk.yml': line 2, column 3, 
but may\nbe elsewhere in the file depending on tntax problem.\n\nThe offending line appears to be:\n\n---\n- hosts: localhost\n  ^ here\n"}


Here is the structure:

.
├── collections
│   └── requirements.yml
├── README.md
├── resizefs_hosts.yml
└── roles
    ├── resizefs
    │   ├── defaults
    │   │   └── main.yml
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   │   └── main.yml
    │   ├── tasks
    │   │   ├── main.yml
    │   │   ├── findfreedisk.yml
    │   ├── tests
    │   │   ├── inventory
    │   │   └── test.yml
    │   └── vars
    │       └── main.yml
    └── vmwaretaks
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── main.yml
        │   ├── addvmwaredisk.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml


This is the excecution order in case that not find a free lun in the host:

resizefs_role_hosts.yml -> roles/resizefs/tasks/main.yml -> roles/resizefs/tasks/findfreedisk.yml -> \
roles/vmwaretaks/tasks/main.yml -> roles/vmwaretaks/tasks/addvmwaredisk.yml


This are what the playbooks looks like:

$ cat  resizefs_role_hosts.yml
---
- hosts: testhost
  become: yes
  become_method: sudo
  roles:
    - role: 'roles/resizefs'
$ cat roles/resizefs/tasks/main.yml
---
# tasks file for create_fs-test
- import_tasks: findfreedisk.yml
$ cat roles/resizefs/tasks/findfreedisk.yml

- name: Finding disk with no partitions
  set_fact:
    disks: "/dev/{{outer_item.key}}"
  when:
    - not outer_item.value.partitions
    - not outer_item.value.links.ids
    - outer_item.key is search ("sd")
  with_dict: '{{ansible_devices}}'
  loop_control:
    loop_var: outer_item
  notify:
    - Format_free_disk
  changed_when: true

- name: Print disk name if available
  debug:
    msg:
      - "{{ disks|default('NOT FREE DISK') }} Available"

# If not disk available add new lun from Vmware
- include_role:
    name: vmwaretaks
  when: disks is undefined
$ cat roles/vmwaretaks/tasks/main.yml
---
# tasks file for add-new-vmware-lun
- import_tasks: addvmwaredisk.yml
$ cat roles/vmwaretaks/tasks/addvmwaredisk.yml

---
- hosts: localhost
  gather_facts: true

  vars:
    vcenter_hostname: 'vcenter.labo.local'
    vcenter_username: 'labo.local\ansible'
    vcenter_password: "{{ vault_pass }}"
    target_host: 'TESTHOST'
    vm_uuid: '4217B33E-014D-E056-0719-45AD3AC1861E'
    vm_unit_number: '5'

  tasks:
    - name: Add disks to virtual machine using UUID
      vmware_guest_disk:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_hostname }}"
        uuid: "{{ vm_uuid | lower }}"
        validate_certs: no
        disk:
          - size_gb: 10
            type: 'thick'
            state: 'present'
            autoselect_datastore: yes
            scsi_controller: '0'
            scsi_type: 'paravirtual'
            unit_number: '5'
            disk_mode: 'independent_persistent'
      delegate_to: localhost

I was trying to find the way to tell it that the second role is not executed in the server but in the vmware virtualizer but with not luck.
If I run the addvmwaredisk.yml as a playbook it runs perfect

Hope you can help!

thanks in advance!

Score:1
th flag

hosts and tasks are play keywords, they cannot be used inside a task list. To execute tasks on a different host than the current play host, use delegation; to group related tasks together and apply directives to all of them, use a block.

- delegate_to: localhost
  vars:
    vcenter_hostname: 'vcenter.labo.local'
    vcenter_username: 'labo.local\ansible'
    vcenter_password: "{{ vault_pass }}"
    target_host: 'TESTHOST'
    vm_uuid: '4217B33E-014D-E056-0719-45AD3AC1861E'
    vm_unit_number: '5'
  block:
    - name: Add disks to virtual machine using UUID
      vmware_guest_disk:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_hostname }}"
        uuid: "{{ vm_uuid | lower }}"
        validate_certs: no
        disk:
          - size_gb: 10
            type: 'thick'
            state: 'present'
            autoselect_datastore: yes
            scsi_controller: '0'
            scsi_type: 'paravirtual'
            unit_number: '5'
            disk_mode: 'independent_persistent'
azk avatar
ph flag
azk
I was trying to implement your solution but it gives me an error: `ERROR! no module/action detected in task. The error appears to be in '/ansible/proyects/extend_fs-role/roles/vmwaretaks/tasks/getvmdiskinfo.yml': line 1, column 3, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - delegate_to: localhost ^ here` Searching for module: `$ ansible-doc -l|grep -i delegate zfs_delegate_admin Manage ZFS delegated administration (user admin privileges)`
azk avatar
ph flag
azk
$ ansible-playbook --version ansible-playbook 2.9.19
flowerysong avatar
th flag
This imples that you have a syntax problem that is not present in the code snippet I provided (it has an action in all of its tasks.) Without seeing your updated code, we have no way of telling you what you did wrong.
Score:0
ph flag
azk

well, I've recently resolv it just adding become: no

- delegate_to: localhost
  become: no
  delegate_facts: yes
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.