【0725】自動化運維——ansible

24.15 ansible介紹

  • 不需要安裝客戶端,通過 sshd 去通信

  • 基於模塊工作,模塊可以由任何語言開發

  • 不僅支持命令行使用模塊,也支持編寫 yaml 格式的 playbook,易於編寫和閱讀

  • 安裝十分簡單,centos 上可直接 yum 安裝

  • 有提供UI(瀏覽器圖形化)www.ansible.com/tower,收費的

  • 官方文檔 http://docs.ansible.com/ansible/latest/index.html

  • ansible 已經被 redhat 公司收購,它在 github 上是一個非常受歡迎的開源軟件,github 地址https://github.com/ansible/ansible

  • 一本不錯的入門電子書https://ansible-book.gitbooks.io/ansible-first-book/


24.16 ansible安裝

1、準備兩臺機器,arslinux-01,arslinux-02

2、在 arslinux-01 上安裝 ansible

[root@arslinux-01 ~]# yum list|grep ansible
[root@arslinux-01 ~]# yum install -y ansible ansible-doc

3、在 arslinux-01 上生成密鑰對

[root@arslinux-01 ~]# ssh-keygen -t rsa

如果 /root/.ssh/ 下有 id_rsa.pub 則不需要生成密鑰對

4、將公鑰放到 arslinux-01,arslinux-02 上的 /root/.ssh/authorized_keys 中

5、驗證連接

[root@arslinux-01 ~]# ssh 192.168.194.132
Last login: Sun Aug  4 21:08:02 2019 from 192.168.194.1

6、配置主機組

[root@arslinux-01 ~]# vim /etc/ansible/hosts
[testhost]
127.0.0.1
192.168.194.132

說明: testhost 爲主機組名字,自定義的。 下面兩個 ip 爲組內的機器 ip


24.17 ansible 遠程執行命令

  • ansible testhost -m command -a '命令'          批量遠程命令

這裏的 testhost 爲主機組名,-m 後邊是模塊名字,-a 後面是命令。當然我們也可以直接寫一個 ip,針對某一臺機器來執行命令

[root@arslinux-01 ~]# ansible testhost -m command -a 'w'
127.0.0.1 | CHANGED | rc=0 >>
21:38:20 up  1:11,  3 users,  load average: 0.25, 0.14, 0.15
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    117月19 24days  0.05s  0.05s -bash
root     pts/1    192.168.194.1    21:07    4.00s  2.63s  0.00s ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/6220ae23ea -tt arslinux-02 /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1564925899.21-98869728293746/AnsiballZ_command.py && sleep 0'
root     pts/4    localhost        21:38    0.00s  0.25s  0.01s w
arslinux-02 | CHANGED | rc=0 >>

21:38:21 up  3:17,  3 users,  load average: 0.08, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    146月19 50days  0.04s  0.04s -bash
root     pts/1    192.168.194.1    21:08   53.00s  0.04s  0.04s -bash
root     pts/2    arslinux-01      21:38    1.00s  0.32s  0.01s w
[root@arslinux-01 ~]# ansible 192.168.194.132 -m command -a 'w'
arslinux-02 | CHANGED | rc=0 >>
21:38:52 up  3:18,  3 users,  load average: 0.05, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    146月19 50days  0.04s  0.04s -bash
root     pts/1    192.168.194.1    21:08    1:24   0.04s  0.04s -bash
root     pts/2    arslinux-01      21:38    1.00s  0.43s  0.01s w

錯誤: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

解決: yum install -y libselinux-python

  • ansible testhost -m shell -a '命令'          shell 模塊同樣可以實現遠程執行的命令

[root@arslinux-01 ~]# ansible testhost -m shell -a 'hostname'
arslinux-02 | CHANGED | rc=0 >>
arslinux-02

127.0.0.1 | CHANGED | rc=0 >>
arslinux-01


24.18 ansible拷貝文件或目錄

  • ansible arslinux-02 -m copy -a 'src= dest= owner= group= mode= '          拷貝目錄或文件

[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/ansible dest=/tmp/ansible_test owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
    "changed": true,
    "dest": "/tmp/ansible_test/",
    "src": "/etc/ansible"
}
[root@arslinux-02 ~]# ll -d /tmp/ansible_test/
drwxr-xr-x 3 root root 21 8月   4 22:02 /tmp/ansible_test/
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:03:09 CST

注意:源目錄會放到目標目錄下面去,如果目標指定的目錄不存在,它會自動創建。

如果拷貝的是文件,dest 指定的名字和源如果不同,並且它不是已經存在的目錄,相當於拷貝過去後又重命名。但相反,如果 desc 是目標機器上已經存在的目錄,則會直接把文件拷貝到該目錄下面

  • 針對文件操作

