Ansible

與 include_tasks 一起使用時 Ansible 處理程序失敗

  • August 14, 2022

這個 ansible 腳本有效。

---
- name: Create Swap
 hosts: serverName
 gather_facts: yes
 become: true
 become_user: root
 become_method: sudo

 tasks:
   - name: Create swap
     shell:
       'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
     when: 
       ansible_facts['memory_mb']['swap']['total'] == 0
     notify: Disable swap

   - name: Collect selected facts
     setup:

 handlers:
   - name: Disable swap
     shell:
       'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
     when: 
       ansible_facts['memory_mb']['swap']['total'] > 500

但是當我在多個單元中打破腳本時。

tree 
.
├── main.yml
├── roles
│   └── swap_creater_560mb.yml

main.yml ==>

---
- name: Create Swap
 hosts: serverName
 gather_facts: yes
 become: true
 become_user: root
 become_method: sudo

 tasks:
   - name: Create swap
     include_tasks: roles/swap_creater_560mb.yml

和 swap_creater_560mb.yml ==>

---
   - name: Create swap
     shell:
       'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
     when: 
       ansible_facts['memory_mb']['swap']['total'] == 0
     notify: Disable swap

   - name: Collect selected facts
     setup:

 handlers:
   - name: Disable swap
     shell:
       'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
     when: 
       ansible_facts['memory_mb']['swap']['total'] > 500

每當我創建交換時,我想通知處理程序禁用交換。但是當我include_tasks:roles/swap_creater_560mb.yml 我得到以下錯誤。

任務

$$ Create swap $$****************************************************** ****************************************************** *****致命的:$$ serverName $$: 失敗的!=> {“reason”: “我們無法讀取 JSON 或 YAML,以下是我們從每個中得到的錯誤:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n did not找到預期的\n\n錯誤似乎在“/home/USER/testing_scripts/swap_creator/roles/swap_creater_560mb.yml”中:第 15 行,第 3 列,但可能\n在文件中的其他位置,具體取決於確切的語法問題。\ n\n違規行似乎是:\n\n\n handlers:\n ^ here\n”} 誰能幫助我或指導我做錯了什麼?我是ansible的新手,我可能會遺漏一些東西。

handlers只能在劇本(或handlers角色的目錄)中設置。不能在 包含的任務列表中設置include_tasks。如果您想將這些任務和處理程序分解到單獨的文件中,請創建一個實際的 ansible 角色。

佈局看起來像:

main.yml
roles/
 swap_creater_560mb/
   tasks/
     main.yml
   handlers/
     main.yml

哪裡tasks/main.yml有:

- name: Create swap
 shell:
   'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
 when: 
   ansible_facts['memory_mb']['swap']['total'] == 0
 notify: Disable swap

- name: Collect selected facts
 setup:

並且handlers/main.yml有:

- name: Disable swap
 shell:
   'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
 when: 
   ansible_facts['memory_mb']['swap']['total'] > 500

然後在你的主要劇本中:

- name: Create Swap
 hosts: serverName
 gather_facts: yes
 become: true
 become_user: root
 become_method: sudo

 tasks:
   - name: Create swap
     include_role:
       name: swap_creater_560mb

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