-1

Error when passing variable

  ansible.builtin.set_fact:
    apache_status: "{{ service_status['ansible_facts']['services']['{{ web_pkg }}.service']['state'] }}"

Below working fine

- name: Fetch Apache Service
  ansible.builtin.set_fact:
    apache_status: "{{ service_status['ansible_facts']['services']['httpd.service']['state'] }}"
U880D
  • 8,601
  • 6
  • 24
  • 40
UME
  • 323
  • 1
  • 2
  • 7
  • 1
    [moustaches don't stack](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names) – Zeitounator Aug 16 '23 at 12:38

1 Answers1

1

Because of your description it is assumed that you are gathering service_facts.

So the syntax could be simplified in example

- name: Show service state
  debug:
    msg: "{{ ansible_facts.services[web_pkg + '.service'].state }}"

Full minimal playbook for testing

---
- hosts: test
  become: true
  gather_facts: false

  vars:

    web_pkg: 'nginx'

  tasks:

  - service_facts:

  - name: Show Service
    debug:
      msg:
        - "{{ ansible_facts.services[web_pkg ~ '.service'].state }}"
        - "{{ ansible_facts.services[web_pkg ~ '.service'].status }}"

resulting into an output of

TASK [Show Service] *****
ok: [test.example.com] =>
  msg:
  - inactive
  - disabled

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40