[英]Hello World in Ansible + Python is not working
我正在開始使用python為ansible編寫自定義模塊。 我使用了像這樣的教程https://medium.com/@heenashree2010/create-a-custom-module-with-ansible-python-6285874a09b4
main.yaml
- hosts: all
remote_user: root
gather_facts: yes
vars_prompt:
- name: giveName
prompt: "Please provide your name"
private: no
failed_when: giveName is undefined
tasks:
- name: Python Execution
testing: yourName={{ giveName }}
register: result
- debug: var=result
testing.py
#! /usr/bin/env python
from ansible.module_utils.basic import *
import os, json
import re, sys
def firstProg(text):
text1 = "Hello " + text
return text1
if __name__ == '__main__':
fields = {
"yourName": {"required": True, "type": "str"}
}
module = AnsibleModule(argument_spec=fields)
yourName = os.path.expanduser(module.params['yourName'])
newName = firstProg(yourName)
module.exit_json(msg=newName)
當我嘗試執行ansible劇本時,出現以下錯誤:
[ebeid@localhost hello-world-custom-module]$ ansible-playbook main.yaml -vvv
ansible-playbook 2.8.4
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/ebeid/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/ebeid/env/lib/python2.7/site-packages/ansible
executable location = /home/ebeid/env/bin/ansible-playbook
python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAYBOOK: main.yaml ************************************************************************************************************************
1 plays in main.yaml
Please provide your name: ebeid
PLAY [localhost] ***************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
task path: /home/ebeid/ansible-sandbox/hello-world-custom-module/main.yaml:1
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ebeid
<127.0.0.1> EXEC /bin/sh -c 'echo ~ebeid && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894 `" && echo ansible-tmp-1567725618.58-95756223105894="` echo /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894 `" ) && sleep 0'
Using module file /home/ebeid/env/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /home/ebeid/.ansible/tmp/ansible-local-32250T4H7Md/tmpnuPOdr TO /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894/ /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/home/ebeid/env/bin/python /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ebeid/.ansible/tmp/ansible-tmp-1567725618.58-95756223105894/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers
TASK [Python Execution] ********************************************************************************************************************
task path: /home/ebeid/ansible-sandbox/hello-world-custom-module/main.yaml:10
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ebeid
<127.0.0.1> EXEC /bin/sh -c 'echo ~ebeid && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142 `" && echo ansible-tmp-1567725620.8-68546874702142="` echo /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142 `" ) && sleep 0'
Using module file /home/ebeid/ansible-sandbox/hello-world-custom-module/library/testing.py
<127.0.0.1> PUT /home/ebeid/ansible-sandbox/hello-world-custom-module/library/testing.py TO /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/AnsiballZ_testing.py
<127.0.0.1> PUT /home/ebeid/.ansible/tmp/ansible-local-32250T4H7Md/tmpfsVxQ3 TO /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/args
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/ /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/AnsiballZ_testing.py /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/args && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/AnsiballZ_testing.py /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/args && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: /home/ebeid/.ansible/tmp/ansible-tmp-1567725620.8-68546874702142/AnsiballZ_testing.py: cannot execute binary file\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 126
}
PLAY RECAP *********************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
似乎是python文件無法識別為可執行文件。 任何想法如何解決?
如模塊格式和文檔指南中所述 ,用python編寫的ansible模塊必須以#!/usr/bin/python
開頭,而您的以#! /usr/bin/env python
開頭#! /usr/bin/env python
#! /usr/bin/env python
您可以看到_get_shebang
用於嗅探解釋器並將其替換為自己的值(傳統上ansible_python_interpreter
的值,這是一個發現的事實,或者您也可以在劇本/臨時運行中設置它)
他們聲稱第二行必須是# -*- coding: utf-8 -*-
但我實際上不記得曾經這樣做過,因此您的經驗可能會有所不同。 我建議按照他們所說的去做,直到您確定它確實有效為止,然后再決定是否保留它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.