[英]Enable Apache site using Ansible
I am running a playbook from a python script.我正在从 python 脚本运行剧本。 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.但是当我尝试通过从 python 脚本执行剧本来做同样的事情时。 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!
致命:[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": "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": []}
=> {"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": "错误: 站点 token_server 不存在!", "stderr_lines": ["错误: 站点 token_server 不存在!"], "stdout": "", "stdout_lines": []}
Update I tried running this playbook.更新我尝试运行此剧本。 This playbook shows the content of "/etc/apache2/sites-available" directory.
该剧本显示了“/etc/apache2/sites-available”目录的内容。
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.它显示了我本地的 /etc/apache2/sites-available 目录的内容。 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.该行包含
connection='local'
指示 ansible 始终连接到 localhost 而不管指定的主机。 Try removing that from your CLIARGS, and your connection should work.尝试从您的 CLIARGS 中删除它,您的连接应该可以正常工作。 Good luck!
祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.