ansible安裝

Ansible 簡介python

Ansible 是新出現的自動化運維工具,基於 Python 開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。web

Ansible 是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是 Ansible 所運行的模塊,Ansible 只是提供一種框架。shell

  1. 新增 epel-release 第三方套件來源
    yum install epel-release.noarch vim

    安裝 Ansible
    yum install ansible安全

    驗證安裝結果
    [root@bogon ~]# ansible --version
    ansible 2.8.1
    config file = /etc/ansible/ansible.cfg
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /usr/bin/ansible
    python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]ruby

  2. 建立SSH交互免密登陸bash

    [root@ansible ~]# ssh-keygen -t rsa                     #生成密鑰
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:IXw+ZtlGQWZ805oSWy9WHhiDYHHquOYOs7OG6D61X2I
    root@ansible
    The key's randomart image is:
    +---[RSA 2048]----+
    |        +=*.o+   |
    |     . . =+.=.+  |
    |      o + .= B . |
    |       * =o = o  |
    |      . S oo .   |
    |   .   + o       |
    | ...+Eo.         |
    |....+*o          |
    |oo..+=o          |
    +----[SHA256]-----+
    [root@ansible ~]# ssh-copy-id root@192.168.1.6          #將本身的密鑰上傳到被遠程節點服務器
    [root@ansible ~]# ssh 192.168.1.6
    [root@host1 ~]# exit
    [root@ansible ~]# ssh-copy-id root@192.168.1.12
    [root@ansible ~]# ssh 192.168.1.12
    [root@host2 ~]# exit
  3. Ansible 返回的值很是友好,通常會用三種顏色來表示執行結果:
    綠色:表示執行成功而且沒有對目標機器作修改
    紅色:表示執行過程有異常
    *×××:表示命令執行後有狀態變化 服務器

    ansible webservers -m copy -a "src=/root/zzq.sh dest=/tmp/ owner=root group=root mode=0755 force=yes"

    [root@rs1 ~]# ssh-keygen -t rsa -Ping #生成安全鏈接祕鑰框架

  4. 生成安全鏈接祕鑰運維

    [root@rs1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.6   #使用安全鏈接祕鑰鏈接1號目標主機

    root@192.168.1.6's password: #輸入目標主機密碼

    [root@rs1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.12 #使用安全鏈接祕鑰鏈接2號目標主機
    root@192.168.1.12's password: #輸入目標主機密碼

  5. 添加本地解析

    [root@rs1 ~]# vim /etc/hosts 添加本地解析
    
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.1.6 vs
    192.168.1.12 rs2
  6. 定義目標主機組

    /etc/ansible/hosts文件支持使用如下變量設置相關的遠程主機信息:
    
    ansible_ssh_host     #用於指定被管理的主機的真實IP
    ansible_ssh_port     #用於指定鏈接到被管理主機的ssh端口號,默認是22
    ansible_ssh_user     #ssh鏈接時默認使用的用戶名
    ansible_ssh_pass     #ssh鏈接時的密碼
    ansible_sudo_pass     #使用sudo鏈接用戶時的密碼
    ansible_sudo_exec     #若是sudo命令不在默認路徑,須要指定sudo命令路徑
    ansible_ssh_private_key_file     #祕鑰文件路徑,祕鑰文件若是不想使用ssh-agent管理時可使用此選項
    ansible_shell_type     #目標系統的shell的類型,默認sh
    ·ansible_connection     #SSH 鏈接的類型: local , ssh , paramiko,在 ansible 1.2 以前默認是 paramiko ,後來智能選擇,優先使用基於 ControlPersist 的 ssh (支持的前提)
    ansible_python_interpreter     #用來指定python解釋器的路徑,默認爲/usr/bin/python 一樣能夠指定ruby 、perl 的路徑
    ansible_*_interpreter     #其餘解釋器路徑,用法與ansible_python_interpreter相似,這裏"*"能夠是ruby或才perl等
  7. 目標主機組:

    [root@rs1 ~]# cd /etc/ansible
    [root@rs1 ansible]# ls
    ansible.cfg  hosts  roles
    [root@rs1 ansible]# vim hosts#編輯文件
    [websrvs]#添加web組
    192.168.1.6
    192.168.1.12
    
    [dbsrvs] #定義db組
    192.168.1.12
    
     [root@rs1 ansible]# ansible all  --list-hosts#列出目標主機
    hosts (2):
        192.168.1.12
        192.168.1.6
    [root@rs1 ansible]# ansible all -m ping  -C# 對全部目標主機預運行ping測試
    192.168.1.12 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.1.6 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@rs1 ansible]# ansible all -m ping # 對全部目標主機ping測試
    192.168.1.12 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.1.6 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
  8. 模塊文檔

    [root@rs1 ansible]# ansible-doc -l #列出目標主機模塊文檔
    [root@rs1 ansible]# ansible-doc -s group 獲取設置組命令文檔
    一、定義指望的目標狀態
    二、操做必須是冪等的,操做次數必須相等
  9. group組模塊使用

    對目標主機建立組,並傳遞參數

    [root@rs1 ansible]# ansible all -m group -a "gid=3000 name=mygrp state=present system=no" #對全部目標主機建立組,m是加載group模塊,a是傳遞參數,state是建立仍是刪除
    192.168.1.6 | SUCCESS => {
        "changed": true, 
        "gid": 3000, 
        "name": "mygrp", 
        "state": "present", 
        "system": false
    }
    192.168.1.12 | SUCCESS => {
        "changed": true, 
        "gid": 3000, 
        "name": "mygrp", 
        "state": "present", 
        "system": false
    }
  10. user用戶模塊使用

    對目標主機建立用戶,並傳遞參數

    [root@rs1 ansible]# ansible all -m user -a "uid=5000 name=testuser state=present groups=mygrp shell=/bin/tcsh"對全部目標主機建立用戶,m是加載user模塊,a是傳遞參數,state是建立仍是刪除,groups是附加組,shell是默認shell
    
    192.168.1.6 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    「group": 5000, 
    "groups": "mygrp", 
    "home": "/home/testuser", 
    "name": "testuser", 
    "shell": "/bin/tcsh", 
    "state": "present", 
    "system": false, 
    "uid": 5000
    }

    192.168.1.12 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 5000,
    "groups": "mygrp",
    "home": "/home/testuser",
    "name": "testuser",
    "shell": "/bin/tcsh",
    "state": "present",
    "system": false,
    "uid": 5000
    }

  11. copy複製模塊使用

    對目標主機拷貝本地文件,並傳遞參數,指明src源文件位置和dest目標文件位置
    [root@rs1 ansible]# ansible-doc -s copy#查詢copy使用文檔
    [root@rs1 ansible]# ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible mode=600"
    對全部目標主機拷貝本地文件,m是使用copy模塊,a是傳遞參數,src源文件位置,dest目標文件位置,mode權限(加了'/'就是目錄)
    192.168.1.6 | SUCCESS => {
    "changed": true,
    "checksum": "4367ba689c50b4ab956ce0704f048f4fb0cc1a28",
    "dest": "/tmp/fstab.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "c6ac458a97ee2f7ed913fdc8b17e9394",
    "mode": "0600",
    "owner": "root",
    "size": 465,
    "src": "/root/.ansible/tmp/ansible-tmp-1534522522.24-76431722279920/source",
    "state": "file",
    "uid": 0
    }
    192.168.1.12 | SUCCESS => {
    "changed": true,
    "checksum": "4367ba689c50b4ab956ce0704f048f4fb0cc1a28",
    "dest": "/tmp/fstab.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "c6ac458a97ee2f7ed913fdc8b17e9394",
    "mode": "0600",
    "owner": "root",
    "size": 465,
    "src": "/root/.ansible/tmp/ansible-tmp-1534522522.23-209859323698602/source",
    "state": "file",
    "uid": 0
    }

