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

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

问题描述

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

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"

但是当我尝试通过从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":错误:站点令牌服务器不存在!","stderr_lines":[错误:站点令牌服务器不存在!"],"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

推荐答案

最可能的解释是,您对示例的关注过于紧密. docs提供的示例包含以下行:

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天全站免登陆