Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Ansible DRY: How to avoid repeating variables?

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 gives me an error like this from a specific task:

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

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.

like image 537
Philip Lombardi Avatar asked Feb 25 '14 22:02

Philip Lombardi


People also ask

How do you manage variables in Ansible?

Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries.

How do you pass extra vars in Ansible Tower?

To able to set extra_vars on schedules, you must select Prompt on Launch for EXTRA VARIABLES on the job template, or a enable a survey on the job template, then those answered survey questions become extra_vars .

What is Hostvars Ansible?

With hostvars , you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using the hostvars variable too, but only after you have gathered (or cached) facts.


1 Answers

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'
like image 62
DomaNitro Avatar answered Oct 13 '22 11:10

DomaNitro