[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/passwd dest=/tmp/123 owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "66cfbbd6ccbbfb5edb8b3d364df81d2d9ce9e619",
    "dest": "/tmp/123",
    "gid": 0,
    "group": "root",
    "md5sum": "d5a72a116f1f47476e3156915f62972e",
    "mode": "0755",
    "owner": "root",
    "size": 1776,
    "src": "/root/.ansible/tmp/ansible-tmp-1564927633.07-72798416414339/source",
    "state": "file",
    "uid": 0
}
[root@arslinux-02 ~]# ll /tmp/123
-rwxr-xr-x 1 root root 1776 8月   4 22:07 /tmp/123
[root@arslinux-02 ~]# tail -3 /tmp/123
pure-ftp:x:1020:1020::/home/pure-ftp:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

這裏的/tmp/123和源機器上的/etc/passwd是一致的,但如果目標機器上已經有/tmp/123目錄,則會再/tmp/123目錄下面建立passwd文件


24.19 ansible遠程執行腳本

1、創建一個腳本

[root@arslinux-01 ~]# vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt

2、分發腳本

[root@arslinux-01 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "6da17d4e84617796e1b3c7bfdd083d93",
    "mode": "0755",
    "owner": "root",
    "size": 49,
    "src": "/root/.ansible/tmp/ansible-tmp-1564928697.25-67620899139563/source",
    "state": "file",
    "uid": 0
}
127.0.0.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/test.sh",
    "size": 49,
    "state": "file",
    "uid": 0
}

3、執行腳本

[root@arslinux-01 ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.194.132 | CHANGED | rc=0 >>


127.0.0.1 | CHANGED | rc=0 >>
[root@arslinux-02 ~]# ll /tmp/
總用量 8
-rwxr-xr-x 1 root  root  1776 8月   4 22:07 123
drwxr-xr-x 3 root  root    21 8月   4 22:02 ansible_test
-rwxr-xr-x 1 root  root    49 8月   4 22:24 test.sh
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:26:22 CST

腳本需要 755 權限,如果不是 755 權限,執行不了

4、shell 模塊,還支持遠程執行命令並且帶管道,而 command 不支持

[root@arslinux-01 ~]# ansible testhost -m command -a "cat /etc/passwd |wc -l"
192.168.194.132 | FAILED | rc=1 >>
cat:無效選項 -- l
Try 'cat --help' for more information.non-zero return code

127.0.0.1 | FAILED | rc=1 >>
cat:無效選項 -- l
Try 'cat --help' for more information.non-zero return code

[root@arslinux-01 ~]# ansible testhost -m shell -a "cat /etc/passwd |wc -l"
192.168.194.132 | CHANGED | rc=0 >>
25

127.0.0.1 | CHANGED | rc=0 >>
37

ansible 需要先將腳本寫好並分發到各機器上,然後在批量執行腳本

saltstack 則可以批量遠程執行腳本,不需要分發


24.20 ansible管理任務計劃

  • ansible 組名/ip/機器名 -m cron -a "name=' ' job=' ' weekday= "          遠程管理任務計劃

[root@arslinux-01 ~]# ansible 192.168.194.132 -m cron -a "name='test cron' job='/bin/touch /tmp/1234546.txt' weekday=6"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test cron"
    ]
}
[root@arslinux-02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1234546.txt

說明:cron 是模塊;name 自定義 crontab任務的名稱;job 指的是任務;weekday 指每週幾

其他的時間表示:分鐘 minute 小時 hour 日期 day 月份 month

  • ansible 組名/ip/機器名 -m cron -a "name=' ' state=absent"          刪除 cron

[root@arslinux-01 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
[root@arslinux-02 ~]# crontab -l
[root@arslinux-02 ~]#


24.21 ansible安裝包和管理服務

  • ansible 組名/ip/機器名 -m yum -a "name=包名"          遠程 yum 安裝包

  • ansible 組名/ip/機器名 -m yum -a "name=包名             state=removed" 遠程卸載

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd"
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=removed"

重新安裝、啓動並設置開機啓動

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=remove state=installed"
  • ansible 組名/ip/機器名 -m service -a "name=  state=  enabled= "         啓動服務並設開機啓動

[root@arslinux-01 ~]# ansible 192.168.194.132 -m service -a "name=httpd state=started enabled=no"
[root@arslinux-02 ~]# ps aux|grep httpd
root      11746  0.3  0.5 224052  5000 ?        Ss   23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11747  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11749  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11750  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11751  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11752  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
root      11769  0.0  0.0 112724   988 pts/1    R+   23:07   0:00 grep --color=auto httpd
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 23:07:43 CST

說明:name 是服務名稱;state 是操作、狀態;enabled 指是否開機啓動

  • Ansible文檔的使用

ansible-doc -l   列出所有的模塊

ansible-doc cron   查看指定模塊的文檔


未完待續



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章