我是Ansible AWX新手。我正在尝试在剧本的两个任务之间传递变量。该剧本看起来像这样:
---
- name: start
hosts: all
gather_facts: no
tasks:
- name: Check Debian disk space
command: df --output=avail {{ a7_pcap_data_path }}
become: true
become_method: sudo
register: df_output
- name: Show Debian free space
vars:
a7_free_space_mb: "{{ (df_output.stdout_lines[1]|int / 1024)|round(0,'floor') }}"
a7_spare_space_mb: "{{ a7_free_space_mb|int - (a7_capture_volume_mb|int * 1.1) }}" # Allow 10% for safety
debug:
msg: "Spare space: {{ a7_spare_space_mb }} MB"
- name: Pass/fail check
debug:
msg: "Spare space: {{ a7_spare_space_mb }} MB"
a7_pcap_data_path
和a7_capture_volume_mb
作为AWX变量传递。当我从AWX中的“作业模板”运行此文件时,我得到了:
Identity added: /tmp/awx_57_PesTa2/credential_3 (/tmp/awx_57_PesTa2/credential_3)
SSH password:
PLAY [start] *******************************************************************
TASK [Check Debian disk space] *************************************************
changed: [ip-172-31-14-43.eu-west-1.compute.internal]
TASK [Show Debian free space] **************************************************
ok: [ip-172-31-14-43.eu-west-1.compute.internal] => {
"msg": "Spare space: 3020.0 MB"
}
TASK [Pass/fail check] *********************************************************
fatal: [ip-172-31-14-43.eu-west-1.compute.internal]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'a7_spare_space_mb' is undefined\n\nThe error appears to have been in '/var/lib/awx/projects/pre_checks/test.yml': line 21, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Pass/fail check\n ^ here\n"}
PLAY RECAP *********************************************************************
ip-172-31-14-43.eu-west-1.compute.internal : ok=2 changed=1 unreachable=0 failed=1
The variable a7_spare_space_mb obviously is available in the task Show Debian free space but not in the subsequent task.
看来a7_spare_space_mb变量的作用域仅在定义它的任务内,但是据我所读,作用域应该是整个剧本。
我在做什么错了?
感谢和问候...保罗
答案 0 :(得分:0)
一种选择是使用set_fact
- set_fact:
a7_free_space_mb: "{{ (df_output.stdout_lines[1]|int / 1024)|round(0,'floor') }}"
a7_spare_space_mb: "{{ a7_free_space_mb|int - (a7_capture_volume_mb|int * 1.1) }}" # Allow 10% for safety
- name: Show Debian free space
debug:
msg: "Spare space: {{ a7_spare_space_mb }} MB"
- name: Pass/fail check
debug:
msg: "Spare space: {{ a7_spare_space_mb }} MB"