title: How to use Ansible command with items?
body: <p>I want to configure my Ansible playbook to copy certain lines out of my <code>/etc/hosts</code> file into a temporary file. This should be easy to do:</p>
  
  <pre><code>---
  hosts: 127.0.0.1
  gather_facts: False
  tasks:
    - command: grep {{ item }} /etc/hosts &gt;&gt; /tmp/hosts_to_backup
      with_items:
        - web
        - database
  </code></pre>
  
  <p>I'd think this would work but I'm getting an error:</p>
  
  <blockquote>
    <p>TypeError: string indicies must be integers, not str</p>
  </blockquote>
  
  <p>I know Ansible is picky about unquoted braces so I put double quotes around the entire command line but I still get the error.</p>
  
  <pre><code>- command: "grep {{ item }} /etc/hosts &gt;&gt; /tmp/hosts_to_backup"
  </code></pre>