设为首页 收藏本站
查看: 1358|回复: 0

[经验分享] ansible 安装与配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-19 09:15:46 | 显示全部楼层 |阅读模式
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/epe ... ease-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到客户端
[iyunv@ansible ~]# 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
[iyunv@web1 ~]# 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、服务端配置:
[iyunv@ansible ansible]# cat hosts
[web]
10.0.90.24
10.0.90.25
[hosts1]
172.16.29.193
注:
hosts文件定义:如果没有配置服务端通过ssh无密码登录客户端,hosts文件配置如下:
[webhosts]  
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登陆密码

简单测试:
[iyunv@ansible ansible]# ansible 172* -m shell -a "hostname"
172.16.29.193 | success | rc=0 >>
guang
[iyunv@ansible ansible]# ansible host1 -m shell -a "hostname"
172.16.29.193 | success | rc=0 >>
guang
以上两种方式都可以,其中模块shell 也可以换成command
[iyunv@ansible ~]# 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-140969-1-1.html 上篇帖子: CentOS 6.5 Ansible详细部署 下篇帖子: Ansible之简单使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表