ansible使用筆記(一)ansible安裝及常用配置說明

目錄
一、ansible介紹
二、ansible安裝及常用配置說明

一、ansible介紹
ansible是2013年推出的一款I(lǐng)T自動化和DevOps軟件,2015年被RedHat收購。是基于Python研發(fā),糅合很多老運(yùn)維工具的優(yōu)點(diǎn),實(shí)現(xiàn)了批量操作系統(tǒng)配置,批量程序部署,批量運(yùn)行命令等功能

ansible可以實(shí)現(xiàn)

  • 自動化部署APP
  • 自動化管理配置項
  • 自動化持續(xù)交付
  • 自動化(AWS)云服務(wù)管理

ansible優(yōu)點(diǎn)

  • 主從工作模式 只需要SSH和Python即可使用,無客戶端
  • ansible功能強(qiáng)大,支持自定義模塊 模塊豐富 支持playbook
  • 上手容易,門檻低
  • 基于Python開發(fā),做二次開發(fā)更容易
  • 使用公司比較多,社區(qū)活躍

ansible特性

  • 模塊化設(shè)計,調(diào)用特定的模塊完成特定任務(wù)
  • 基于Python語言實(shí)現(xiàn)
  • paramiko
  • PyYAML(半結(jié)構(gòu)化語言)
  • Jinja2
  • 其模塊支持JSON等標(biāo)準(zhǔn)輸出格式,可以采用任何編程語言重寫

管理主機(jī)

  • Ansible可以在安裝了Python 2(2.7版)或Python 3(3.5版及更高版本)的任何計算機(jī)上運(yùn)行。這包括Red Hat,Debian,CentOS,macOS,任何BSD等??刂乒?jié)點(diǎn)不支持Windows
  • ansible 使用以下模塊,都需要安裝
    paramiko
    PyYAML(半結(jié)構(gòu)化語言)
    Jinja2
    httplib2
    six

對于被托管的主機(jī)

  • ansible默認(rèn)通過SSH協(xié)議管理機(jī)器
  • 被管理主機(jī)要開啟SSH服務(wù),允許ansible主機(jī)登錄
    在托管節(jié)點(diǎn)上也需要安裝Python 2(2.7版)或Python 3(3.5版及更高版本)
    如果托管節(jié)點(diǎn)上開啟中了SElinux,需要安裝libselinux-Python

部署證書文件
ansible 是通過SSH在遠(yuǎn)程執(zhí)行命令的,SSH遠(yuǎn)程執(zhí)行命令必須通過認(rèn)證才行,密碼寫入配置文件安生性很差,一般會使用key方式認(rèn)證,給所有主機(jī)公鑰
沒有秘鑰命令執(zhí)行會出錯

二、ansible安裝及常用配置說明
環(huán)境準(zhǔn)備
6臺主機(jī),1臺管理主機(jī),5臺托管主機(jī),以實(shí)現(xiàn)批量程序部署,批量運(yùn)行命令等功能,具體要求如下表

ansible常用配置參數(shù)說明

  • ansible配置文件查找順序

  • 首先檢測 ANSIBLE_CONFIG變量定義的配置文件

  • 其次檢查當(dāng)前目錄下 ./ansible.cfg文件

  • 再次檢查當(dāng)前用記家目錄下 ~/ansible.cfg 文件

  • 最后檢查/etc/ansible/ansible.cfg文件

  • /etc/ansible/ansible.cfg是ansible默認(rèn)配置文件路徑

  • /etc/ansible/hosts 是ansible默認(rèn)host文件路徑

ansible.cfg配置文件
inventory 定義托管主機(jī)地址配置host文件路徑名 指定的配置文件,寫入遠(yuǎn)程主機(jī)的地址
host_key_checking = False ssh主機(jī)key驗(yàn)證配置參數(shù)
-如果為False,不需要輸入yes
-如果為Ture,等待輸入yes
ansible_ssh_prot
ssh端口號:如果不是默認(rèn)的端口號,通過此變量設(shè)置
ansible_ssh_user
默認(rèn)的ssh用戶名
ansible_ssh_pass
ssh密碼(這種方式并不安全,強(qiáng)烈建議使用SSH密鑰)
ansible_ssh_private_key_file
ssh使用的私鑰文件,適用于有多個密鑰,而你不想使用SSH代理的情況

