安装与使用ansible-Centos6.5
Ansible安装与使用
安装epel源
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
yum clean all
利用yum进行安装
yum install ansible -y
#出现了报错,可以发现是因为缺少依赖包
Error: Package: ansible-1.9.4-1.el6.noarch (epel)
Requires: PyYAML
Error: Package: ansible-1.9.4-1.el6.noarch (epel)
Requires: python-jinja2
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
下载相关依赖包PyYAML
、python-jinja2
- 推荐一个自认为很好用的rpm包下载地址:RPM SEARCH
python-Jinja2-2.6-19.3.noarch.rpm
PyYAML-3.10-3.1.el6.x86_64.rpm
rpm -ivh python-Jinja2-2.6-19.3.noarch.rpm
rpm -ivh PyYAML-3.10-3.1.el6.x86_64.rpm
#继续依赖libyaml
warning: PyYAML-3.10-3.1.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:
libyaml-0.so.2()(64bit) is needed by PyYAML-3.10-3.1.el6.x86_64
rpm -ivh libyaml-0.1.4-2.3.x86_64.rpm PyYAML-3.10-3.1.el6.x86_64.rpm
再次尝试安装,基本就可以成功
yum install ansible -y
简单测试(测试之前实现ansible主机与测试机之间的无密码登录)
[root@localhost yum.repos.d]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
79:fe:76:78:92:11:77:5a:63:79:da:5b:04:85:02:06 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| E.o. o.|
| . . o |
| . ..|
| . . .+=|
| S . o.Bo|
| o . o o|
| . + o|
| .= o. |
| ..+ |
+-----------------+
[root@localhost yum.repos.d]# ssh-copy-id root@192.168.1.103
root@192.168.1.103's password:
Now try logging into the machine, with "ssh 'root@192.168.1.103'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@localhost yum.repos.d]# ssh-copy-id root@192.168.1.105
root@192.168.1.105's password:
Now try logging into the machine, with "ssh 'root@192.168.1.105'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren
制作hosts
[root@localhost yum.repos.d]# cat /etc/ansible/hosts
[test]
192.168.1.103
192.168.1.105
[root@localhost yum.repos.d]# ansible test -m shell -a "hostname"
192.168.1.105 | success | rc=0 >>
localhost.localdomain
192.168.1.103 | success | rc=0 >>
localhost.localdomain
[root@localhost yum.repos.d]
简单介绍比较常用的几个模块
-
copy模块 :把主控端文件拷贝到到指定节点上:
ansible test -m copy -a 'src=XXXX dest=XXXX'
-
file模块 :更改指定节点上文件的权限:
ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"
-
cron模块 :在指定节点上定义一个计划任务:
ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
-
group模块 :在所有节点上创建一个组:
ansible all -m group -a 'gid=2014 name=nolinux'
-
user模块 :在指定节点上创建一个用户:
ansible 10.1.1.113 -m user -a 'name=nolinux groups=nolinux state=present'
-
yum模块 :在指定节点上安装服务:
ansible all -m yum -a "state=present name=httpd"
-
service模块 :启动指定节点上的服务,并让其开机自启动:
ansible test -m service -a 'name=puppet state=restarted enabled=yes'
-
script模块 :在指定节点上执行脚本(该脚本是在ansible控制节点上的):
ansible test -m script -a '/root/a.sh'
-
ping模块 :检查指定节点机器是否还能连通:
ansible test -m ping
-
command模块 :在指定节点上运行命令:
ansible test -m command -a 'hostname'
-
raw模块 :在节点上运行命令:
ansible test -m raw-a 'hostname|tee'
-
get_url模块 :将文件下载到指定节点的/tmp目录下:
ansible test -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
-
synchronize模块:将主控方目录推送到指定节点的目录下:
ansible test -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'