2016-03-11 39 views
1

我正在嘗試動態引用從artifactory中提取的j2模板。 下面的代碼工作:試圖動態引用jinja模板在劇本中

- name: Confiure the sqoop templates 
    template: src="{{ item }}" dest="{{ local_root }}/{{ FEED_NAME }}/{{ FEED_SCHEMA }}/sqoopgen_jobs/{{ item|replace(\"/tmp/tardis/\", \"\") }}" mode=0744 
    with_fileglob: 
    - "/tmp/{{ FEED_NAME }}/*.j2" 

下列不:

- name: Confiure the sqoop templates 
    template: src="{{ item }}" dest="{{ local_root }}/{{ FEED_NAME }}/{{ FEED_SCHEMA }}/sqoopgen_jobs/{{ item|replace(\"/tmp/{{ FEED_NAME }}/\", \"\") }}" mode=0744 
    with_fileglob: 
    - "/tmp/{{ FEED_NAME }}/*.j2" 

與錯誤而失敗:

failed: [host_ip] => (item=/tmp/tardis/forecast.j2) => {"changed": true, "failed": true, "invocation": {"module_args": {"backup": false, "content": null, "delimiter": null, "dest": "/path/tardis/tardis/sqoopgen_jobs//tmp/tardis/forecast.j2", "directory_mode": null, "follow": true, "force": true, "group": null, "mode": "0744", "original_basename": "forecast.j2", "owner": null, "regexp": null, "remote_src": null, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": "/home/jenkins/.ansible/tmp/ansible-tmp-1457719702.84-279730849155325/source", "validate": null}}, "item": "/tmp/tardis/forecast.j2", "msg": "Destination directory /path/tardis/tardis/sqoopgen_jobs//tmp/tardis does not exist"}

我讀這是ansible的情況下,不擴張變量在一個變量內:such as here

什麼是最好的方法採取這種情況?我想保持劇本儘可能通用。

ansible 2.0.1.0

感謝

回答

1

不能嵌套神社表情就像這樣:

{{ item|replace("/tmp/{{ FEED_NAME }}/", "") }} 

你想要的是連接字符串。

{{ item|replace("/tmp/" ~ FEED_NAME ~ "/", "") }} 

"Other Operators" in the Jinja2 docs

~

Converts all operands into strings and concatenates them.

{{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John! .

- name: Configure the sqoop templates 
    template: 
     src: "{{ item }}" 
     dest: "{{ local_root }}/{{ FEED_NAME }}/{{ FEED_SCHEMA }}/sqoopgen_jobs/{{ item|replace('/tmp/' ~ FEED_NAME ~ '/', '') }}" 
     mode: 0744 
    with_fileglob: 
     - "/tmp/{{ FEED_NAME }}/*.j2" 
+0

這是偉大的感謝 – bulmanp