title: how to check the current host appears in a variable list within ansible?
body: <p>I want to run a microservice role multiple times against hosts to install multiple services. I would also like to use ansible to move services around. This implies that I run against <code>all</code> my hosts and do some logic to check whether the service is installed and whether it should be installed. </p>
  
  <p>I am finding it easy to check whether the service is or isn't installed. What I am doing to check whether the service should be installed is clunky. In my playbook I defined a list of hosts that should have the role installed:</p>
  
  <pre>
  roles:
      - {
        role: "ansible-role-springboot",
        sb_app_name: "microservices-registration",
        sb_app_group_id: "org.springframework.samples.service.service",
        sb_app_artifact_id: "microservices-demo",
        sb_app_version: "2.0.0.RELEASE",
        <b>sb_hosts</b>: [
          "my-test-vm"
        ]
      }
  </pre>
  
  <p>Then I am wanting to test a fact "is the current host in the list?". With my particular test kitchen ansible setup the inventory_hostname is always coming out as <code>localhost</code> even though the vm has the correct hostname. So I am wanting to test the output of the command <code>hostname -f</code> is in var list <code>sb_hosts</code>. This is what I have come up with:</p>
  
  <pre><code>- name: Transfer the script
    copy: src=host_test.sh dest=/home/sbuser mode=0777
  
  - name: Resolve hostname
    command: "/home/sbuser/host_test.sh {{ sb_hosts }}"
    register: hostname_output
  
  - set_fact: 
      hostname_listed="{{ 'HOSTNAME_LISTED' in hostname_output.stdout}}"
  </code></pre>
  
  <p>Where <code>host_test.sh</code> is:</p>
  
  <pre><code>#!/bin/sh
  HOSTNAME=$(hostname -f)
  VARS=$1
  if echo $VARS | grep $HOSTNAME; then
      echo HOSTNAME_LISTED
  else
      echo HOSTNAME_NOT_LISTED
  fi
  </code></pre>
  
  <p>Copying a shell script to do something so basic feels very clunky. Is there a cleaner way to check "is is variable scalar within this variable list"?</p>