无法使用ansible下载golang存储库。

huangapple go评论124阅读模式
英文:

Unable to download golang repository by using ansible

问题

我正在尝试从GitHub下载一个golang包。这是我的playbook的样子:

  • name: 获取最新的gogs存储库
    shell: "go get -u github.com/gogits/gogs"
    become: true
    become_user: git

它给我抛出以下错误:

{
"changed": true,
"cmd": "go get -u github.com/gogits/gogs",
"delta": "0:00:00.002695",
"end": "2017-08-22 10:50:02.984669",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "go get -u github.com/gogits/gogs",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"rc": 127,
"start": "2017-08-22 10:50:02.981974",
"stderr": "/bin/sh: go: command not found",
"stderr_lines": [
"/bin/sh: go: command not found"
],
"stdout": "",
"stdout_lines": []
}

当我尝试这样做时:

  • name: 获取最新的gogs存储库
    shell: "go get -u {{ gogs_repo }}"
    environment:

    • PATH: $PATH:/usr/local/go/bin:/usr/bin
    • GOPATH: "{{gogs_home}}/{{ gogs_project_directory }}/src"
    • GOBIN: "{{gogs_home}}/{{ gogs_project_directory }}/bin"
      become: true
      become_user: git

我得到了这个错误:

fatal: [atul-ec2]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Shared connection to ec2-13-126-203-235.ap-south-1.compute.amazonaws.com closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File "/tmp/ansible_gntmXa/ansible_module_command.py", line 220, in \r\n main()\r\n File "/tmp/ansible_gntmXa/ansible_module_command.py", line 163, in main\r\n os.chdir(chdir)\r\nOSError: [Errno 13] Permission denied: '/home/ec2-user/goprojects/src/src/github.com/gogits/gogs'\r\n",
"msg": "MODULE FAILURE",
"rc": 1
}

这里是我的变量:


go_version: go1.7.linux-amd64.tar.gz
go_url: https://storage.googleapis.com/golang/{{ go_version }}
go_hash: sha256:702ad90f705365227e902b42d91dd1a40e48ca7f67a2f4b2fd052aaa4295cd95
go_project_dir: goprojects
go_home: "{{ ansible_env.HOME }}"
gogs_home: "/home/git"
gogs_project_directory: "git.varadev.com"
gogs_repo: github.com/gogits/gogs

但是当我在服务器上使用以下命令时:

which go

我得到了这个结果:

/usr/local/go/bin/go

当我手动尝试go get -u github.com/gogits/gogs时,它正常工作。

英文:

I am trying to download a golang package from github. This is how my playbook looks like

- name: Fetch latest gogs repository
      shell: "go get -u github.com/gogits/gogs"
      become: true
      become_user: git

It is throwing me following error:

{
    "changed": true, 
    "cmd": "go get -u github.com/gogits/gogs", 
    "delta": "0:00:00.002695", 
    "end": "2017-08-22 10:50:02.984669", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "_raw_params": "go get -u github.com/gogits/gogs", 
            "_uses_shell": true, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "warn": true
        }
    }, 
    "rc": 127, 
    "start": "2017-08-22 10:50:02.981974", 
    "stderr": "/bin/sh: go: command not found", 
    "stderr_lines": [
        "/bin/sh: go: command not found"
    ], 
    "stdout": "", 
    "stdout_lines": []
}

When I am trying this

- name: Fetch latest gogs repository
      shell: "go get -u {{ gogs_repo }}"
      environment:
        - PATH: $PATH:/usr/local/go/bin:/usr/bin
        - GOPATH: "{{gogs_home}}/{{ gogs_project_directory }}/src"
        - GOBIN:  "{{gogs_home}}/{{ gogs_project_directory }}/bin"
      become: true
      become_user: git

I got this error

