简体   繁体   English

在本地读取文件并在Ansible中使用vars远程

[英]Read a file locally and use the vars remote in Ansible

I read a YAML file locally with the following playbook: 我通过以下剧本在本地阅读了YAML文件:

- name: Ensure the deploy_manifest var is defined and read deploy manifest
  hosts: localhost
  connection: local
  gather_facts: False
  tasks:
    - assert:
        that: deploy_manifest is defined
        msg: |
          Error: Must provide providers config path. Fix: Add '-e deploy_manifest=/path/to/manifest' to the ansible-playbook command

    - name: Read deploy manifest
      include_vars:
        file: "{{ deploy_manifest }}"
        name: manifest
      register: manifest

    - debug:
        msg: "[{{ manifest.key }}]: {{ manifest.value }}"
      with_dict: "{{ manifest.ansible_facts }}"

and then in the same playbook YAML file I run: 然后在同一本Playbook YAML文件中运行:

- name: Deploy Backend services
  hosts: backend
  remote_user: ubuntu
  gather_facts: False
  vars:
    env: "{{ env }}"
    services: "{{ manifest.ansible_facts }}"
  tasks:
    - include_role:
        name: services_backend
      when: backend | default(true) | bool

However it doesn't work because debug fails. 但是,由于debug失败,它不起作用。 It says that manifest is empty. 它说清单是空的。

Which is the best way to read a YAML file or generally a configuration in a playbook and then have the variables passed in another playbook? 哪种方法是读取YAML文件或通常是一本剧本中的配置,然后将变量传递到另一本剧本中的最佳方法?

Your debug module doesn't say " that manifest is empty ", it says the key manifest.key does not exist because it does not. 您的debug模块不会说“ 清单是空的 ”,而是说密钥manifest.key不存在,因为它不存在。

  1. You registered a fact named manifest with: 您使用以下内容注册了一个名为manifest的事实:

     register: manifest 
  2. You try to refer to a key of the above manifest named key and another key (!) named value : 您尝试引用上述manifest名为key和另一个名为value键(!):

     msg: "[{{ manifest.key }}]: {{ manifest.value }}" 

  • Please read Looping over Hashes chapter and acknowledge that (without using loop control ) you refer to the iterated variable using item . 请阅读“ 在哈希表上循环”一章,并确认(不使用循环控制 )您使用item引用了迭代变量。

  • Please note that with name: manifest and register: manifest you read your vars file into manifest.ansible_facts.manifest . 请注意,使用name: manifestregister: manifest将您的vars文件读入manifest.ansible_facts.manifest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM