简体   繁体   中英

Enable Apache site using Ansible

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM