我有一個 Ansible 2.9.27,我正在嘗試為我之前用 Ansible 克隆的 git 存盤庫添加上游遠程。假設已經克隆的存盤庫位于/home/user/Documents/github/目錄中,我想為它們添加上游遠程(git remote add upstream為每個存盤庫)。
任務如下所示:
- name: Add remote upstream to github projects
# TODO: how to add remote with git module?
command: git remote add upstream [email protected]:{{ git_user }}/{{ item }}.git
changed_when: false
args:
chdir: /home/user/Documents/github/{{ item }}
loop: "{{ github_repos }}"
問題是 ansible-lint 不喜歡使用command而不是git模塊:
WARNING Listing 1 violation(s) that are fatal
command-instead-of-module: git used in place of git module
tasks/github.yaml:15 Task/Handler: Add remote upstream to github projects
我需要做什么才能為這些帶有git模塊的存盤庫添加遠程上游?
uj5u.com熱心網友回復:
由于您想要實作的目標(還……)尚未由git模塊負責,因此這是command.
在這種情況下,可以針對該特定任務在 ansible lint 中使特定規則靜音。
更進一步,您的changed_when: false子句看起來有點像一個快速而骯臟的修復來使no-changed-when規則靜音,并且可以與failed_when子句一起增強以檢測遠程已經存在的情況。
以下是我將如何撰寫該任務以使其具有冪等性、檔案化和傳遞所有需要的 lint 規則:
- name: Add remote upstream to github projects
# Git module does not know how to add remotes (yet...)
# Using command and silencing corresponding ansible-lint rule
# noqa command-instead-of-module
command:
cmd: git remote add upstream [email protected]:{{ git_user }}/{{ item }}.git
chdir: /home/user/Documents/github/{{ item }}
register: add_result
changed_when: add_result.rc == 0
failed_when:
- add_result.rc != 0
- add_result.stderr | default('') is not search("remote .* already exists")
loop: "{{ github_repos }}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398248.html
標籤:混帐 能听懂的 ansible-2.x
上一篇:推送變更的問題——git
