ansible - 保持 Ansible DRY : How to avoid repeating variables?

标签 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 在特定任务中给了我这样的错误:

"recursive loop detected in template string: {{jenkins.home}}/updates_jenkins.json"



我已经将问题缩小到 bin_path 和 updates_path 都指的是“家”这一事实。有没有解决的办法?我需要多次定义“/opt/jenkins”,这有点糟糕。

最佳答案

据我所知,这是 jinja2 变量的限制。它使用旧的 ${} 并且从 1.4 开始就坏了。我不确定他们是否在 1.5 中修复了这个问题。

我的临时解决方案是从“ Jenkins ”中移除家

---
# 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'

关于ansible - 保持 Ansible DRY : How to avoid repeating variables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027902/

相关文章:

ruby-on-rails - 如何使用 Ansible 检查文件中是否存在行?

deprecated - Ansible 1.6 include with_items已弃用

Ansible:读取远程文件

elasticsearch - 重启vagrant box后如何自动启动服务?

java - apt_repository 模块失败

elasticsearch - Elasticsearch重新启动任务在Ansible Playbook中挂断

ansible - 如何从包含 YAML 的字符串中读取 Ansible 变量?

ansible - 当前从Terraform创建Ansible库存的最佳方法

ssl - Ansible,如何信任证书?

ansible - 无论传入什么标签,如何执行预任务?