copy模塊設置屬主屬組用法

[root@rs1 ansible]# ansible all -m copy -a "content='hi tere\n' dest=/tmp/hi.txt owner=testuser group=mygrp"#對全部目標主機拷貝本地文件,m是使用copy模塊,a是傳遞參數,content建立文檔到dest目標文件位置,設置屬主屬組
192.168.1.6 | SUCCESS => {
"changed": true, 
"checksum": "50dbdebeaa8c0f1c3cccfcae54ef71fc2c0e4fa8", 
"gid": 3000, 
"group": "mygrp", 
"mode": "0644", 
"owner": "testuser", 
"path": "/tmp/hi.txt", 
"size": 8, 
"state": "file", 
"uid": 5000
}
192.168.1.12 | SUCCESS => {
"changed": true, 
"checksum": "50dbdebeaa8c0f1c3cccfcae54ef71fc2c0e4fa8", 
"gid": 3000, 
"group": "mygrp", 
"mode": "0644", 
"owner": "testuser", 
"path": "/tmp/hi.txt", 
"size": 8, 
"state": "file", 
"uid": 5000
}

fetch複製模塊

從遠程單一主機複製到本地主機
使用文檔:ansibile-doc -s fetch

command模塊執行命令

對目標主機執行命令

[root@rs1 ansible]# ansible all -m command -a "ifconfig"#對全部目標主機,m使用模塊,command命令模塊,a傳遞參數 執行ifconfig命令
192.168.1.12 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
.............     
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
.............    

