Ansible安裝及部分模塊初步使用

Ansible安裝及初步使用

Ansible概述

由於互聯網的快速發展導致產品更新換代速度逐漸加快,運維人員每天都要進行大量的維護操作,仍舊按照傳統的方式進行維護工作會使得工作效率低下。這時,部署自動化運維就可以儘可能安全、高效地完成這些工作。

一般會把自動化運維工具劃分爲兩類:一類是使用代理工具,與就是基於專用的Agent程序完成管理功能,如:Puppet、Func、Zabbix等;另外一類是不需要配置代理工具的,可以基於SSH服務來完成管理功能,如:Ansible、Fabric等。

Ansible基於Python開發,集合了衆多的優秀運維工具的優點,實現了批量運行命令、部署程序、配置系統等功能。默認通過SSH協議進行遠程命令執行下發配置,無需部署任何客戶端代理軟件,從而使得自動化環境部署變得更加簡單。可以同時支持多臺主機並行管理,使得管理主機更加便捷。

部署環境

角色 主機名 IP地址 組名
控制主機 rabbitmq01 192.168.58.142
被管理主機 rabbitmq02 192.168.58.132 webserver
被管理主機 rabbitmq03 192.168.58.143 dbserver

部署實施

安裝Ansible

可以使用源碼安裝也可以使用rpm包安裝,這裏直接使用yum倉庫安裝,需要依賴第三方的EPEL源。

[root@rabbitmq01 ~]# yum install -y epel-release
#安裝epel源
[root@rabbitmq01 ~]# yum install ansible -y
#安裝ansible軟件包
[root@rabbitmq01 ~]# ansible --version
ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
#安裝好後,可以查看版本信息

配置主機清單

Ansible通過默認主機清單/etc/ansible/hosts文件,修改主機與組配置後,可以連接到多個被管理主機上執行任務。

[root@rabbitmq01 ~]# vim /etc/ansible/hosts 
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups
[webserver]         #設計主機清單
192.168.58.132
[dbserver]
192.168.58.143

設置SSH密碼登錄

使用ssh-keygen產生一對密鑰(公鑰和私鑰),使用ssh-copy-id來下發生成的公鑰。

[root@rabbitmq01 ~]# ssh-keygen -t rsa  #產生密鑰
[root@rabbitmq01 ~]# cd /root/.ssh/
[root@rabbitmq01 .ssh]# ls
id_rsa  id_rsa.pub               #這就是產生的密鑰

Ansible安裝及部分模塊初步使用
下面將id_rsa.pub公鑰分發給被管理主機。

[root@rabbitmq01 .ssh]# ssh-copy-id [email protected]
#分發給被管理主機
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.58.132 (192.168.58.132)' can't be established.
ECDSA key fingerprint is SHA256:H7QKvUWPKe+VutgnWibgRHM/knIJq5CCZ7MEvSgiHXA.
ECDSA key fingerprint is MD5:8b:26:28:97:7a:d1:84:62:6f:f6:8d:2e:06:09:5e:5f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:                  #這裏輸入的是被管理主機的root密碼,不是生成的密鑰

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@rabbitmq01 .ssh]# ssh-copy-id [email protected]
#分發給另外一臺被管理主機

完成上述操作後,就可以進行ansible操作

