Ansible 自动化运维

一、 Ansible:开源的运维自动化工具,可以提高运维工程师的效率,减少人为失误。操作简单,功能丰富。

     特点:1)基于python开发,易于二次开发 2)丰富的内置模块,基本可以满足一切要求  3)管理模式非常简单 4)无客户端模式,底层通过ssh通信,也不需要代理程序  5)可以应用在大公司环境下。

二、Ansile的角色:

    1、使用者:如何使用Ansible(与Ansible的交互方式)

       CMDB:直接下发指令调用Ansibe工具集完成任务目标

       PUBLIC/PRIVATE:以API调用的方式

       Ad-hoc命令集:通过Ad-hoc命令集调用Ansible工具集完成任务

       Playbooks:根据预先编写好的Ansible Playbooks按序执行任务

    2、Ansible工具集:包含invertory,modules,plugins和API。

       Ansible plakbooks:任务脚本,编排定义Ansible任务集的配置文件,一般为json格式的yml文件

       inventory:管理Ansible设备列表,通过分组管理主机。

       modules: 执行命令的各种模块,任务通过模块执行

       plugins: 附加功能,是模块功能的补充

       API:供第三方调用的应用程序编程接口,便于二次开发

       Ansible:组合上述工具的命令工具,为核心的执行工具

    3、作用对象:linux、windows、各类PUBLIC/PRIVATE网络设施

三、Ansible的配置:inventory是Ansible管理主机信息的配置文件,相当于系统hosts文件的功能,默认存放在/etc/ansible/hosts.可以用-i选项指定在其他文件。在hosts文件中通过分组来组织设备。设备列表支持ip地址和主机名,通过ssh(22端口)管理设备。

四、Ansible命令:

    ansible:大多数维护命令以ansible开头。执行结果有:红色表示执行过程异常、橘黄色代表目标有状态变化、绿色代表执行成功且没有对目标修改

      格式:ansible  主机组  命令选项

          主机组:必须是hosts文件定义的设备组,或者单个ip,all代表所有

          主要选项:-m 指定执行使用的模块   -a 指定模块参数  -l(--limit) 限制运行主机    --list 列出符合条件的主机

     ansible-playbook命令:执行任务的脚本。根据预先编好的yml格式的playbook文件实现集中处理任务。文件中存放执行的任务代码。

        格式:ansible-playbook    .yml文件

五、Ansible的主要模块:

    1、command模块:在远程主机执行命令,不支持管道、重定向特性

    2、shell模块:与command命令相同,可以支持管道和重定向

    3、copy模块:复制指定主机文件到远程主机的指定位置

    4、hostname模块:管理远程主机的主机名

    5、yum模块:基于yum对远程主机管理程序包

    6、service模块:管理远程主机的服务

    7、user模块:管理远程主机的用户账号

六、Playbook配置文件:放置所有任务代码,使用yaml语法,扩展名为.yml或.yaml.可以实现自动化运维,类似任务脚本

    语法通过缩进来展示,通过-来代表项冒号:来分隔键和值,整个文件以- - -开头以...结束。

    playbook文件中可以配置触发器:需要触发才能执行的任务。即之前定义的tasks任务执行成功后触发 handlers(触发器)里的触发任务。

    playbook的tasks文件集中存放在某个目录下,则该目录就是角色。角色一般存放在/etc/ansible/roles中,目录下可以由自定义的各个子目录比如files存放模块调用的文件,tasks存放任务,handlers存放触发器等等

ansible批量管理服务意义
    01. 提高工作的效率
    02. 提高工作准确度
    03. 减少维护的成本
    04. 减少重复性工作
    ansible批量管理服务功能
    01. 可以实现批量系统操作配置
    02. 可以实现批量软件服务部署
    03. 可以实现批量文件数据分发
    04. 可以实现批量系统信息收集

复制代码
①. 管理服务器创建私钥和公钥(密钥对)

②. 将公钥文件远程传送复制到被管理服务器相应用户~/.ssh/id_dsa.pub下,并修改.ssh目录权限为700

③. 修改公钥文件文件名称为authorized_keys,授权权限为600

④. 利用ssh服务配置文件的配置参数,进行识别公钥文件authorized_keys

⑤. 进而实现基于密钥远程登录服务器(免密码登录/非交互方式登录)

 

 

Yum仓库配置:

[name]

name=ggg

baseurl=file:///media/cdrom

enabled=1

gpgcheck=0

[ansible]

name=ldsakl

baseurl=file:///root/ansiblerepo

enabled=1

gpgcheck=0

