自動化運維之ansible-安裝部署與基礎命令篇

1、Ansible簡介

Ansible基於Python語言開發,集合了衆多優秀運維工具的優勢,實現了批量運行命令、部署程序、配置系統等功能。mysql

2、安裝部署Ansible服務

Ansible自動化運維環境由控制主機與被管理主機組成,因爲Ansible是基於SSH協議進行通訊的,因此控制主機安裝Ansible軟件後不須要重啓或容許任何程序,被管理主機也不須要安裝和運行任何應用程序。web

Ansible案例環境sql

角色 ip地址 組名
控制主機 192.168.174.209
被管理主機1 192.168.174.208 webservers
被管理主機2 192.168.174.142 dbservers

1. 在控制主機上安裝epel源

yum install -y epel-release

2.使用yum命令安裝Ansibleshell

yum install -y ansible

3.安裝tree,展現ansible樹狀結構vim

yum install tree -y

4.查看樹狀結構bash

tree /etc/ansible/
/etc/ansible/
├── ansible.cfg    #ansible的配置文件
├── hosts         #ansible的主倉庫,用於存儲須要管理的遠程主機的相關信息
└── roles     #角色

5.配置主機清單app

vim /etc/ansible/hosts
插入被管理主機的組名,及ip地址

[webservers]
192.168.174.208

[dbservers]
192.168.174.142

6.設置SSH無密碼登陸運維

ssh-keygen -t rsa      #基於ssh密鑰的鏈接

在使用ssh-keygen產生一對密鑰後,在家目錄會產生一個隱藏文件夾.ssh,進入.ssh目錄下,使用ssh-copy-id來下發生成的密鑰。ssh

ssh-copy-id root@192.168.174.208   #配置密鑰對驗證
ssh-copy-id root@192.168.174.142

7.使用免交互代理ide

ssh-agent bash
ssh-add

8.使用ansible命令測試是否安裝成功

[root@promote .ssh]# ansible all -a 'date'  //查看兩臺被管理主機的時間
192.168.174.142 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 20:55:39 CST

192.168.174.208 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 20:55:39 CST

以上ansible的環境部署就完成了。

3、Ansible基礎命令的應用

Ansible可使用命令方式進行自動化管理,其基本語法爲:
ansible <host-pattern> [-m module_name] [-a args]

<host-pattern>被管理的主機的ip或組號

[-m module_name]要使用的哪些模塊

[-a args]模塊特有的參數

Ansible自帶了不少的模塊進行對管理主機的各類任務管理。首先來了解一下Ansible經常使用的一些模塊。

1.command模塊

Ansible管理工具使用-m選項指定使用的模塊,默認使用的是command模塊。

(1)使用ip地址指定運行的主機

[root@promote .ssh]# ansible 192.168.174.208 -m command -a 'date'
192.168.174.208 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 21:21:18 CST

(2)使用被管理主機中的分類運行

[root@promote .ssh]# ansible webservers -m command -a 'date'
192.168.174.208 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 21:22:38 CST

(3)在全部被管理主機上運行

[root@promote .ssh]# ansible all -m command -a 'date'
192.168.174.142 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 21:23:28 CST

192.168.174.208 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 21:23:28 CST

2.cron模塊

即自定義任務計劃模塊,用於管理被管理主機的任務計劃。cron有兩種狀態(state):present表示添加(能夠省略),absent表示移除。

(1)添加任務計劃

ansible webservers -m cron -a 'minute="*/1" job="/bin/echo heihei name="test cron job"'

(2)移除任務計劃

移除任務計劃是按照指定的任務計劃名來執行的,所以必須指定被移除任務計劃的名字name,假如該計劃任務沒有取名字,name=None便可

ansible webservers -m cron -a 'name="test cron job" state=absent'

3.user模塊

用於建立新用戶和更改、刪除已存在的用戶。用name指定建立的用戶的名稱。

(1)建立用戶

[root@promote .ssh]# ansible webservers -m user -a 'name=zhangsan'
192.168.174.208 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1000, 
    "home": "/home/zhangsan", 
    "name": "zhangsan", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1000
}

(2)查看用戶是否建立成功