[root@rabbitmq01 .ssh]# ansible webserver -a 'ifconfig'
Enter passphrase for key '/root/.ssh/id_rsa': 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.58.132  netmask 255.255.255.0  broadcast 192.168.58.255
        inet6 fe80::f017:cc4f:7b2b:3652  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e7:34:3b  txqueuelen 1000  (Ethernet)
        RX packets 18857  bytes 20093953 (19.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7340  bytes 584633 (570.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 790  bytes 52247 (51.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 790  bytes 52247 (51.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

設置SSH免交戶代理

但是每次操作都會被要求輸入ssh密碼,這裏設置免交戶代理。

[root@rabbitmq01 .ssh]# ssh-agent bash
[root@rabbitmq01 .ssh]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:        #輸入ssh密碼
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

這個時候我們再進行操作的時候,就不會再要求輸入密碼了。

[root@rabbitmq01 .ssh]# ansible webserver -a 'ifconfig'
192.168.58.132 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.58.132  netmask 255.255.255.0  broadcast 192.168.58.255
        inet6 fe80::f017:cc4f:7b2b:3652  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e7:34:3b  txqueuelen 1000  (Ethernet)
        RX packets 19025  bytes 20210670 (19.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7451  bytes 599404 (585.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 790  bytes 52247 (51.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 790  bytes 52247 (51.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Ansible命令應用

Ansible可以使用命令行方式進行自動化管理,管理工具是由一系列模塊、參數支持的,可以在命令後面加上-h或者--help獲取幫助,如使用ansible-doc工具可以通過ansible-doc -h或者ansible --help獲取幫助信息。

ansible-doc是也用來查看模塊幫助信息的工具,最主要的選項-l用來列出可使用的模塊,-s用來列出某個模塊的描述信息和使用示例。
Ansible安裝及部分模塊初步使用

command模塊

Ansible管理工具使用-m選項指定使用模塊,默認使用command模塊,-m選項省略也會運行此模塊,用於在被管理主機上運行命令。

[root@rabbitmq01 ~]# ansible webserver -m command -a 'date'  
#使用ansible查看被管理主機日期信息
192.168.58.132 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 14:47:26 CST

[root@rabbitmq01 ~]# ansible dbserver -a 'date'   #不使用-m時,默認就是command模塊。
192.168.58.143 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 14:47:47 CST

cron模塊

Ansible中cron模塊用於定義計劃性任務。其中有兩種狀態(state):present表示添加,absent表示移除。下面演示添加計劃性任務:

[root@rabbitmq01 ~]# ansible webserver -m cron -a 'minute="*/10" job="/usr/bin/echo hello" name="test"'
#這句話表示添加一個名稱爲test的計劃性任務,完成每十分鐘輸出hello字符串的任務
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test"
    ]
}

我們到webserver這臺被管理主機上查看是否生成了這個計劃性任務。

[root@rabbitmq02 ~]# crontab -l  #查看計劃性任務,可以看到已經生成
#Ansible: test
*/10 * * * * /usr/bin/echo hello

移除計劃性任務

[root@rabbitmq01 ~]# ansible webserver -m cron -a 'name="test" state=absent'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

[root@rabbitmq02 ~]# crontab -l
[root@rabbitmq02 ~]# 
#可以看到計劃性任務已經被移除了

user模塊

Ansible中的user模塊用於創建新用戶和更改、刪除已經存在的用戶。其中name選項用來指明創建的用戶名稱。

[root@rabbitmq01 ~]# ansible webserver -m user -a 'name="user1"'
#創建一個user1的用戶
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1000, 
    "home": "/home/user1", 
    "name": "user1", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1000
}

[root@rabbitmq02 ~]# id user1
uid=1000(user1) gid=1000(user1) 組=1000(user1)
#到webserver查看user1用戶

移除用戶

[root@rabbitmq01 ~]# ansible webserver -m user -a 'name="user1" state=absent'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "user1", 
    "remove": false, 
    "state": "absent"
}

[root@rabbitmq02 ~]# id user1
id: user1: no such user

group模塊

Ansible中的group模塊用於對用戶組進行管理。

[root@rabbitmq01 ~]# ansible webserver -m group -a 'name=yx gid=400 system=yes'
#添加一個叫yx的系統組。指定gid=400
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "gid": 400, 
    "name": "yx", 
    "state": "present", 
    "system": true
}

[root@rabbitmq02 ~]# tail -2 /etc/group
rabbitmq:x:986:
yx:x:400:
#可以看到創建成功

[root@rabbitmq01 ~]# ansible webserver -m user -a 'name="user2" uid=300 group=yx system=yes'
#創建一個叫test02的系統用戶,指定uid=300,所屬組是yx
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 400, 
    "home": "/home/user2", 
    "name": "user2", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 300
}

[root@rabbitmq02 ~]# id user2
uid=300(user2) gid=400(yx) 組=400(yx)

移除組和移除用戶方法一樣

copy模塊

Ansible中的copy模塊用於實現文件複製和批量下發文件。其中使用src來定義本地文件路徑,使用dest定義被管理主機文件路徑。使用content則是通過指定信息內容來生成目標文件。

[root@rabbitmq01 ~]# ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=600'
#將本地/etc/fstab文件複製到被管理主機webserver中的/opt/fstab.bk,屬主爲root,權限爲600
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "checksum": "317728dfdd657138fa15180e7c4318414c3d5774", 
    "dest": "/opt/fstab.bk", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b8d938e54772dcb1d95b3d5054d1f0a9", 
    "mode": "0600", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 595, 
    "src": "/root/.ansible/tmp/ansible-tmp-1533107605.28-118537359554274/source", 
    "state": "file", 
    "uid": 0
}

[root@rabbitmq02 ~]# cd /opt/
[root@rabbitmq02 opt]# ll
總用量 4
-rw-------. 1 root root 595 8月   1 15:13 fstab.bk
drwxr-xr-x. 2 root root   6 3月  26 2015 rh
#可以看到已經生成了/opt/fstab.bk文件
[root@rabbitmq01 ~]# ansible webserver -m copy -a 'content="this is test" dest=/opt/test.txt'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "checksum": "b6794b2000d94d348203d0279c2e7322b922cb16", 
    "dest": "/opt/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "8c6d115258631625b625486f81b09532", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 12, 
    "src": "/root/.ansible/tmp/ansible-tmp-1533107768.16-134608628410659/source", 
    "state": "file", 
    "uid": 0
}

