简体   繁体   中英

Send data from Google Compute Engine to Google Pub/Sub using Ansible

I would like to send data from Google vm instance to Google Pub/Sub using Ansible.

Ansible playbook

---
- hosts: localhost
  tasks:

  - name: Write into log file
    local_action:
      shell echo "Hello" >> /etc/ansible/log.txt

  - name: Send message through pubsub
    shell: python pub.py project-344421 topic_test
    args:
      chdir: /home/pubsub 

Error

    "msg": "non-zero return code", 
    "rc": 1, 
    "start": "2020-04-14 18:20:00.723265", 
    "stderr": "Traceback (most recent call last):\n  File \"pub.py\", line 22, in <module>\n    from google.cloud import pubsub_v1\nImportError: No module named google.cloud", 
    "stderr_lines": [
        "Traceback (most recent call last):", 
        "  File \"pub.py\", line 22, in <module>", 
        "    from google.cloud import pubsub_v1", 
        "ImportError: No module named google.cloud"
    ], 
    "stdout": "", 
    "stdout_lines": []

Manually, the script pub.py runs properly with python pub.py project-344421 topic_test

Output

der_topic
Published message Hello
 now has message ID 874436722336138
Published 1 message(s).

The modules google.cloud, google-cloud-pubsub and google-auth are installed

pip install --upgrade google-cloud
pip install --upgrade google-cloud-pubsub
pip install --upgrade google-auth 

It is possible that the python executable used by Ansible is different that the one you're using at the command line.

Use the following command to figure out the path to your executable where it works...

$ which python

...then copy/paste the executable's path into your shell command.

  - name: Send message through pubsub
    shell: /<FULL>/<PATH>/python pub.py project-344421 topic_test
    args:
      chdir: /home/pubsub 

If this does not work, then you could also try checking your PYTHONPATH env var to see if it is the same in both your terminal and Ansible contexts.

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