JPNagarajan
JPNagarajan

Reputation: 928

how to concatenate string and variable with in with_items in ansible?

I want to install pkgs based on the application name which passed as a variable

**My vars.yml file:**

jsc_pkgs: [wget,telnet,nginx]

**My playbook:**

---
- hosts: all
  become: yes
  gather_facts: true
  vars_files:
  - ./vars.yml
  tasks:
  - name: Install required packages for jsc application
    dnf:
     name: "{{ item }}"
     state: latest
    with_items:
    - "{{ app + '_pkgs' }}"
    when: app  is defined

Executable command :

ansible-playbook -i inventory ecom-config-playbook.yml -e "app=jsc"

The above code didn't worked.Is it possible to append a string and var like this? or how to approach this?

Upvotes: 1

Views: 1050

Answers (1)

JPNagarajan
JPNagarajan

Reputation: 928

The below code worked

---
- hosts: all
  become: yes
  gather_facts: true
  vars_files:
  - ./vars.yml
  tasks:
  - name: Install required packages for jsc application
    dnf:
     name: "{{ item }}"
     state: latest
    with_items:
    - "{{ vars[app + '_pkgs'] }}"
    when: app  is defined

Upvotes: 1

Related Questions