1. 安装方式
- Ubuntu
$ sudo apt-get install -y ansible
- CentOS
$ sudo yum install -y ansible
- MacOS
$ brew install ansible
- Container
$ docker pull ansible/ansible
2. 配置方式
在大规模的配置管理工作中我们需要管理不同业务的机器,这些机器的信息都存放在ansible的inventory组件里。在我们工作中配置部署针对的主机必须先存放在inventory里,这样才能使用ansible对它进行操作。
默认ansible的inventory是一个静态的init文件/etc/ansible/hosts
,亦可通过ANSIBLE_HOSTS环境变量指定或者命令运行时用-i参数临时设置。
$ cat /etc/ansible/hosts
100.0.0.1 ansible_ssh_pass='123456'
100.0.0.2 ansible_ssh_pass='123456'
[docker]
100.0.0.1[1:3]
[docker:vars]
ansible_ssh_pass='123456'
[ansible:children]
docker
- 第一、二行定义一个主机,指定ssh登录密码
- 第三行定义了一个叫docker的组
- 第四行定义了docker组下面四个主机从100.0.0.11-100.0.0.13
- 第五、六行定义了docker组的ssh登录密码
- 第七、八行定义了ansible组,ansible组包含docker组
3. 控制方式
Ad-Hoc Command
命令格式:ansible <host-pattern> [options]
- ping模块
作用:判断远程主机的网络是否畅通
$ ansible 100.0.0.1 -m ping
100.0.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
- copy模块
作用:将ansible执行机器上的文件拷贝到远程节点
//[-a]:表示args的意思
$ ansible 100.0.0.1 -m copy -a 'src=/root/install.log dest=/tmp/install.log owner=testuser group=testgroup'
100.0.0.1 | success >> {
"changed": true,
"checksum": "7b3626c84bb02d12472c03d2ece878fdc4756c94",
"dest": "/tmp/install.log",
"gid": 1100,
"group": "testgroup",
"md5sum": "c7d8a01a077940859e773b7770d2e07e",
"mode": "0644",
"owner": "testuser",
"size": 9458,
"src": "/root/.ansible/tmp/ansible-tmp-1456387213.94-229503410500766/source",
"state": "file",
"uid": 1000
}
- shell模块
作用:它负责在被ansible控制的节点执行命令行。shell 模块是通过/bin/sh进行执行,所以shell 模块可以执行任何命令,就像在本机执行一样
//[-i]:后面跟的是inventory档案,表示指定inventory
//指令解释:先cd到某个需要编译的目录,执行condifgure然后,编译,然后安装
$ ansible -i hosts all -m shell -a "./configure && make && make insatll" chdir=/xxx/yyy
- command模块
作用:command 模块用于运行系统命令。不支持管道符和变量等("<", ">", "|", and "&"等),如果要使用这些,那么可以使用shell模块。在使用ansible中的时候,默认的模块是-m command,从而模块的参数不需要填写,直接使用即可
//命令解释:ansible命令行调用-m command模块 -a表示使用参数 “”内的为执行的command命令,该命令会将hosts定义的所有节点都会执行关机
$ ansible -i hosts all -m command -a "/sbin/shutdown -t now"
- raw模块
作用:raw模块的功能与shell和command类似。但raw模块运行时不需要在远程主机上配置python环境
$ ansible 100.0.0.1 -m raw-a 'hostname|tee'
- fetch模块
作用:文件拉取模块主要是将远程主机中的文件拷贝到本机中,和copy模块的作用刚刚相反,并且在保存的时候使用hostname来进行保存,当文件不存在的时候,会出现错误,除非设置了选项fail_on_missing为yes
$ ansible pythonserver -m fetch -a "src=/root/123 dest=/root"
SSH password:
100.0.0.1 | success >> {
"changed": true,
"dest": "/root/100.0.0.1/root/123",
"md5sum": "31be5a34915d52fe0a433d9278e99cac",
"remote_md5sum": "31be5a34915d52fe0a433d9278e99cac"
}
- file模块
作用:主要用来设置文件、链接、目录的属性,或者移除文件、链接、目录,很多其他的模块也会包含这种作用,例如copy,assemble和template
$ ansible 100.0.0.1 -m file -a "path=/root/123 owner=kel group=kel mode=0644"
SSH password:
100.0.0.1 | success >> {
"changed": true,
"gid": 500,
"group": "kel",
"mode": "0644",
"owner": "kel",
"path": "/root/123",
"size": 294,
"state": "file",
"uid": 500
}
- service模块
作用:service模块其实就是linux下的service命令。用于service服务管理
//启动服务
$ ansible 100.0.0.1 -m service -a "name=httpd state=started" host31 | SUCCESS => { "changed": true, "name": "httpd", "state": "started" }
//停止服务
$ ansible 100.0.0.1 -m service -a "name=httpd state=stopped" host31 | SUCCESS => { "changed": true, "name": "httpd", "state": "stopped" }
- 除了以上模块外,还有cron模块、user模块、group模块、script模块、get_url模块、synchronize模块、mount模块、unarchive模块(解压文件模块)等
Playbook
$ cat hello_world.yml
---
- name: say 'hello world'
hosts: all
tasks:
- name: echo 'hello world'
command: echo 'hello world'
register: result
- name: print stdout
debug:
msg: ""
$ ansible-playbook hello_world.yml
PLAY [say 'hello world'] *******************************************************
TASK [setup] *******************************************************************
ok: [server1]
TASK [echo 'hello world'] ******************************************************
changed: [server1]
TASK [print stdout] ************************************************************
ok: [server1] => {
"msg": "hello world"
}
PLAY RECAP *********************************************************************
100.0.0.1 : ok=3 changed=1 unreachable=0 failed=0