ansible安裝與配置文件

ansible 安裝
python

  1. 源碼安裝git

    git clone git://github.com/ansible/ansible.git --recursivegithub

    cd ./ansibleshell

    source ./hacking/env-setupcentos

    1)若是須要安裝pip, 先安裝對應python版本的pip:ssh

    easy_install pipide

    2)安裝控制主機須要的Python模塊測試

    pip install paramiko PyYAML Jinja2 httplib2 sixcentos7

    3)更新ansible版本日誌

    git pull --rebase

    git submodule update --init --recursive

  2. yum 安裝

    1)yum源

    centos6:rpm -Uvh http://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm

    centos7:rpm -Uvh http://mirrors.zju.edu.cn/epel/7/x86_64/e/epel_release-7-5.noarch.rpm

    yum install ansible


3.本次爲yum 安裝:/etc/ansible

配置運行環境

  1. ansible.cfg 配置文件

    1)環境變量:export ANSIBLE_SUDO_USER=root

    2)配置參數

    inventory 資源清單,被管理的主題資源列表配置文件位置

    library 代碼模塊目錄,多個目錄能夠用:隔開

    forks 設置ansible最多能有多少個進程同時工做

    sudo_user 默認指向命令用戶

    remote_port 指定連接被管理節點端口,默認22

    host_key_checking 默認設置爲False;相似第一次ssh時須要進行yes確認初始化known_host

    timeout ssh連接的超時間隔

    log_path 日誌文件配置


Linux主機ssh無密碼訪問

測試:

$ansible --version


測試主機連通性/etc/ansible/host定義列表

192.168.1.1 | SUCCESS => {

    "changed": false, 

    "ping": "pong"

}

192.168.1.2 | SUCCESS => {

    "changed": false, 

    "ping": "pong"

}


4.在被管理節點批量執行命令

能夠新建一個新主機清單,執行一些命令

cat new.cfg

[test]

192.168.1.1

192.168.1.2


$ansible test -m shell -a '/bin/echo hello ansible!' -i inventory.cfg

$ansible test -m command -a "/bin/echo  hello ansible" -i new.cfg

192.168.1.1 | SUCCESS | rc=0 >>

hello ansible!


192.168.1.2 | SUCCESS | rc=0 >>

hello ansible!


5.ansible獲取幫助信息

ansible-doc -h 幫助信息

ansible-doc -l 列出支持的模塊

ansible-doc -s+ 直接加模塊命令,顯示模塊描述和使用示例:ansible-doc -s yum

執行ansible命令時在命令後加-vvv能夠得到詳細輸出結果


警告:

[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

將/var/log/ansible.log 加上寫權限就行

chmod +666 /var/log/ansible.log

相關文章
相關標籤/搜索