title: Enable Apache site using Ansible
body: <p>I am running a playbook from a python script. I am following <a href="https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html" rel="nofollow noreferrer">this code</a></p>
  
  <p>The following command works perfectly.</p>
  
  <pre><code>ansible -i path/to/inventory.yml host_name -m command -a"a2ensite site_name"
  </code></pre>
  
  <p>But when I try to do the same by executing a playbook from the python script. It says the site doesn't exist. Following is the playbook.</p>
  
  <pre><code>playbook = dict(
          name = "Enable Site",
          hosts = ['token_server'],
          gather_facts = 'no',
          tasks = [
              dict(action=dict(module='command', args="a2ensite " + site_name), register='shell_out'),
              dict(action=dict(module='service', args="name='apache2' state='reloaded'"), register='shell_out'),
          ]
      )
  </code></pre>
  
  <p>It gives the following error.</p>
  
  <blockquote>
    <p>fatal: [token_server]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "cmd": "a2ensite token_server", "delta": "0:00:00.054682", "end": "2019-12-11 01:03:10.546478", "msg": "non-zero return code", "rc": 1, "start": "2019-12-11 01:03:10.491796", "stderr": "ERROR: Site token_server does not exist!", "stderr_lines": ["ERROR: Site token_server does not exist!"], "stdout": "", "stdout_lines": []}</p>
  </blockquote>
  
  <p><strong>Update</strong>
  I tried running this playbook. This playbook shows the content of "/etc/apache2/sites-available" directory.</p>
  
  <pre><code>playbook = dict(
          name = "Enable Site",
          hosts = ['token_server'],
          gather_facts = 'yes',
          tasks = [
              dict(action=dict(module='shell', args='ls /etc/apache2/sites-available'), register='shell_out'),
          dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
          ]
      )
  </code></pre>
  
  <p>It shows the contents of /etc/apache2/sites-available directory on my local.
  It means the command is actually being executed on my local, not on the remote server.</p>
  
  <p>Here is my "hosts inventory file".</p>
  
  <pre><code>all:
    hosts:
      policy_server:
        ansible_host: 155.138.130.72
        ansible_password: XXXXXXXXXX
        ansible_ssh_common_args: -o StrictHostKeyChecking=no
        ansible_user: root
      token_server:
        ansible_host: 155.138.150.239
        ansible_password: XXXXXXXXXX
        ansible_ssh_common_args: -o StrictHostKeyChecking=no
        ansible_user: root
  </code></pre>