【问题标题】:Ansible: undefined variable when copying file on localAnsible:在本地复制文件时未定义的变量
【发布时间】:2020-03-23 05:37:44
【问题描述】:

我即将制作一个用于自动更新某些库的 ansible playbook

我得到的错误信息:
"msg": "任务包含一个带有未定义变量的选项。 错误是:“lib_wheel_path”未定义\n\n错误似乎是 在“/home/user/Desktop/setup/roles/lib/tasks/main.yml”中:第 4 行, 第 3 列,但可能\n位于文件中的其他位置,具体取决于确切的 语法问题。\n\n有问题的行似乎是:\n\n\n- 名称: 在此处下载 lib\n ^\n"

我的 ansible/vars 文件包含库的路径:

local_path: "~/Some_Libraries/"
lib_wheel_path: "{{ local_path }}lib/"

ansible/role/lib/vars/main.yml 文件包含:

lib_path: "{{ lib_wheel_path }}"
lib_wheels: "'{{ lib_path }}' | regex_findall('(lib\\S*\\.whl)') | sort(reverse=True) }}"

ansible/role/lib/tasks/main.yml 文件包含:

- name: Download lib
  copy:
    src: "{{ lib_path }}/{{ lib_wheels[0] }}"
    dest: /tmp
    remote_src: true

- name: Install lib
  pip:
    name: file:///tmp/{{ lib_wheels[0] }}
  become: yes

预计它将包含 ansible/vars,其中包含 lib_wheel_path,我认为 ansible 的工作原理没有错

【问题讨论】:

    标签: ansible yaml devops


    【解决方案1】:

    您必须包含 var 文件。 例如:如果你的 var 文件是 ansible/role/lib/vars/lib.yml

    local_path: "~/Some_Libraries/"
    lib_wheel_path: "{{ local_path }}lib/"
    

    在你的剧本中包括如下。

    - name: vars
      include_vars: lib.yml
    
    - name: Download lib
      copy:
        src: "{{ lib_path }}/{{ lib_wheels[0] }}"
        dest: /tmp
        remote_src: true
    
    - name: Install lib
      pip:
        name: file:///tmp/{{ lib_wheels[0] }}
      become: yes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2022-11-21
      • 2022-11-18
      相关资源
      最近更新 更多