ansiable的安装部署

所需安装包

ansible-2.7.8-1.el7.noarch.rpm
ansible-tower-setup-bundle-3.4.2-1.el7.tar.gz
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm

实验环境
三台主机:
172.25.2.104 server4 服务端
172.25.2.105 server5 客户端
172.25.2.106 server6 客户端
1.安装

[root@server4 ansible]# yum install -y *

[root@server5 ansible]# yum install -y *

[root@server6 ansible]# yum install -y *

2.添加普通用户devops

[root@server4 ~]# useradd devops
[root@server4 ~]# id devops
uid=1001(devops) gid=1001(devops) groups=1001(devops)
[root@server4 ~]# passwd devops  ##设置密码为redhat

[root@server4 ~]# useradd devops
[root@server4 ~]# id devops
uid=1001(devops) gid=1001(devops) groups=1001(devops)
[root@server4 ~]# passwd devops  ##设置密码为redhat

[root@server4 ~]# useradd devops
[root@server4 ~]# id devops
uid=1001(devops) gid=1001(devops) groups=1001(devops)
[root@server4 ~]# passwd devops  ##设置密码为redhat

3.编辑ansible配置文件

devops@server4 ~]$ mkdir ansible
[devops@server4 ~]$ cd ansible/
[devops@server4 ansible]$ ls
[devops@server4 ansible]$ vim inventory
[devops@server4 ansible]$ cat inventory
[all]
172.25.2.105
172.25.2.106
[devops@server4 ansible]$ vim ansible.cfg
[devops@server4 ansible]$ cat ansible.cfg
[defaults]
inventory=./inventory

4.设置免密登陆

[devops@server4 ansible]$ ssh-keygen  ##服务端生成秘钥
Generating public/private rsa key pair.
Enter file in which to save the key (/home/devops/.ssh/id_rsa): 
Created directory '/home/devops/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/devops/.ssh/id_rsa.
Your public key has been saved in /home/devops/.ssh/id_rsa.pub.
The key fingerprint is:
a1:9d:7c:01:66:fe:bc:61:a5:13:1d:47:35:a9:ae:fc devops@server4
The key's randomart image is:
+--[ RSA 2048]----+
|        +   ..ooo|
|       + . . o ..|
|        o o o .  |
|       + = = .   |
|      . S O .    |
|         o + .   |
|          o .    |
|           o     |
|            .E   |
+-----------------+

[devops@server4 ansible]$ ssh-copy-id server5  ##将私钥发送给客户端
[devops@server4 ansible]$ ssh-copy-id server6

5.测试ansible

[devops@server4 ansible]$ ansible all --list-hosts  ##列出所有主机
  hosts (2):
    172.25.2.105
    172.25.2.106
[devops@server4 ansible]$ ansible  all -m ping
172.25.2.106 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.25.2.105 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[devops@server4 ansible]$ ansible all -m copy -a 'src=inventory dest=/tmp'  ##执行copy命令
172.25.2.105 | CHANGED => {
    "changed": true, 
    "checksum": "615c39e4577832f185db750ac4e7d4e394d2b1ad", 
    "dest": "/tmp/inventory", 
    "gid": 1001, 
    "group": "devops", 
    "md5sum": "0c1ed0d80a5245ed231bf5dd5c885ed1", 
    "mode": "0664", 
    "owner": "devops", 
    "size": 32, 
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1564026789.6-28457061654644/source", 
    "state": "file", 
    "uid": 1001
}
172.25.2.106 | CHANGED => {
    "changed": true, 
    "checksum": "615c39e4577832f185db750ac4e7d4e394d2b1ad", 
    "dest": "/tmp/inventory", 
    "gid": 1001, 
    "group": "devops", 
    "md5sum": "0c1ed0d80a5245ed231bf5dd5c885ed1", 
    "mode": "0664", 
    "owner": "devops", 
    "size": 32, 
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1564026789.61-28207580764894/source", 
    "state": "file", 
    "uid": 1001
}
[devops@server4 ansible]$ ansible all -m command -a 'ls /tmp'
172.25.2.106 | CHANGED | rc=0 >>
ansible_command_payload_FUgirx
inventory

172.25.2.105 | CHANGED | rc=0 >>
ansible_command_payload_w1ThBu
inventory

[devops@server4 ansible]$ ansible all -m command -a 'rm /tmp/inventory'
 [WARNING]: Consider using the file module with state=absent rather than
running 'rm'.  If you need to use command because file is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.

172.25.2.105 | CHANGED | rc=0 >>


172.25.2.106 | CHANGED | rc=0 >>

你可能感兴趣的:(ansiable的安装部署)