2

I've spent most of the day trying to solve this problem and have thus far failed. I am building some playbooks to automate functions in Splunk, and am attempting to convert a list of hosts from an inventory group E.G.

[search_head]
1.2.3.4
5.6.7.8

My expected (desired) result from the debug output of the play should be: https://1.2.3.4:8089, https://5.6.7.8:8089

I am attempting to complete this by running the following playbook against a running host:

---
  - name: Build search head list to initialize the captain
    hosts: search_head
    remote_user: ansible
    vars:
      inventory_file: ./inventory-ec2-single-site
      search_head_uri: "{{ lookup('template', './bootstrap-sh-deployer.j2') }}"
pre_tasks:
  - include_vars: 
      dir: 'group_vars'
      extensions:
        - yml
        - yaml
tasks:
  - name: dump array
    debug:
        msg: "{{ search_head_uri }}"`

With the template bootstrap-sh-deployer.j2:

{%- set search_head_uri = [] %}
{% for host in groups['search_head'] %}
    {%- if search_head_uri.append("https://{{ host }}:8089") %} 
{%- endif %}
{%- if not loop.last %}, {% endif -%}
{%- endfor %}

However, the current play returns search_head_uri: ", " which tells me that the loop is running, but {{ host }} is not resolving.

techraf
  • 64,883
  • 27
  • 193
  • 198
AlmostGosu
  • 65
  • 6

2 Answers2

2

Once you open a Jinja2 expression or a statement you should use Jinja2 syntax. You cannot nest them (i.e. you can't use {{ }} inside {% %}).

{%- if search_head_uri.append("https://" + host + ":8089") %}
techraf
  • 64,883
  • 27
  • 193
  • 198
  • Awesome, it stopped erroring out. Now I'm trying to get the `ansible_nodename` from each host in the `inventory` file. `{%- set search_head_uri = [true] %} {% for host in groups['search_head'] %} {%- if search_head_uri.append("https://" + [host][ansible_nodename] + ":8089") %} {% endif -%} {%- if not loop.last %}, {% endif -%} {%- endfor %}` – AlmostGosu Jun 30 '17 at 11:32
  • Errors with: `fatal: [1.2.3.4]: FAILED! => { "failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: {{ lookup('template', './bootstrap-sh-deployer.j2') }}: 'list object' has no attribute u'ip-1-2-3-4'\n\nThe error appears to have been in '/Users/christophergarrett/dev/splunk-ansible/deploy_ec2_testing.yml': line 16, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: dump array\n ^ here\n" }` – AlmostGosu Jun 30 '17 at 11:36
  • If you have a different problem, please post a different question. Before, think about the reason why you enclosed `host` in square brackets. – techraf Jun 30 '17 at 14:04
0

This worked - Combination of the answer above to fix jinja formatting and using hostvars to get to the ansible_nodename.

{%- set search_head_uri = [] %}
{% for host in groups['search_head'] %}
    {{ "https://" + hostvars[host]['ansible_nodename'] + ":8089" }}
    {%- if not loop.last %}, {% endif -%}
{%- endfor %}
AlmostGosu
  • 65
  • 6