[root@promote .ssh]# ansible webservers -a 'tail -1 /etc/passwd'
192.168.174.208 | SUCCESS | rc=0 >>
zhangsan:x:1000:1000::/home/zhangsan:/bin/bash

4.group模塊

用於對用戶組的管理

#建立mysql組,並將zhangsan用戶添加到mysql組
[root@promote .ssh]# ansible webservers -m group -a 'name=mysql gid=306 system=yes'
192.168.174.208 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}
[root@promote .ssh]# ansible webservers -m user -a 'name=zhangsan uid=307 system=yes group=mysql'
192.168.174.208 | SUCCESS => {
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 306, 
    "home": "/home/zhangsan", 
    "move_home": false, 
    "name": "zhangsan", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 307
}

5.copy模塊

實現文件的複製和批量下發文件

#將本地文件/etc/fstab複製到被管理主機的/opt下,取名fstab.bk,並將全部者設置爲root,權限設置爲666
[root@promote .ssh]# ansible webservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=666'
192.168.174.208 | SUCCESS => {
    "changed": true, 
    "checksum": "6fb776f669de8f65b92aa2d6975f29d14860e8a9", 
    "dest": "/opt/fstab.bk", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "7f37c08e599574ebb0f41f2f92a54f20", 
    "mode": "0666", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 465, 
    "src": "/root/.ansible/tmp/ansible-tmp-1533132004.58-223000841853605/source", 
    "state": "file", 
    "uid": 0
}
[root@promote .ssh]# ansible webservers -a 'ls -l /opt'
192.168.174.208 | SUCCESS | rc=0 >>
總用量 4
-rw-rw-rw-. 1 root root 465 8月   1 22:00 fstab.bk
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

6.file模塊

用於設置被管理主機的文件屬性

#設置/opt/fstab.bk的所屬主爲mysql,所屬組爲mysql,權限爲644
[root@promote .ssh]# ansible webservers -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.bk'
192.168.174.208 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "group": "mysql", 
    "mode": "0644", 
    "owner": "mysql", 
    "path": "/opt/fstab.bk", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 465, 
    "state": "file", 
    "uid": 306
}
[root@promote .ssh]# ansible webservers -a 'ls -l /opt'
192.168.174.208 | SUCCESS | rc=0 >>
總用量 4
-rw-r--r--. 1 mysql mysql 465 8月   1 22:00 fstab.bk
drwxr-xr-x. 2 root  root    6 3月  26 2015 rh

7.ping模塊

檢測指定被管理主機的連通性

[root@promote .ssh]# ansible webservers -m ping 
192.168.174.208 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

8.yum模塊

負責在被管理主機上安裝與卸載軟件包

[root@promote .ssh]# ansible webservers -m yum -a 'name=httpd'

9.service模塊

控制管理服務的運行狀態

#啓動http的服務並設置爲開機自啓
[root@promote .ssh]# ansible webservers -m service -a 'enabled=true name=httpd state=started'

10.shell模塊

Ansible中的shell模塊能夠在被管理主機上運行命令

使用無交互方式給zhangsan用戶設置密碼
[root@promote .ssh]# ansible webservers -m shell -a 'echo abc123 | passwd --stdin zhangsan'
192.168.174.208 | SUCCESS | rc=0 >>
更改用戶 zhangsan 的密碼 。
passwd:全部的身份驗證令牌已經成功更新。

11.script模塊

將本地腳本複製到被管理主機上進行運行

#編寫本機腳本test.sh,複製到被管理主機上進行運行
[root@promote .ssh]# vim test.sh
#!/bin/bash
echo "this is test script" >/opt/fstab.bk
[root@promote .ssh]# chmod +x test.sh #給腳本添加可執行權限
[root@promote .ssh]# ansible webservers -m script -a 'test.sh'
192.168.174.208 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.174.208 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.174.208 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
[root@promote .ssh]# ansible webservers -a 'cat /opt/fstab.bk' 
192.168.174.208 | SUCCESS | rc=0 >>
this is test script

12.setup模塊

用於模塊收集、查看被管理主機的facts。

[root@promote .ssh]# ansible webservers -m setup
相關文章
相關標籤/搜索