而微软 发表于 2015-11-19 09:15:46

ansible 安装与配置

ansible 安装与配置以及实现运维自动化
一、ansible介绍
ansible是新出现的运维工具是基于Python研发的糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。

实验环境介绍:
ansible          Centos 6.6 x86_64   hostname:ansible
web1            Centos 6.6 x86_64   hostname:web1
web2            Centos 6.6 x86_64   hostname:web2            
1、在服务端安装ansible,不需要在客户端安装。
首先安装epel源
#rpm -ivh http://fr2.rpmfind.net/linux/epel/6/x86_64/epel-release-6-8.noarch.rpm
安装ansible
#yum install ansible -y
2、批量建立服务端和客户端的ssh信任
如果管理的客户端比较多使用脚本跑一下(不再赘述)

如果不建立服务端与客户端的ssh信任,相对安全,但是每次都有需要输入远端服务器密码,如下:
#ansible 192.168.1.20 -m ping -k   
SSH password:   --输入远端服务器的root密码
192.168.1.2 | success >> {
    "change": false,
    "ping": "pong"
}
注:服务端与客户端没有配置SSH证书信任,需要在执行ansible命令时添加 -k 参数,需要提供root(默认)账号密码
有些人更倾向于使用普通用户账户进行连接并使用sudo命令实现root权限
格式:
#ansible webservers -m ping -u ansible -s
我本人直接使用root用户!

3、ansible 配置ansible服务端与客户端ssh信任关系(客户端比较少的情况):
在服务端
#ssh-keygen -t rsa         --生成加密串儿,一路回车
{ssh-keygen -t rsa -P ''   --密钥设置为空}

拷贝key到客户端
# ssh-copy-id-i ~/.ssh/id_rsa.pub root@172.16.29.193
The authenticity of host '172.16.29.193 (172.16.29.193)' can't be established.
RSA key fingerprint is 0d:2c:da:c7:2b:2c:38:d3:28:bc:78:65:f4:dc:af:4f.
Are you sure you want to continue connecting (yes/no)? yes   --输入yes
Warning: Permanently added '172.16.29.193' (RSA) to the list of known hosts.
root@172.16.29.193's password:                               --输入172.16.29.193服务器的root密码
Now try logging into the machine, with "ssh 'root@172.16.29.193'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

到客户端查看key
# ls ~/.ssh/authorized_keys
/root/.ssh/authorized_keys

在服务端端ssh登录客户端测试
#ssh 172.16.29.193
可以正常登录
(## 写入信任文件(将/root/.ssh/id_rsa_storm1.pub分发到其他服务器,并在所有服务器上执行如下指令):
# cat /root/.ssh/id_rsa_storm1.pub >> /root/.ssh/authorized_keys
# chmod 600 /root/.ssh/authorized_keys)
4、服务端配置:
# cat hosts

10.0.90.24
10.0.90.25

172.16.29.193
注:
hosts文件定义:如果没有配置服务端通过ssh无密码登录客户端,hosts文件配置如下:

172.16.10.22 ansible_ssh_user=root ansible_ssh_pass=mima
172.16.10.33 ansible_ssh_user=root ansible_ssh_pass=mima
解释
#ansible_ssh_user=root 是ssh登陆用户
#ansible_ssh_pass=mima 是ssh登陆密码

简单测试:
# ansible 172* -m shell -a "hostname"
172.16.29.193 | success | rc=0 >>
guang
# ansible host1 -m shell -a "hostname"
172.16.29.193 | success | rc=0 >>
guang
以上两种方式都可以,其中模块shell 也可以换成command
# ansible host1 -m command -a 'date'
172.16.29.193 | success | rc=0 >>
Wed Jun 10 22:37:20 CST 2015

默认的模块名为command ,即“-m command” 可以省略
如:
ansible host1 -m command -a "uptime"等价与ansible host1 -a "uptime"
#ansible host1 -m service -a"name=httpd state=restarted"

5、ansible 管理系统用户
首先生成密码:
#openssl passwd -1 -salt 12345678
Password:    --输入密码,就会生成加密字符串
创建:
#ansible web -m user -a 'name=test1 comment="add a test user" password="$1$12345678$qT.Vr20lsSaufZbuk4JIb."'
删除:
#ansible web -m user -a "name=test1 state=absent"   --使用这种方式删除用户,不会删除用户的家目录
#ansible web -m user -a "name=test1 state=absent remove=yes" --使用这种方式删除用户,可以删除用户的家目录
ansible 使用普通用户操作
#su - test
$ansible webservers -m ping -u test1 -sudo

使用yml文件批量创建用户,并且将用户添加到wheel组,如果不想添加到wheel组,去掉groups=wheel即可(未设置密码)
#cat add_user.yml
---
- hosts: web
remote_user: root
gather_facts: true
tasks:
- name: Add several users
    user: name={{ item }} state=present groups=wheel
    with_items:
   - testuser1
   - testuser2
执行:
ansible-playbook add_user.yml

批量删除用户:   --可以将用户家目录也删除,从whell组中删除
#cat del_user.yml
---
- hosts: web
remote_user: root
gather_facts: true
tasks:
- name: del several users
    user: name={{ item }} state=absent remove=yes
    with_items:
   - testuser1
   - testuser2   
执行:
ansible-playbook del_user.yml

批量创建用户并且设置密码:
使用openssl 生成密码
#cat add_user.yml
---
- hosts: web
remote_user: root
gather_facts: true
tasks:
- name: Add several users
    user: name={{ item }} state=present password="$1$1234567$IElhfIqK0wF7y.p/fYkzb/"
    with_items:
   - testuser1
   - testuser2

页: [1]
查看完整版本: ansible 安装与配置