192.168.1.6 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
............... 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
...............

注意:command缺點是沒法解析管道命令

shell模塊使用

shell模塊解決了command模塊的缺點,對傳遞參數用shell解析並執行
[root@rs1 ansible]# ansible all -m shell -a "echo 123 | passwd --stdin testuser"#對全部目標主機使用shell解析傳遞參數中的管道命令
192.168.1.6 | SUCCESS | rc=0 >>
更改用戶 testuser 的密碼 。
passwd:全部的身份驗證令牌已經成功更新。

192.168.1.12 | SUCCESS | rc=0 >>
更改用戶 testuser 的密碼 。
passwd:全部的身份驗證令牌已經成功更新。

file模塊文件屬性命令

對目標主機,傳遞參數,建立文件、目錄和軟鏈接
[root@rs1 ansible]# ansible all -m file -a "path=/var/tmp/hello.dir state=directory"#對全部目標主機使用file模塊建立hello.dir目錄
192.168.1.12 | SUCCESS => {
"changed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"owner": "root", 
"path": "/var/tmp/hello.dir", 
"size": 6, 
"state": "directory", 
"uid": 0
}
192.168.1.6 | SUCCESS => {
"changed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"owner": "root", 
"path": "/var/tmp/hello.dir", 
"size": 6, 
"state": "directory", 
"uid": 0
}

[root@rs1 ansible]# ansible all -m file -a"src=/etc/fstab path=/var/tmp/fstab.link state=link"#對全部目標主機使用file模塊建立fstab文件的符號鏈接
192.168.1.12 | SUCCESS => {
"changed": true, 
"dest": "/var/tmp/fstab.link", 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"size": 10, 
"src": "/etc/fstab", 
"state": "link", 
"uid": 0
}
192.168.1.6 | SUCCESS => {
"changed": true, 
"dest": "/var/tmp/fstab.link", 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"size": 10, 
"src": "/etc/fstab", 
"state": "link", 
"uid": 0
}

cron模塊計劃任務

對目標主機,傳遞參數,設置計劃任務

[root@rs1 ansible]# ansible all -m crom -a "minute=*/3 job='/usr/sbin/update 192.168.1.1 &> /dev/null' name=none state=present"#對全部目標主機 ,m使用模塊,crom計劃任務,建立任務每三分鐘同步時間

yum模塊安裝軟件程序包

rpm軟件的安裝

[root@rs1 ansible]# ansible all -m yum -a "name=ngix state=instlled"#對全部主機安裝ngix

service模塊管理目標服務

對目標主機,傳遞參數,管理服務。

[root@rs1 ansible]# ansible all -m service -a"name=httpd state=started"#對全部主機啓動httpd服務
[root@rs1 ansible]# ansible all -m service -a"name=httpd state=stoped"#對全部主機中止httpd服務

script模塊腳本管理

[root@rs1 ansible]# vim /tmp/test.sh#編寫一個測試腳本
對目標主機,傳遞參數,執行本地bash腳本
#!binbash

echo "ansible script" > /tmp/ansible.txt
[root@rs1 ansible]# ansible all -m script -a "/tmp/test.sh"#全部目標主機執行本地bash腳本
相關文章
相關標籤/搜索