0


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!

azk
  • 39
  • 9

2 Answers2

1

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'
flowerysong
  • 901
  • 4
  • 6
  • 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 Sep 20 '21 at 17:42
  • $ ansible-playbook --version ansible-playbook 2.9.19 – azk Sep 20 '21 at 17:43
  • 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. – flowerysong Sep 21 '21 at 00:12
0

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

- delegate_to: localhost
  become: no
  delegate_facts: yes
azk
  • 39
  • 9