ansible 托管主機(jī)地址配置host文件
格式
# 表示注釋
[組名稱]
主機(jī)名稱或IP地址, 其它參數(shù)

  • vars變量定義,用于組名后面
    例如
    [all:vars] //指定所有組key的存放位置
    ansible_ssh_private_key_file="/root/keyfile/id_dsa"
  • children子組定義,用于引用其它組名稱
    例如
    [app:children] //其中web、db分別為不同分組
    web
    db
[root@ansible myansible]# cat myhost 
[app1]
web1
db1
[app2]
web2
db2
[app:children]
app1
app2
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"
  • ansiblew命令基礎(chǔ)
    列出要執(zhí)行主機(jī)
    ansible all --list-hosts
  • 批量檢測主機(jī)
    ansible all -m ping -k
  • ansible主機(jī)集合 -m 模塊名稱 -a 模塊參數(shù)
    主機(jī)集合 主機(jī)名或分組名,多個使用"逗號"分隔
    -m 模塊名稱,默認(rèn)command模塊
    -a or --args模塊參數(shù)
    其它參數(shù)
    -i inventory文件路徑,或可執(zhí)行腳本
    -k 使用交互式登陸密碼
    -e 定義變量
    -v 顯示詳細(xì)信息

1 )管理主機(jī) 安裝EPEL源 EPEL源包含上面ansible所需要的所有模塊

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ~]# yum clean all
[root@ansible ~]# yum repolist

2)安裝ansible

[root@ansible ~]# yum -y install ansible              
[root@ansible ~]# ansible --version  //查看ansible版本
ansible 2.9.13

3)生成秘鑰 配置免秘登陸托管主機(jī)

[root@ansible ~]# vim /etc/ansible/hosts
[web]
web1
web2
[db]
db[1:2]
[other]
cache

[root@ansible ~]# ssh-keygen  //一路回車生成秘鑰
[root@ansible .ssh]# ansible all -m ping  //測試 失敗
web1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
db1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
......

[root@ansible .ssh]# for i in {40..45};do ssh-copy-id root@192.168.4.1$i; done   //使用for循環(huán)給托管主機(jī)傳送秘鑰 免秘登陸
[root@ansible .ssh]# ansible all -m ping  //成功
db2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

4 )指定秘鑰存放位置

[root@ansible ~]# mkdir keyfile/
[root@ansible ~]# mv .ssh/id_dsa keyfile/  //改變key的存放位置
[root@ansible ~]# ansible all -m ping    //失敗
web1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
......

[root@ansible ~]# cat /etc/ansible/hosts
[web]
web1
web2
[db]
db[1:2]
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"   //指定key的存放位置
[root@ansible ~]# ansible all -m ping   //成功
db2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

[root@ansible ~]# mkdir myansible
[root@ansible ~]# cd myansible/
[root@ansible myansible]# vim myhost
[app1]
web1
db1
[app2]
web2
db2
[app:children]
app1
app2
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"

[root@ansible myansible]# vim ansible.cfg
[defaults]
inventory = myhost
host_key_checking = False

[root@ansible myansible]# ls
ansible.cfg  myhost

[root@ansible myansible]# ansible app1 -m ping
web1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

[root@ansible myansible]# ansible app -m ping
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

5 ) 測試ansible.cfg文件

[root@ansible myansible]# ansible app --list-hosts   //首先檢查本目錄 下ansible.cfg文件 成功
  hosts (4):
    web1
    db1
    web2
    db2
[root@ansible myansible]# cd ..
[root@ansible ~]# ansible app --list-hosts    //本目錄下無ansible.cfg文件  默認(rèn)的/etc/ansible/hosts 也沒有定義app 組 報錯
[WARNING]: Could not match supplied host pattern, ignoring: app
[WARNING]: No hosts matched, nothing to do
  hosts (0):
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容