轻松使用Ansible

2019-02-02  本文已影响3人  louyang

Ansible是一种自动化工具,通过SSH管理远端的服务器。
Ansible也是一种编程语言,可以通过这种语言来描述我们要做的事情。

1 安装

我在Fedora 29上,使用下面命令安装了ansible。

dnf install ansible

检测ansible是否安装好了

ansible --version

建立一个独立的ansible工作平台

cp -R /etc/ansible/ myplatform
2 编辑hosts

假设有两台远端服务器需要管理,先用ssh-copy-id把公钥拷过去,参见https://www.jianshu.com/p/f5064defb140。目的是SSH可以免密码登陆。

进入myplatform目录,编辑该目录下的hosts文件:

10.107.184.20 ansible_user=cranuser8
10.109.3.244 ansible_user=cbam

运行ansible ping

[myplatform]$ ansible -m ping all
10.107.184.20 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
10.109.3.244 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
3 查看hostname和用户名
[myplatform]$ ansible -m shell -a 'hostname' all
10.107.184.20 | CHANGED | rc=0 >>
controller-2

10.109.3.244 | CHANGED | rc=0 >>
elam-cbam-18-0-02-single.cbaminternal
myplatform]$ ansible -m shell -a 'whoami' all
10.107.184.20 | CHANGED | rc=0 >>
cranuser8

10.109.3.244 | CHANGED | rc=0 >>
cbam
4 在远端运行脚本

我们编写一个简单的python脚本:

#!/usr/bin/python
import platform
print('hello ' + platform.node())

在本地运行,是这样的效果:

$ ./a.py
hello localhost.localdomain

通过ansible,可以使这个脚本在所有远端服务器上运行:

$ ansible -m script -a a.py all
10.109.3.244 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 10.109.3.244 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 10.109.3.244 closed."
    ],
    "stdout": "hello elam-cbam-18-0-02-single.cbaminternal\r\n",
    "stdout_lines": [
        "hello elam-cbam-18-0-02-single.cbaminternal"
    ]
}
10.107.184.20 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 10.107.184.20 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 10.107.184.20 closed."
    ],
    "stdout": "hello controller-2\r\n",
    "stdout_lines": [
        "hello controller-2"
    ]
}
5 编写playbook

编写playbook,就像是写一个剧本,方法如下:

在myplatform下创建一个playbook.yml:

- name: Play 1
  hosts: all
  tasks:
    - name: Execute command 'hostname'
      command: hostname
    - name: Execute command 'whoami'
      command: whoami
    - name: Run a script
      script: ./a.py

运行剧本:

[myplatform]$ ansible-playbook playbook.yml

PLAY [Play 1] ************************************************************************

TASK [Gathering Facts] ***************************************************************
ok: [10.109.3.244]
ok: [10.107.184.20]

TASK [Execute command 'hostname'] ****************************************************
changed: [10.109.3.244]
changed: [10.107.184.20]

TASK [Execute command 'whoami'] ******************************************************
changed: [10.107.184.20]
changed: [10.109.3.244]

TASK [Run a script] ******************************************************************
changed: [10.107.184.20]
changed: [10.109.3.244]

PLAY RECAP ***************************************************************************
10.107.184.20              : ok=4    changed=3    unreachable=0    failed=0
10.109.3.244               : ok=4    changed=3    unreachable=0    failed=0

如果想看到命令的输出,可以加-v参数。

参考

https://www.youtube.com/watch?v=icR-df2Olm8
https://www.youtube.com/watch?v=pRZA9ymZXn0
https://www.youtube.com/watch?v=Z01b9QZG0D0&list=PL2We04F3Y_42_PN52bT_U5o_lt6uPQqqq&index=4

上一篇下一篇

猜你喜欢

热点阅读