无法使用ansible下载golang存储库

时间:2017-08-22 11:05:27

标签: go ansible

我正在尝试从github下载golang包。这就是我的剧本的样子

- name: Fetch latest gogs repository
      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: 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

我收到了这个错误

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
}

这里我的变量是

---
  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时,它运行正常。

2 个答案:

答案 0 :(得分:1)

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

---
- hosts: all
  connection: local 

  tasks:
  - name: check go version
    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: go get 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可能有助于调试更多细节问题。

答案 1 :(得分:0)

我知道这篇文章太旧了,但这也许对某人有帮助。

此问题/bin/sh: go: command not found是因为当Ansible运行时您缺少某些配置,并且可能需要像这样来获取bash配置文件:

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

对我有用。