Home > Blockchain >  Ansible - Concatinating variable give error `can only concatenate list (not \"NoneType\"
Ansible - Concatinating variable give error `can only concatenate list (not \"NoneType\"

Time:07-12

I have three variable files app1.yml, app2.yml and app3.yml having the same variable name viz dbconn

cat app1-webapps-dev.yml
dbconn:
  - host1 port1

cat app2-webapps-dev.yml
dbconn:
  - host4 port4
  - host5 port5

cat app3-webapps-dev.yml
dbconn:

As you can see the variable dbconnin app3.yml is empty. When empty the below ansible play to concat the variable fails with error:

- set_fact:
    dbconn: "{{ dbconn|d([])   (lookup('file', item ~ '-webapps-'   ENV ~ '.yml' )|from_yaml).dbconn }}"
  loop:
    - app1
    - app2
    - app3

Error:

fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ dbconn|d([]) (lookup('file', item ~ '-webapps-' ENV ~ '.yml' )|from_yaml).dbconn }}): can only concatenate list (not "NoneType") to list"}

Can you please suggest?

CodePudding user response:

you can add a condition, as a NoneType equals false

- set_fact:
    dbconn: "{{ dbconn|d([])   (lookup('file', item ~ '-webapps-dev'~ '.yml' )|from_yaml).dbconn  }}"
  when:   ((lookup('file', item ~ '-webapps-dev'~ '.yml' )|from_yaml).dbconn)
  • Related