やりたいこと
Playbook で GitLabのリポジトリにファイルを登録します。
公式ドキュメントを検索したけど、git add, git commit, git push の一連ができそうなモジュールを見つけられず、GItHubを覗いてみたら git_acpモジュールがありました。
GitHub - lvrfrc87/git-acp-ansible: git_acp (add/commit/push) installer for Ansible
git_acpのインストールと設定例
01.インストールコマンドを実行します。
pip install git-acp-ansible
02.モジュールのパスをansible.cfgに記載します。
例
[defaults]
library=/usr/local/lib/python3.6/site-packages/git_acp/modules
Playbook例
日時がファイル名のテキストを作成し、GitLabに登録します。
予め、Gitlabにトークンを設定します。
---
- hosts: gitlab
gather_facts: false
connection: local
vars:
now_date: "{{ lookup('pipe','date +%Y%m%d%H%M') }}"
tasks:
- name: git clone
git:
repo: http://root:【パスワード】@XX.XX.XX.XX/root/sample.git
dest: /tmp/test/sample
update: yes
version: master
become: yes
- name: make file
file:
path: "/tmp/test/sample/{{ now_date }}.txt"
state: touch
mode: 0755
- name: git add & commit & push
git_acp:
user: root
token: XXXXXXXXXXXX
path: /tmp/test/sample/
branch: master
comment: Add file
add: "{{ now_date }}.txt"
mode: ssh
url: "git@XX.XX.XX.XX:root/sample.git"
実行結果
Playbookは通りました。
Using /etc/ansible/ansible.cfg as config file
PLAY [gitlab] **********************************************************
TASK [git clone] *******************************************************
changed: [gitlab_a] => {"after": "eb64b8b9a9b40d85cc9fc34a4d296bc974bca61b", "ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "before": "0ba46d56d56fe704f02f4be1cd3ccf8a52573a32", "changed": true, "remote_url_changed": false}
TASK [make file] *******************************************************
changed: [gitlab_a] => {"changed": true, "dest": "/tmp/test/sample/202010231820.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0}
TASK [git add & commnt & push] ************************************
changed: [gitlab_a] => {"changed": true, "git_commit": "[master c53188e] Add file\n Committer: root <root@XX.XX.XX.XX>\nYour name and email address were configured automatically based\non your username and hostname. Please check that they are accurate.\nYou can suppress this message by setting them explicitly:\n\n git config --global user.name \"Your Name\"\n git config --global user.email you@example.com\n\nAfter doing this, you may fix the identity used for this commit with:\n\n git commit --amend --reset-author\n\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 202010231820.txt\n", "git_push": "To http://root:【パスワード】@XX.XX.XX.XX/root/sample.git\n eb64b8b..c53188e master -> master\n"}
PLAY RECAP **********************************************************
gitlab_a : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
GitLabのリポジトリにも、登録されていました。
備考
git add / git commit / git push を実行するには、今のところコミュニティのモジュールしかないのかな。