微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法从 Ansible Playbook 中运行 Python 脚本

如何解决无法从 Ansible Playbook 中运行 Python 脚本

我正在尝试编写 ansible playbook 来抓取网站,然后将其内容存储到 aws s3 存储桶下的静态文件中。这是爬虫代码

"""
Handling pages with the Next button

"""
import sys
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup

url =  "https://xyz.co.uk/"
file_name = "web_content.txt"

while True:
    response = requests.get(url)
    soup = BeautifulSoup(response.text,'html.parser')
    raw_html = soup.prettify()
    file = open(file_name,'wb')
    print('Collecting the website contents')
    file.write(raw_html.encode())
    file.close()
    print('Saved to %s' % file_name)
    #print(type(raw_html))

    # Finding next page
    next_page_element = soup.select_one('li.next > a')
    if next_page_element:
        next_page_url = next_page_element.get('href')
        url = urljoin(url,next_page_url)
    else:
        break  

这是我的 ansible-playbook:

---
- name: create s3 bucket and upload static website content into it
  hosts: localhost
  connection: local
  tasks:
  - name: create a s3 bucket
    amazon.aws.aws_s3:
      bucket: testbucket393647914679149
      region: ap-south-1
      mode: create

  - name: create a folder in the bucket
    amazon.aws.aws_s3:
      bucket: testbucket393647914679149
      object: /my/directory/path
      mode: create

  - name: Upgrade pip
    pip:
      name: pip
      version: 21.1.3

  - name: install virtualenv via pip
    pip:
      requirements: /root/ansible/requirements.txt
      virtualenv: /root/ansible/myvenv
      virtualenv_python: python3.6
    environment:
      PATH: "{{ ansible_env.PATH }}:{{ ansible_user_dir }}/.local/bin"

  - name: Run script to crawl the website
    script: /root/ansible/beautiful_crawl.py


  - name: copy file into bucket folder
    amazon.aws.aws_s3:
      bucket: testbucket393647914679149
      object: /my/directory/path/web_content.text
      src: web_content.text
      mode: put

问题是当我运行它时,它运行良好,直到任务 name: install virtualenv via pip 然后在执行任务时抛出以下错误 name: Run script to crawl the website:

致命:[本地主机]:失败! => {“已更改”:true,“msg”:“非零返回码”,“rc”:2,“stderr”:“/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-9798 3643645466 /beautiful_crawl.py: line 1: import: command not found\n/root/.ansible /tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 2: from/root command not found /.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 3 行:导入:未找到命令\n/roo t/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_cra wl.py:第 4 行:来自:未找到命令\n/root/.ansible/tmp/ansible-tmp-18686406386463046304635463 /beautiful_crawl.py:第 6 行:网址:命令未找到根/.ansible/tmp/ansible-t mp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 10 行:意外标记附近的语法错误 ('\n/root/.ansible/tmp/ansible-tmp-1625137700.885430 6-13026-97983643645466/beautiful_crawl.py: line 10: response = requests.get (url)'\n","stderr_lines": ["/root/ /tmp/ansible-tmp-1625137700.8854306-1 3026-97983643645466 / beautiful_crawl.py:第1行:导入:命令未找到 “”/ RO OT / .ansible / TMP / ansible-TMP-1625137700.8854306-13026-97983643645466 / beautiful_cr锥子.py:第 2 行:来自:com mand not found","/root/.ansible/tmp/ansible-tmp-162513 7700.8854306-13026-97983643645466/beautiful_crawl.py: line 3: import: command no t found","/root/.ansible/tmp/ -tmp-1625137700.8854306-13026-9798364364546 6/beautiful_crawl.py: line 4: from: command not found","/root/.ansible/tmp/ansi ble-tmp-1625137736454637700.6653743006548437730065454843773006561377300684374545485 : url: 命令未找到","/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97 983643645466/beautiful_crawl.py:第 7 行:文件名:找不到命令”、“/root/. ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl。 py:第 10 行:意外标记附近的语法错误 ('","/root/.ansible/tmp/ansibl e-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 10: response = requests.get(url)'"],"stdout": "","stdout_lines": []}

在这里做错了什么?

解决方法

您有多个问题。
检查 documentation

没有。 1:默认情况下,script 模块将运行 bash 脚本,而不是 python 脚本。如果要运行python脚本,则需要在脚本的第一行添加像#!/usr/bin/env python3这样的shebang或使用executable参数。

否 2:您创建了一个 venv,所以我假设您想在该 venv 中运行脚本。您无法使用 script 模块开箱即用,因此您需要解决这个问题。

这应该对你有用(你不需要shebang,因为你告诉脚本模块使用executable参数在venv中使用python运行它):

  - name: Run script to crawl the website
    script: /root/ansible/beautiful_crawl.py
      executable: /root/ansible/myvenv/bin/python

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。