fatal: [atul-ec2]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Shared connection to ec2-13-126-203-235.ap-south-1.compute.amazonaws.com closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_gntmXa/ansible_module_command.py\", line 220, in <module>\r\n    main()\r\n  File \"/tmp/ansible_gntmXa/ansible_module_command.py\", line 163, in main\r\n    os.chdir(chdir)\r\nOSError: [Errno 13] Permission denied: '/home/ec2-user/goprojects/src/src/github.com/gogits/gogs'\r\n", 
    "msg": "MODULE FAILURE", 
    "rc": 1
}

Here my variables are

---
  go_version: go1.7.linux-amd64.tar.gz
  go_url: https://storage.googleapis.com/golang/{{ go_version }}
  go_hash: sha256:702ad90f705365227e902b42d91dd1a40e48ca7f67a2f4b2fd052aaa4295cd95
  go_project_dir: goprojects
  go_home: "{{ ansible_env.HOME }}"
  gogs_home: "/home/git"
  gogs_project_directory: "git.varadev.com"
  gogs_repo: github.com/gogits/gogs

But when i am using following command on my server

which go

I got this

/usr/local/go/bin/go

and when I try manually go get -u github.com/gogits/gogs, it is working fine.

答案1

得分: 4

希望这可以作为一个起点帮助你:

<!-- language: yaml -->


  • hosts: all
    connection: local

    tasks:

    • name: 检查 go 版本
      command: go version
      register: result
      changed_when: no
      ignore_errors: true

    • set_fact:
      go_path: "{{ lookup('env', 'GOPATH') | default(ansible_env.HOME+'/go', true) }}"
      when: not result|failed

    • name: 安装 gogs
      shell: go get -u github.com/gogits/gogs
      environment:
      GOPATH: "{{ go_path }}"
      register: gogs
      when: not result|failed

    • debug: var=gogs

尝试在远程服务器上运行以下命令:

ansible-playbook gogs.yml -i localhost

如果这样可以工作,那么稍后再尝试远程执行。

通常情况下,你不希望这样做,因为你想通过 SSH 远程执行,但由于你迄今为止尝试并且遇到了一些错误,可能通过在本地使用 connection: local 可以帮助更详细地调试这个问题。

英文:

Hope this can help you as a start point:

<!-- language: yaml -->

---
- hosts: all
  connection: local 

  tasks:
  - name: check go version
    command: go version
    register: result
    changed_when: no
    ignore_errors: true

  - set_fact:
      go_path: &quot;{{ lookup(&#39;env&#39;, &#39;GOPATH&#39;) | default(ansible_env.HOME+&#39;/go&#39;, true) }}&quot;
    when: not result|failed

  - name: go get gogs
    shell: go get -u github.com/gogits/gogs
    environment:
      GOPATH: &quot;{{ go_path }}&quot;
    register: gogs
    when: not result|failed

  - debug: var=gogs

Try to run this on your remote server by typing:

ansible-playbook gogs.yml -i localhost,

If that works then just later try remotely.

Normally you don't want to do this since you want to execute this remotely over ssh, but since you had tried so far and are getting some errors, probably by trying locally connection: local could help to debug more in details this issue.

答案2

得分: 0

我知道这篇帖子很旧了,但也许这可以帮助到某人。

这个问题"/bin/sh: go: command not found"是因为在Ansible运行时缺少一些配置,可能需要像这样源化bash配置文件:

- name: 安装gogs
  shell: "source ~/.bash_profile && github.com/gogits/gogs"
  args:
    chdir: /home/{{ owner }}
  become_user: '{{ owner }}'

这对我起作用了。

英文:

I know this post is too old, but maybe this may help someone.

Ths issue /bin/sh: go: command not found it is because you are missing some configuration when Ansible runs, and probably you need to source the bash profile like this:

- name: Install gogs
  shell: &quot;source ~/.bash_profile &amp;&amp; github.com/gogits/gogs&quot;
  args:
    chdir: /home/{{ owner }}
  become_user: &#39;{{ owner }}&#39;

That worked for me.

huangapple
  • 本文由 发表于 2017年8月22日 19:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/45815938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定