3

I am running a playbook from a python script. I am following this code

The following command works perfectly.

ansible -i path/to/inventory.yml host_name -m command -a"a2ensite site_name"

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.

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'),
        ]
    )

It gives the following error.

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": []}

Update I tried running this playbook. This playbook shows the content of "/etc/apache2/sites-available" directory.

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}}')))
        ]
    )

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.

Here is my "hosts inventory file".

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
Amarjit Singh
  • 2,068
  • 19
  • 52
  • You are using a different site name in each (one is `site_name`, the other is `token_server`). Can you test with `-a "a2ensite token_server"` in your ad hoc command, or `args="a2ensite site_name"` in your python code? – Matt P Dec 10 '19 at 20:49
  • @MattP ad-hoc commands work fine. But All the commands that I run using a playbook from the python script, They just get executed on my local – Amarjit Singh Dec 10 '19 at 20:56
  • 2
    Probably need to see more of the python script. The example code you are using from Ansible Docs connects to localhost using `connection: local`. This will need to be modified in your case to manage remote hosts – Matt P Dec 10 '19 at 22:34
  • @MattP I've updated the code and also posted the contents of the inventory file. – Amarjit Singh Dec 11 '19 at 17:25
  • 1
    @AmarjitSingh: what `context.CLIARGS` – 2ps Jan 25 '20 at 00:18

1 Answers1

2

The most likely explanation is that you followed the example a little too closely. The example provided by the docs has the following line:

context.CLIARGS = ImmutableDict(connection='local', 
    module_path=['/to/mymodules'], 
    forks=10, become=None, become_method=None, become_user=None, 
    check=False, diff=False)

That line contains connection='local' which instructs ansible to always connect to localhost regardless of the specified host. Try removing that from your CLIARGS, and your connection should work. Good luck!

2ps
  • 15,099
  • 2
  • 27
  • 47
  • What should I set in the connection? I already set the host in the playbook and it works when I use the script with windows target. – Amarjit Singh Jan 25 '20 at 14:26
  • Thanks, it worked with connection='ssh'. I have another problem with python script. https://stackoverflow.com/questions/59910992/write-ansible-playbooks-using-python-dictionary – Amarjit Singh Jan 25 '20 at 16:10