使用 Ansible 启用 Apache 站点 [英] Enable Apache site using Ansible

查看:47
本文介绍了使用 Ansible 启用 Apache 站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 python 脚本运行剧本.我正在关注此代码

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

以下命令完美运行.

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

但是当我尝试通过从 python 脚本执行剧本来做同样的事情时.它说该网站不存在.以下是剧本.

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

它给出了以下错误.

致命:[token_server]:失败!=> {"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": "非零返回码", "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": []}

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

更新我试着运行这个剧本.此剧本显示/etc/apache2/sites-available"目录的内容.

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

它显示了我本地的/etc/apache2/sites-available 目录的内容.这意味着命令实际上是在我的本地执行,而不是在远程服务器上执行.

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)

该行包含 connection='local',它指示 ansible 始终连接到 localhost,而不考虑指定的主机.尝试从您的 CLIARGS 中删除它,您的连接应该可以正常工作.祝你好运!

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!

这篇关于使用 Ansible 启用 Apache 站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