Ansible

從角色中的主機執行任務並將其他任務委託給其他角色中的其他主機

  • September 20, 2021

背景:

我有一個有兩個角色的項目。

為了更好地理解,我已將任務數量減少到有問題的任務。

問題:

我從 server1 中的角色 1 執行任務,並嘗試從第一個 server1 角色任務中的條件將任務委託給 vmware 虛擬器。並且有一個主機失敗,因為它想要在 server1 中執行第二個角色任務(Vmware)。

錯誤:

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"}

這是結構:

.
├── 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

這是在主機中找不到空閒 lun 的情況下的執行順序:

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

這是劇本的樣子:

$ 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

我試圖找到方法告訴它第二個角色不是在伺服器中執行,而是在 vmware 虛擬器中執行,但沒有運氣。

如果我將 addvmwaredisk.yml 作為劇本執行,它執行完美

希望您能提供幫助!

提前致謝!

hosts並且tasks是播放關鍵字,它們不能在任務列表中使用。要在與目前播放主機不同的主機上執行任務,請使用委託;要將相關任務組合在一起並將指令應用於所有任務,請使用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'

引用自:https://serverfault.com/questions/1077906