iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 7
0
Cloud Native

AWS高手同事離職後不止30天系列 第 7

IT自動化工具。設定系統,佈署安裝軟體 > Ansible II

  • 分享至 

  • twitterImage
  •  

四、沒有Remote Host(只有localhost)

前一天提到Ansible可以透過執行Playbooks中的play來對Host(s)進行「工作任務列表」(Tasks) >> 讓你的Host(s)完成一個演出(play)。而Ansible預設會用本地OpenSSH來跟遠端機器溝通,第一時間沒有Remote Host那就開始來跟localhost溝通吧,順便看看過程需要什麼~~

Command Line Tools > ansible

  • 語法:ansible <host-pattern> [options]
  • 範例:ansible localhost -m shell -a "/bin/echo hello"
    01_ansible localhost.png
  • 說明:
    ansible執行時預設使用的設定檔(ansible.cfg)位置順序
    • 當前目錄
    • $HOME
    • /etc/ansible/ansible.cfg(ansible裝起來就會存在,裡面有相關說明)
      預設的host清單位置 > /etc/ansible/hosts
      所以上圖結果顯示,您提供的host清單是空的,但「localhost」是可以用的。

設定/etc/ansible/hosts > 加上127.0.0.1

  • 圖例:
    02_localhost_to_inventory.png
  • 說明:
    上圖中,當你再次執行ansible localhost -m shell -a "/bin/echo hello",會出現錯誤,因為這時候,ansible會嘗試用ssh去連線,這時候就會失敗。因為本機ssh Server 預設是「未啟動的」。

啟動本機ssh server

其實用google搜尋有一堆關於ssh文章,因為這算是很基本的。這邊就當作是自己的筆記,並配合ansible的了解,另外,當你搞定localhost時,那Remote hsots你就知道該怎麼「溝通」了

  • 產生Server端金鑰:sudo ssh-keygen -A
    03_gen_ssh_server_host_keys.png

  • 啟動Server:sudo service ssh start
    04_start_ssh_server.png

  • 產生Client端金鑰:ssh-keygen -b 4096(key type > rsa)
    05_gen_ssh_client_keys.png

  • 上傳Client端金鑰(公鑰)到Server端:因為是本機,所以將id_rsa.pub 放入 ~/.ssh/authorized_keys
    指令:cat id_rsa.pub >> authorized_keys
    06_upload_client_public_key_to_server.png

  • 修改authorized_keys:chmod 600 authorized_keys(不然連線時會出現Permission denied (publickey))
    07_chmod_authorized_keys.png

  • ssh Client to ssh Server
    08_client_ssh_to_server.png

  • 再次執行ansible指令:ansible localhost -m shell -a "/bin/echo hello"
    09_ansible_to_host_by_ssh.png


待續…


上一篇
IT自動化工具。設定系統,佈署安裝軟體 > Ansible I
下一篇
IT自動化工具。設定系統,佈署安裝軟體 > Ansible III
系列文
AWS高手同事離職後不止30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言