自动化运维---ansible

        ansible是一种由Python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

特点:

部署简单

默认使用ssh进行管理,基于python里的paramiko模块开发

管理端和被管理端不需要启动服务

配置简单,功能强大,扩展性强

能过playbook(剧本)进行多个任务的编排

ansible环境搭建 

 需要三台主机,一台管理主机,两台被管理主机

主机环境:

        1.静态ip

        2.关闭防火墙

        3.关闭selinux

        4.要epel源

实验过程

第1步: 管理机上安装ansible,被管理节点必须打开ssh服务。

yum -y install ansible
ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

第2步: 实现master对agent的免密登录,只在master上做。(如果这 ⼀步不做,则在后⾯操作agent时都要加-k参数传密码;或者在主机清 单⾥传密码)

ssh-keygen 
ssh-copy-id -i 192.168.1.63
ssh-copy-id -i 192.168.1.64

第3步: 在master上定义主机组,并测试连接性

vim /etc/ansible/hosts 
[group01]
192.168.1.63
192.168.1.64

[group02]
192.168.1.63
192.168.1.64
192.168.1.65

ping模块

ansible 192.168.1.63 -m ping 
192.168.1.63 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

 小结:

主机清单的作用: 服务器分组。

主机清单的常见功能:

        1. 可以通过IP范围来分, 主机名名字的范围来分

        2. 如果ssh端口不是22的,可以传入新的端⼝。

        3. 没有做免密登录,可以传密码。

 ansible模块

查看所有⽀持的模块

ansible-doc -l 

官⽹模块⽂档地址: https://docs.ansible.com/ansible/latest/modules /list_of_all_modules.html

 hostname模块

 hostname模块⽤于修改主机名(注意: 它不能修改/etc/hosts⽂件)

基本格式为: ansible 操作的机器名或组名 -m 模块名 -a "参数 1=值1 参数2=值2"

#将其中⼀远程机器主机名修改为agent1.cluster.com
ansible 192.168.1.63 -m hostname -a 'name=agent1.cluster.com'

创建⼀个⽬录

ansible group1 -m file -a 'path=/test state=directory'

创建⼀个⽂件

ansible group1 -m file -a 'path=/test/111 state=touch'

 递归修改owner,group,mode

ansible group1 -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'

 删除⽬录(连同⽬录⾥的所有⽂件)

ansible group1 -m file -a 'path=/test state=absent'

 创建⽂件并指定owner,group,mode等

ansible group1 -m file -a 'path=/tmp/111 state=touch owner=bin group=daemon mode=1777'

 删除⽂件

ansible group1 -m file -a 'path=/tmp/111 state=absent'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值