Keep Ansible DRY:如何避免重复变量?

时间:2014-02-25 22:47:45

标签: ansible ansible-playbook

最近刚开始使用Ansible,我遇到了一个问题。在我的一个YAML结构中,我定义了类似的东西:

---
# file: main.yml
#
# Jenkins variables for installation and configuration

jenkins:
  debian:
    # Debian repository containing Jenkins and the associated key for accessing the repository
    repo: 'deb http://pkg.jenkins-ci.org/debian binary/'
    repo_key: 'http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key'

    # Dependencies needed for Jenkins to run properly
    dependencies:
      - 'openjdk-7-jdk'
      - 'git-core'
      - 'curl'

  port: 8080

  # Installation directory
  home: '/opt/jenkins'

  # Path to the location of the main Jenkins-cli binary
  bin_path: '{{jenkins.home}}/jenkins-cli.jar'

  # Path to the location of the Jenkins updates file
  updates_path: '{{jenkins.home}}/updates_jenkins.json'

Ansible在特定任务中给出了这样的错误:

  

“在模板字符串中检测到递归循环:   {{jenkins.home}} / updates_jenkins.json“

我已经把它缩小到了事实,因为bin_path和updates_path都引用了'home'。有没有解决的办法?有点糟糕,我需要多次定义'/ opt / jenkins'。

2 个答案:

答案 0 :(得分:4)

据我所知,这是jinja2变量的限制。它正在使用旧的$ {}并且自1.4以来就破产了。我不确定他们是否在1.5中确定了这一点。

我的临时解决方案是从“詹金斯”中删除家园

---
# file: main.yml
#
# Jenkins variables for installation and configuration

# Installation directory
jenkins_home: '/opt/jenkins'
jenkins:
  debian:
    # Debian repository containing Jenkins and the associated key for accessing the repository
    repo: 'deb http://pkg.jenkins-ci.org/debian binary/'
    repo_key: 'http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key'

    # Dependencies needed for Jenkins to run properly
    dependencies:
      - 'openjdk-7-jdk'
      - 'git-core'
      - 'curl'

  port: 8080



  # Path to the location of the main Jenkins-cli binary
  bin_path: '{{jenkins_home}}/jenkins-cli.jar'

  # Path to the location of the Jenkins updates file
  updates_path: '{{jenkins_home}}/updates_jenkins.json'

答案 1 :(得分:0)

这应该这样做:

bin_path: "{{ jenkins['home'] }}/jenkins-cli.jar"