0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansibleでファイルを生成しGitHubへpush

Posted at

Ansibleでローカル環境にファイルを生成しGithubにpushするまで。

手順

1. ローカル環境でSSHキーを生成(ubuntuから実行)

ssh-keygen -t rsa -b 4096
  • プロンプトに戻るまでEnterキーを数回押す。

2. SSH公開キーをGitHubに追加

生成したSSHキーの公開部分(~/.ssh/id_rsa.pub)をコピーし、GitHubに追加。

  1. 公開キーをコピー:

    cat ~/.ssh/id_rsa.pub
    
  2. GitHubのWebページにログインし、SSHキーの設定ページにアクセスし、「New SSH key」で追加。
    image.png

  3. ローカルでSSH接続のテストをする。

    ssh -T git@github.com
    

3. リポジトリをclone

ローカルからSSHを使用してリポジトリをcloneする。

git clone git@github.com:xxx-xxx-xxx-com/FlexOne_workshop.git ~/FlexOne_workshop

4. Ansible play-bookの作成

以下の内容でansible-playbook.ymlを作成する。

---
- name: Run command on localhost, save output, and push to GitHub
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Run command and save output
      shell: "★echo 'Hello, World!' && date"
      register: command_output

    - name: Save output to a file
      copy:
        content: "{{ command_output.stdout }}"
        dest: "★/home/xxx/FlexOne_workshop/ansible_output.txt"

    - name: Configure Git user email
      command: git config --global user.email "★@yahoo.co.jp"
    
    - name: Configure Git user name
      command: git config --global user.name "★38pinn"

    - name: Add file to git
      command: "git add ansible_output.txt"
      args:
        chdir: "★/home/xxx/FlexOne_workshop"

    - name: Commit changes
      command: "git commit -m 'Add Ansible command output'"
      args:
        chdir: "★/home/xxx/FlexOne_workshop"

    - name: Push to GitHub
      command: "git push origin main"
      args:
        chdir: "★/home/xxx/FlexOne_workshop"

`★の部分は環境に合わせて置き換える。

5. 実行

以下のコマンドを実行する。

ansible-playbook ansible-playbook.yml

実行ログ

xxx@xxx-virtual-machine:~/Ansible$ ansible-playbook ansible-playbook.yml

PLAY [Run command on localhost, save output, and push to GitHub] **************************************************************************************************************************************************************************

TASK [Run command and save output] ********************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Save output to a file] **************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Configure Git user email] ***********************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Configure Git user name] ************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Add file to git] ********************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Commit changes] *********************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Push to GitHub] *********************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************
localhost                  : ok=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

xxxo@xxx-virtual-machine:~/Ansible$ 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?