操作系统:centos6.8
服务端ip:192.168.137.142
客户端ip: 192.168.137.34 192.168.137.33
安装ansible
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum clean all
yum repolist
yum install -y ansible
生成秘钥,让服务端和客户端能进行免密钥
ssh-keygen -t dsa #一路按回车键即可
cat /root/.ssh/id_dsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
#按回车键,然后输入yes,最后输入客户端的服务器密码
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
#按回车键,然后输入yes,最后输入客户端的服务器密码
配置ansible
# vim /etc/ansible/ansible.cfg
[defaults]
hostfile =/etc/ansible/hosts
library =/usr/share/ansible
remote_tmp =$HOME/.ansible/tmp
pattern = *
forks = 5
poll_interval = 15
sudo_user = root
transport = smart
remote_port = 22
timeout = 10
添加主机
# vim /etc/ansible/hosts
[test] #自定义主机组名
192.168.137.34 #添加客户端的免密钥登录ip
192.168.137.33
3.常用模块使用
(1).setup
#用来查看远程主机的一些基本信息
ansible test -m setup #有绿色内容显示为执行成功
(主机组) (模块)
(2).ping
#用来测试远程主机的运行状态
ansible test -m ping
192.168.137.34 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.137.33 | SUCCESS => {
"changed": false,
"ping": "pong"
(3).file
#设置文件的属性
相关选项如下:
force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group: 定义文件/目录的属组
mode: 定义文件/目录的权限
owner: 定义文件/目录的属主
path: 必选项,定义文件/目录的路径
recurse:递归设置文件的属性,只对目录有效
src: 被链接的源文件路径,只应用于state=link的情况
dest: 被链接到的路径,只应用于state=link的情况
state:
directory:如果目录不存在,就创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
例子:
# 远程文件符号链接创建,当客户端在/home目录存在文件test_a,并且其他用户拥有执行权限,显示绿色为执行成功
ansible test -m file -a "src=/home/test_a dest=/tmp/test_a state=link"
192.168.137.33 | SUCCESS => {
"changed": false,
"dest": "/tmp/test_a",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 12,
"src": "/home/test_a",
"state": "link",
"uid": 0
}
192.168.137.34 | SUCCESS => {
"changed": false,
"dest": "/tmp/test_a",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 12,
"src": "/home/test_a",
"state": "link",
"uid": 0
}
#远程文件信息查看
ansible test -m command -a "ls -l /home/test_a"
192.168.137.33 | SUCCESS | rc=0 >>
-rwxr-xr-x. 1 root root 14209 Sep 13 05:11 /home/test_a
192.168.137.34 | SUCCESS | rc=0 >>
-rwxr-xr-x. 1 root root 12 Sep 13 10:34 /home/test_a
## 将本地文件“/etc/ansible/ansible.cfg”复制到远程服务器
ansible test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
192.168.137.34 | SUCCESS => {
"changed": true,
"checksum": "1b4bbebae90acabf3b33da80ddcb2b4564a8d323",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "56d60e05c5f0e6c503b82403281dbbcf",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 18307,
"src": "/root/.ansible/tmp/ansible-tmp-1505325737.38-134417600986364/source",
"state": "file",
"uid": 0
}
192.168.137.33 | SUCCESS => {
"changed": true,
"checksum": "1b4bbebae90acabf3b33da80ddcb2b4564a8d323",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "56d60e05c5f0e6c503b82403281dbbcf",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 18307,
"src": "/root/.ansible/tmp/ansible-tmp-1505325737.34-166041859775381/source",
"state": "file",
"uid": 0
}
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
(4).shell
## 切换到某个shell执行指定的指令
例子:
# 先在本地创建一个SHELL脚本
# vim /tmp/a.sh
#!/bin/sh
echo "hello"
#chmod +x /tmp/a.sh
# 将创建的脚本文件分发到远程
ansible test -m copy -a "src=/tmp/a.sh dest=/tmp/a.sh owner=root group=root mode=0755"
## 远程执行
ansible test -m shell -a "/tmp/a.sh"
192.168.137.34 | SUCCESS | rc=0 >>
hello
192.168.137.33 | SUCCESS | rc=0 >>
hello
Ansinle的Playbooks
例子:
vim /etc/ansible/test.yml
- name: create_user
hosts: test
user: root
gather_facts: false
vars:
- user: "testyml"
tasks:
- name: create {{ user }}
sudo: yes
user: name="{{ user }}"
注意:
name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;
gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;
vars参数指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;
user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
执行:
cd /etc/ansible/
ansible-playbook test.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is ‘sudo‘ (default).
This feature will be removed in a future release.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
PLAY [create_user] ***********************************************************************************************************************************************************
TASK [create testyml] ********************************************************************************************************************************************************
changed: [192.168.137.33]
changed: [192.168.137.34]
PLAY RECAP *******************************************************************************************************************************************************************
192.168.137.33 : ok=1 changed=1 unreachable=0 failed=0
192.168.137.34 : ok=1 changed=1 unreachable=0 failed=0
检查:
ansible 192.168.137.33 -m shell -a "id testyml"
[[email protected] ansible]# ansible 192.168.137.33 -m shell -a "id testyml"
192.168.137.33 | SUCCESS | rc=0 >>
uid=501(testyml) gid=501(testyml) groups=501(testyml)
[[email protected] ansible]# ansible 192.168.137.34 -m shell -a "id testyml"
192.168.137.34 | SUCCESS | rc=0 >>
uid=501(testyml) gid=501(testyml) groups=501(testyml)