0

Hi I have a YAML file that looks like this -

- name: Name 1
  url: http://localhost:8080/page1
- name: Name 2
  url: http://localhost:8080/page2
- name: Name 3
  url: http://localhost:8080/page3

As you can see that http://localhost:8080/ is a common section of the url property. Is there a way to declare a variable like hostname and reuse it across the file?

I went through this doc and tried the following but it doesnt work -

- hostname: "{{http://localhost:8080}}"
- name: Name 1
  url: {{hostname}}/page1
- name: Name 2
  url: {{hostname}}/page2
- name: Name 3
  url: {{hostname}}/page3

But this doesnt work. Help!

Archit Arora
  • 2,508
  • 7
  • 43
  • 69

2 Answers2

1

For example, the playbook

- hosts: localhost
  vars:
    hostname: "http://localhost:8080"
    l1:
      - name: Name 1
        url: "{{ hostname }}/page1"
      - name: Name 2
        url: "{{ hostname }}/page2"
      - name: Name 3
        url: "{{ hostname }}/page3"
  tasks:
    - debug:
        var: l1

gives

ok: [localhost] => 
  l1:
  - name: Name 1
    url: http://localhost:8080/page1
  - name: Name 2
    url: http://localhost:8080/page2
  - name: Name 3
    url: http://localhost:8080/page3

When you put the items of the list into a file, e.g.

shell> cat name-url.yml
- name: Name 1
  url: "{{ hostname }}/page1"
- name: Name 2
  url: "{{ hostname }}/page2"
- name: Name 3
  url: "{{ hostname }}/page3"

use lookup plugin template and convert the YAML string to a list, e.g. the playbook below gives the same result

- hosts: localhost
  vars:
    hostname: "http://localhost:8080"
  tasks:
    - set_fact:
        l1: "{{ lookup('template', 'name-url.yml')|from_yaml }}"
    - debug:
        var: l1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

I wanted to toss this suggestion in, you it looks like you're doing this for the base URL of the site you're snapshotting. You can pass a --base-url flag to the snapshot command:

$ npx percy snapshot --base-url http://localhost:8080 snapshots.yml

snapshots.yml could be:

- name: Name 1
  url: /page1
- name: Name 2
  url: /page2
- name: Name 3
  url: /page3

## or

- url: /page1
- url: /page2
- url: /page3

Relevant Percy CLI PR

robdel12
  • 229
  • 2
  • 8