【问题标题】:ansible Task separation via Variables must be stored as a dictionary/hashansible 通过变量分离任务必须存储为字典/散列
【发布时间】:2020-08-04 21:21:46
【问题描述】:
- name: gather os specific variables
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
    - "{{ ansible_distribution }}.yml"
  tags: vars

尝试设置多 linux 发行版 ansible 剧本我确实使用了官方 ansible 文档中建议的剧本 https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/variables/

我确实添加了 Centos-6.yml 和 Debian-9.yml 进行测试,但结果如下

失败:[1.1.1.1] (item=/home/ansible/ansible-scripts/2AllInOne/CentOS-6.yml) => { “ansible_facts”:{}, “ansible_included_var_files”:[], “ansible_loop_var”:“项目”, “改变”:错误, "项目": "/home/ansible/ansible-scripts/2AllInOne/CentOS-6.yml", "message": "/home/ansible/ansible-scripts/2AllInOne/CentOS-6.yml 必须存储为字典/哈希"

有什么建议吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    问:CentOS-6.yml 必须存储为字典/散列。可能是什么问题?

    答:问题在于文件CentOS-6.yml 的格式。它必须是 valid YAML 并且必须包含 dictionary/hash aka mapping。例如

    shell> cat CentOS-6.yml
    my_dictionary:
      my_distro: Centos
      my_major: 6
      my_file: CentOS-6.yml
    

    【讨论】:

    • 谢谢弗拉基米尔,我确实添加了它,但是当我添加命令时它确实显示问题:“我们无法读取 JSON 或 YAML”my_dictionary: my_distro: Centos my_major: 6 my_file: CentOS-6.yml - name: install the latest version of Htop yum: name: htop state: latest
    【解决方案2】:

    修改yml后问题解决如下:

    1 ---  
    2 - User_pass: varfileinclude
    

    到

    1 ---
    
    2 User_pass: varfileinclude
    
    # removed "-" from the variable. 
    

    【讨论】:

    • 这确实是一个关键的变化。也许值得补充的是,前导破折号 - 使条目成为向量的一部分,而删除它会创建一个 dict 条目。
    猜你喜欢
    • 2023-02-15
    • 1970-01-01
    • 2015-01-08
    • 2016-04-17
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多