我在vars/all
目录中有多个变量名相同但值不同的文件。我曾经使用以下命令运行,以实现运行所有这些命令。
$ for var in `ls vars/all/`; do ansible-playbook foo.yaml -e@vars/all/$var; done
PLAY [localhost] **************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [localhost]
TASK [shell] ******************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************
ok: [localhost] => {
"msg": "john"
}
PLAY RECAP ********************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
PLAY [localhost] **************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [localhost]
TASK [shell] ******************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************
ok: [localhost] => {
"msg": "smc"
}
PLAY RECAP ********************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
战术手册如下
---
- hosts: localhost
tasks:
- shell: echo "{{ name }}"
register: print
- debug:
msg: "{{ print.stdout }}"
可变文件
$ cat vars/all/one.yaml
name: john
$ cat vars/all/two.yaml
name: smc
我想自动化它,所以我不必每次都使用for循环。我已经使用include_vars
来解决这个问题,如下所示,但是,它只占用循环中的最后一个文件。
---
- hosts: localhost
tasks:
- name: Include vars
include_vars:
dir: vars/all
- shell: echo "{{ name }}"
register: print
- debug:
msg: "{{ print.stdout }}"
输出
ansible-playbook foo.yaml
PLAY [localhost] **************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [localhost]
TASK [Include vars] ***********************************************************************************************************
ok: [localhost]
TASK [shell] ******************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************
ok: [localhost] => {
"msg": "smc"
}
PLAY RECAP ********************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
我想我正在使用循环,我可以知道有没有一种方法可以在循环中使用相同的变量名,但使用不同的值来执行相同的任务集?
注意:可以创建vars/all/下的更多变量文件,因此不可能使用vars_files
进行硬编码。
将文件包含在循环中,并将变量存储在具有唯一名称的字典中。例如,从文件的名称创建这样的字典名称
- name: Include vars
include_vars:
file: "{{ item }}"
name: "my_vars_name_{{ item|basename|splitext|first }}"
with_fileglob:
- "vars/all/*.yaml"
然后,使用查找varnames来查找字典并迭代列表。使用查找vars获取特定字典的值,并选择属性name
- command:
cmd: "echo {{ lookup('vars', item).name }}"
register: print
with_varnames:
- "my_vars_name_.*"
使用属性结果,因为变量打印已在循环中注册
- debug:
msg: "{{ print.results|map(attribute='stdout')|list }}"
给出
msg:
- john
- smc
测试的完整剧本示例
- hosts: localhost
tasks:
- name: Include vars
include_vars:
file: "{{ item }}"
name: "my_vars_name_{{ item|basename|splitext|first }}"
with_fileglob:
- "vars/all/*.yaml"
- command:
cmd: "echo {{ lookup('vars', item).name }}"
register: print
with_varnames:
- "my_vars_name_.*"
- debug:
msg: "{{ print.results|map(attribute='stdout')|list }}"
给出
PLAY [localhost] *****************************************************************************
TASK [Include vars] **************************************************************************
ok: [localhost] => (item=/export/scratch/tmp8/test-979/vars/all/one.yaml)
ok: [localhost] => (item=/export/scratch/tmp8/test-979/vars/all/two.yaml)
TASK [command] *******************************************************************************
changed: [localhost] => (item=my_vars_name_one)
changed: [localhost] => (item=my_vars_name_two)
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg:
- john
- smc
PLAY RECAP ***********************************************************************************
localhost: ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0