yum -y install ansible

ansible –version

[root@xiao---tong ~]# createrepo /root/ansiblerepo/

Spawning worker 0 with 108 pkgs

Workers Finished

Saving Primary metadata

Saving file lists metadata

Saving other metadata

Generating sqlite DBs

Sqlite DBs complete

[root@xiao---tong ~]# cd /etc/ansible/

[root@xiao---tong ansible]# ls

ansible.cfg  hosts  roles

[root@xiao---tong ansible]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Created directory '/root/.ssh'.

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:

SHA256:q+vs+wqRtL6WD6C88bcKdfpn4SkeuinNFBt5KWRvMD4 root@xiao---tong

The key's randomart image is:

+---[RSA 2048]----+

|                 |

|   =             |

|  + * .          |

|   E B           |

|  ..%.  S        |

|...=o. . .       |

|.++.+o. +        |

| .=o*B.*         |

| ..B*B#=.        |

+----[SHA256]-----+

[root@xiao---tong ansible]# ls /root/.ssh/

id_rsa  id_rsa.pub

[root@xiao---tong ansible]# ssh-copy-id root@192.168.10.20

[root@xiao---tong ansible]# ssh-copy-id root@192.168.10.30

[root@xiao---tong ansible]# ssh 192.168.10.20

[root@xiao---tong ansible]# ssh 192.168.10.30
vim /etc/ansible/hosts

[gzs]

192.168.10.20

[gao]

192.168.10.30

[root@xiao---tong ansible]# ansible all –m ping

[root@xiao---tong ansible]# ansible gzs –m ping

[root@xiao---tong ansible]# ansible all -f 5 -m ping

[root@xiao---tong ansible]# ansible all –list

[root@xiao---tong ansible]# ansible gzs -m command -a "df -hT"

192.168.10.20 | SUCCESS | rc=0 >>

文件系统                类型      容量  已用  可用 已用% 挂载点

/dev/mapper/centos-root xfs        50G  3.6G   47G    8% /

devtmpfs                devtmpfs  897M     0  897M    0% /dev

tmpfs                   tmpfs     912M     0  912M    0% /dev/shm

tmpfs                   tmpfs     912M   18M  895M    2% /run

tmpfs                   tmpfs     912M     0  912M    0% /sys/fs/cgroup

/dev/sda1               xfs      1014M  179M  836M   18% /boot

/dev/mapper/centos-home xfs        47G   33M   47G    1% /home

tmpfs                   tmpfs     183M  4.0K  183M    1% /run/user/42

tmpfs                   tmpfs     183M   44K  183M    1% /run/user/0

/dev/sr0                iso9660   4.3G  4.3G     0  100% /media/cdrom

IP地址

[root@xiao---tong ansible]# ansible 192.168.10.20 -m command -a "ifconfig ens33"

yum仓库:

[root@xiao---tong ansible]# ansible all -m command -a "cat /etc/yum.repos.d/benet.repo"

更改主机名:

[root@xiao---tong ansible]# ansible 192.168.10.30 -m command -a "hostname AAA"

[root@xiao---tong ansible]# ansible 192.168.10.30 -m command -a "hostname " 查看

root@xiao---tong ansible]# ansible-doc –l

cat /etc/hostname

shell模块:

ansible web -m shell -a 'echo "heelo" >> /tmp/hello.txt'

     ssh 192.168.10.30 cat /tmp/hello.txt

Copy模块:

      ansible web -m copy -a "src=/etc/hosts dest=/tmp mode=777 owner=nobody group=root"

     ssh 192.168.10.30 ls -l /tmp/hosts

     Hostname 模块:

ansible 192.168.10.20 -m hostname -a "name=demo

yum 模块:"

ansible web -m yum -a "name=httpd state=present"

ssh 192.168.10.30 rpm -qa | grep httpd

Service 模块:

ansible web -m service -a "name=httpd state=started enabled=yes"

ansible ssh -m service -a "name=httpd state=started enabled=yes"

ansible gzs -m user -a 'name=user1 system=yes uid=501 group=root groups=sshd shell=/sbin/nologin home=/home/user1 password=user1 comment="test user"'

ssh 192.168.10.20 tail -l /etc/passwd

  ansible web -m user -a 'name=user1 system=yes uid=501 group=root groups=sshd shell=/sbin/nologin home=/home/user1 password=user1 comment="test user"'

[root@demo ~]# vim /etc/ansible/hosts

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftp

grep -v "^#" /etc/vsftpd/vsftpd.ccc > /etc/vsftpd/vsftpd.conf

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值