ansible连接条件中的字符串和变量

时间:2017-05-17 03:32:30

标签: ansible

我无法弄清楚如何在条件中加入字符串和{{item}}。我不确定这是否可行或支持

这是我的任务块:

- name: enable repos
  command: "subscription-manager repos --enable {{ item }}"
  when: enable_repo_{{ item }} == 'yes'
  with_items:
    - rhel-7-server-rpms
    - rhel-7-server-optional-rpms
    - rhel-7-server-extras-rpms
    - rhel-7-server-satellite-tools-6.2-rpms
    - rhel-7-server-thirdparty-oracle-java-rpms
    - jws-3-for-rhel-7-server-rpms

我得到的错误是:

TASK [satellite_client : enable repos] *****************************************
fatal: [10.187.15.31]: FAILED! => {"failed": true, "msg": "The conditional check 'enable_repo_item == 'yes'' failed. The error was: error while evaluating conditional (enable_repo_item == 'yes'): 'enable_repo_item' is undefined\n\nThe error appears to have been in '/home/marcp/git/satellite_client/tasks/main.yml': line 21, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: enable repos\n  ^ here\n"}

PLAY RECAP *********************************************************************
10.187.15.31               : ok=1    changed=0    unreachable=0    failed=1

如何将字符串与{{item}}连接起来,还是应该以不同的方式进行此操作?

1 个答案:

答案 0 :(得分:2)

{{ ... }}中的条件应该是Jinja2表达式(因此您可以认为它隐含在vars中,因此无法打开另一个表达内部的Jinja2。)

您可以使用when: vars['enable_repo_' + item] == 'yes' 字典并将变量名称作为连接字符串传递:

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
  document.body.removeChild(element);
}