[root@rabbitmq02 opt]# cat test.txt 
this is test 
#生成了test.txt這個文件

file模塊

在Ansible中使用file模塊用於設置文件屬性,其中path指定文件路徑,使用src定義源文件路徑,使用name或者dest替換創建文件的符號鏈接。

[root@rabbitmq01 ~]# ansible webserver -m file -a 'owner=mysql group=root mode=644 path=/opt/fstab.bk'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "mysql", 
    "path": "/opt/fstab.bk", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 595, 
    "state": "file", 
    "uid": 27
}

this is test[root@rabbitmq02 opt]# ll
總用量 8
-rw-r--r--. 1 mysql root 595 8月   1 15:13 fstab.bk
drwxr-xr-x. 2 root  root   6 3月  26 2015 rh
#可以看到/opt/test.txt文件屬性已經修改成功
[root@rabbitmq01 ~]# ansible webserver -m file -a 'path=/opt/test.link src=/etc/passwd state=link' 
#將在被管理主機上,設置/opt/test.link爲/etc/passwd文件的鏈接文件
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "dest": "/opt/test.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 11, 
    "src": "/etc/passwd", 
    "state": "link", 
    "uid": 0
}

[root@rabbitmq02 opt]# ll
總用量 8
-rw-r--r--. 1 mysql root 595 8月   1 15:13 fstab.bk
drwxr-xr-x. 2 root  root   6 3月  26 2015 rh
lrwxrwxrwx. 1 root  root  11 8月   1 15:21 test.link -> /etc/passwd
#生成成功

ping模塊

在Ansible中使用ping模塊檢測指定主機的連通性。

[root@rabbitmq01 ~]# ansible dbserver -m ping
192.168.58.143 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
#連接正常

yum模塊

在Ansible中的yum模塊負責在被管理主機上安裝卸載軟件包,但是要每個節點部署yum倉庫。name指定安裝的軟件包,不帶版本號默認安裝最新版本,state指定軟件包狀態,present、latest用於安裝,absent表示卸載。

[root@rabbitmq02 opt]# rpm -q httpd
未安裝軟件包 httpd 
#在webserver上,未安裝httpd

[root@rabbitmq01 ~]# ansible webserver -m yum -a 'name=httpd'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    .....
[root@rabbitmq02 ~]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64
#安裝成功

service模塊

在Ansible中使用service模塊來控制管理服務器的運行狀態。其中使用enabled表示是否開機啓動,取值true或者false;只用name定義服務名稱;使用state指定服務狀態,取值爲started、stoped、restarted。

[root@rabbitmq02 ~]# netstat -ntap | grep 80

[root@rabbitmq01 ~]# ansible webserver -m service -a 'name=httpd enabled=true state=started'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 

[root@rabbitmq02 ~]# netstat -ntap | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      7110/httpd 

shell模塊

Ansible中的shell模塊可以在管理主機上運行命令,並支持像管道符號等功能的複雜命令。

[root@rabbitmq01 ~]# ansible webserver -m shell -a 'echo "abc123"|passwd --stdin user2'
#這就是使用無交互模式給用戶設置密碼
192.168.58.132 | SUCCESS | rc=0 >>
更改用戶 user2 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。

script模塊

Ansible中的script模塊可以將本地的腳本複製到管理主機上運行。

#在本地創建一個腳本
[root@rabbitmq01 ~]# vim test.sh
[root@rabbitmq01 ~]# chmod +x test.sh 
[root@rabbitmq01 ~]# cat test.sh 
#!/bin/bash
echo "this is a test" > /opt/script.txt
[root@rabbitmq01 ~]# ansible webserver -m script -a '/root/test.sh'
192.168.58.132 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.58.132 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.58.132 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

[root@rabbitmq02 opt]# ls
fstab.bk  rh  script.txt  test.link  test.txt
[root@rabbitmq02 opt]# cat script.txt 
this is a test
#腳本執行成功

setup模塊

在Ansible中使用setup模塊收集、查看被管理主機的facts(facts是Ansible採集被管理主機設備信息的一個功能)。每個被管理主機在接受並運行管理命令之前,都會將自己的相關信息發送給控制主機。

[root@rabbitmq01 ~]# ansible webserver -m setup
192.168.58.132 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.58.132"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::f017:cc4f:7b2b:3652"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
....省略,信息很多
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章