1. 程式人生 > >Ansible詳解(一)基礎安裝和配置

Ansible詳解(一)基礎安裝和配置

note oom 管理 基礎 get 結合 conn float 模塊化

ansible 是一款輕量級自動化運維工具,由的 Python 語言開發,結合了多種自動化運維工具的特性,實現了批量系統配置,批量程序部署,批量命令執行等功能; ansible 是基於模塊化實現批量操作的。

一、安裝

控制機器

pip install ansible==2.5.5

yum install sshpass

受控機器

yum install libselinux-python

yum install python2-simplejson

version<python2.4

測試

echo 127.0.0.1>host

ansible all -m ping -i hosts --ask -pass

技術分享圖片

二、管理協議

Ansible 通過 ssh 協議對受控機器管理,可使用口令和秘鑰對兩種方式進行權限驗證,默認使用密鑰對方式。

秘鑰對

1.在控制機器生成秘鑰對

ssh -keygen -t rsa -b 4096 -C*kk

2.添加公鑰到受控機器

  • 拷貝添加:ssh-copy-id -i ~/.ssh/id_rsa.pub user@host
  • 本地添加:cat ~/.ssh/id_rsa.pub>>~/.ssh/authorized_keys

3.測試

ssh user@host

ansible all -m ping -i hosts

技術分享圖片

三、配置

inventory

1.ansible 管理主機信息的配置

2.配置文件格式

  • ini
  • yaml

3.配置文件路徑

  • 通過命令行參數制定:ansible -i
  • 通過環境變量制定:export ANSIBLE_INVENTORY
  • 默認配置路徑:/ect/ansible/hosts

4.配置內容

4.1基本配置

host_v1.ini

127.0.0.1
ip

host_v1.yaml

---
all:
hosts:
 127.0.0.1:
 ip:

測試

ansible all -m ping -ihosts -i host_v1.ini

ansible all -m ping -i hosts -i host_v1.yaml

ansible 127.0.0.1-m ping -ihosts -i host_v1.ini

ansible ip -m ping -ihosts -i host_v1.yaml

主機參數配置

1.參數項

alias 主機別名

ansible_connection

默認 smart

可選值:local、smart、ssh、paramiko

ansilbe_host 登錄主機地址

ansilbe_port 默認 22

ansilbe_user 登錄主機用戶名

ansible_become

是否啟用 sudo 權限

默認: false

可選值 :true、false

ansible_become_pass

登錄主機用戶密碼,用於切換 sudo 權限

建議使用 ansible 命令行參數ask_become_pass 替換

ansible_become_user

切換 sudo 後 執行進程中使用的用戶名

ansible_ssh_pass

登錄主機使用密碼

建議使用 ansible 命令行參數ask_pass 替換

ansible_ssh_private_key_file

登錄主機使用私鑰

ansible_python_interpreter

受控機器執行 Python 解釋器

默認 /bin/env/python

hosts_v2.ini

技術分享圖片

hosts_v2.yaml

技術分享圖片

組&組變量

  • 可對主機進行分組並命名,批量對主機進行操作
  • 一個主機可屬於多個組

host_v3.ini

技術分享圖片

host_v3.yaml

技術分享圖片

測試

ansible ip -m ping -ihosts -i host_v3.yaml

ansible webserver -m command -a ‘sleep 30‘ -ihost_v3.ini --become --ask-become-pass

組中組

host_v4.ini

技術分享圖片

host_v4.yaml

技術分享圖片

測試

ansible test --list hosts -i host_v4.yaml

ansible test -m ping -ihosts -i host_v4.yaml

配置分割

  • 在 hosts 文件中值配置主機分組信息,主機配置與組配置分別存儲在 host_vars 和 group_vars 目錄
  • 主機配置存儲在 host_vars 目錄中,文件名使用別名.yaml
  • 組配置存儲在 group_vars 目錄中,文件名使用組名.yaml

host_v5.ini

localhost

[webserver]
mytest

[test:children]
webserver

host_v5.yaml

---
all:
hosts:
 localhost:
children:
  webserver:
    hosts:
     mytestm:
  test:
   children:
    webserver:

host_vars

host_vars/localhost.yaml

---
ansible_connect: local

host_vars/mytest.yaml

---
ansible_host: ip
ansible_user:silence

group_vars

group_vars/webserver.yaml

---
ansible_connect:smart
ansible_port:22
ansible_become_user: root
ansible_python_interpreter:"/bin/env python2.6"

測試

ansible test -m ping -i host_v5.yaml

ansible test -m setup -i host_v5.yaml

ansible test -m command -a ‘sleep 30‘ -ihost_v5.ini --become --ask-become-pass

動態 inventory

文件 inventory.py腳本內容

技術分享圖片

初始化權限

xhmod +x inventory.py

測試

ansible all --list -hosts -i inventory.py

ansible all -m ping -i inventory.py

ansible.cfg

1.配置文件路徑

  • export ANSIBLE_CONFIG=~/ansible.cfg
  • ansible.cfg
  • ~/.ansible.cfg
  • /etc/ansible/ansible.cfg

2.默認配置

  • https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
  • ansible-config list
  • ansible-config dump

3.配置項

host_key_checking

  • 是否檢查控制密鑰存在於 know_hosts 列表
  • 默認值 :true
  • 可選值:true、false

技術分享圖片

未完待續......

作者:KK

鏈接:http://t.cn/RrAjAte

51Reboot 第19期Python實戰班、第8期自動化運維班正在招生中

詳情咨詢QQ Ada:279312229

Ansible詳解(一)基礎安裝和配置