通过将变量提供给剧本从dict访问变量

时间:2019-01-31 12:52:26

标签: ansible

我有以下格言:

webpaket:
  "001":
    projekt: "Test"
    domain: "testdomain.de"
    subdomain: "test"
    fqdn: "test.testdomain.de"
    serveradmin: "webmaster@testdomain.de"
    ip: "*"
    ssl: "true"
    password: "somepass"
    vhost: ""
    symfony: "true"
    localdb: "true"
    clusterdb: "false"
    localdb_pgsql: "false"

这是剧本:

---
# file: webpaket-remove.yml
- hosts: all
  vars:
    removeweb: "{{ webpaket[webid] }}"
  roles:
    - { role: webpaket-remove, stage: 'dev' }

这是任务:

---
    - name: Delete Web-Directory for {{ webid }}
      file: path=/test/{{ item.value }} state=absent
      with_dict: "{{removeweb}}"

Id喜欢通过给ansible剧本一个参数来访问变量,例如:

ansible-playbook -l testserver.de --check --diff webpaket-remove.yml --extra-vars "webid=001"

当我运行它时,我得到以下输出:

ok: [testserver.de] => (item={'key': 'projekt', 'value': 'Test'})
ok: [testserver.de] => (item={'key': 'domain', 'value': 'testdomain.de'})

如何访问键值?

如果我使用item.value.projekt我得到: "The task includes an option with an undefined variable. The error was: 'projekt' is undefined

item [projekt]也确实起作用,因为这里的想法是:/

1 个答案:

答案 0 :(得分:1)

removeweb 不是目录。 projekt 应该直接处理

- file:
    path: "/test/{{ removeweb['projekt'] }}"
    state: absent