保持Ansible DRY:如何避免重复变量? [英] Keep Ansible DRY: How to avoid repeating variables?

查看:165
本文介绍了保持Ansible DRY:如何避免重复变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近才刚开始使用Ansible,但是我遇到了一个问题.在我的一个YAML结构中,我定义了以下内容:

Recently just started using Ansible and I have run into a problem. In one of my YAML structures I have defined something like this:

---
# 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从特定任务中给我这样的错误:

Ansible gives me an error like this from a specific task:

在模板字符串中检测到

递归循环: {{jenkins.home}}/updates_jenkins.json"

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

我已将其范围缩小到因bin_path和updates_path都引用"home"这一事实而引起的问题.有没有解决的办法?我需要多次定义'/opt/jenkins'有点糟.

I've narrowed it down to the problem being with the fact bin_path and updates_path both refer to 'home'. Is there a way around this? It kind of sucks that I would need to define '/opt/jenkins' multiple times.

推荐答案

据我所知,这是对jinja2变量的限制.它正在使用旧的$ {}并从1.4开始中断.我不确定他们是否将其固定为1.5.

As far as I know that this is a limitation of jinja2 variables. it was working with the old ${} and broke since 1.4. I am not sure if they fixed that in 1.5.

我的临时解决方案是将房屋从詹金斯"中撤出

My temp solution would be removing home from the "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 DRY:如何避免重复变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