1.3.12: playbook with_items and register



0. 背景说明

变量example

# all组变量文件中的变量
cdn_proxy_nginx:
  - ip1
  - ip2

# 子组的变量文件中的变量
ipset_web:
  cdn_nginx: "{{ cdn_proxy_nginx }}"
  "{{ brand }}_nginx":
    - ip3
    - ip4

ipset_web本身是一个变量,如果要获得ip3和ip4的话,需要使用key -> "{{ brand }}_nginx",这样就要面临变量中使用变量的情况

1. 变量中如何使用变量

- name: ipset add ip to group "{{ brand }}_nginx"
  command: ipset add "{{brand}}_nginx" "{{ item }}"
  with_items: "{{ ipset_web['{{ brand }}_nginx'] }}"
  ignore_errors: yes

重点是"{{ ipset_web['{{ brand }}_nginx'] }}"参考链接