ansible set_fact无法访问

cak*_*ter 4 ansible ansible-playbook

我有这个ansible剧本

- name: set var small
  set_fact:
    iops_price = 41538
    ram_price_id = 32438
    second_san_price_id = 32926
    os_price_id = 49061
  when: plan|lower == 'small'

- name: aa
  command: echo "{{iops_price}}"
Run Code Online (Sandbox Code Playgroud)

它失败了,因为它说没有定义iops_price,这是输出:

TASK [set var small] ***********************************************************
task path: /home/hanna/proj/db2onc-deploy/db.yml:98
ok: [localhost] => {"ansible_facts": {"_raw_params": "iops_price = 41538  ram_price_id = 32438 second_san_price_id = 32926 os_price_id = 49061"}, "changed": false}

TASK [aa] **********************************************************************
task path: /home/hanna/proj/db2onc-deploy/db.yml:107
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'iops_price' is undefined\n\nThe error appears to have been in '/home/hanna/proj/db2onc-deploy/db.yml': line 107, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n      - name: aa\n        ^ here\n"}
Run Code Online (Sandbox Code Playgroud)

事实证明,我设置的事实可以通过_raw_params访问,这相当于

_raw_params = "iops_price = 41538 ram_price_id = 32438 second_san
_price_id = 32926 os_price_id = 49061"
Run Code Online (Sandbox Code Playgroud)

这不是我想要的,我实际上想设置那些个体变量,任何人都知道为什么会发生这种情况?

小智 5

通常我发现在YAML中,如果字段周围有空格,它不会将'='解析为字段.尝试:

  set_fact:
    iops_price=41538
    ram_price_id=32438
    second_san_price_id=32926
    os_price_id=49061
  when: plan|lower == 'small'
Run Code Online (Sandbox Code Playgroud)

  • 实际上并不是关于YAML.它是一个Ansible能够从原始参数字符串解析`key = value`参数(`=`之间没有空格),你只需使用YAML能力将一条连续线分成多行.如果要以YAML方式指定参数 - 只有一个可能的分隔符 - ":". (2认同)