本文章主要介绍了如何在linux通过yum安装Ansible以及一些常见命令使用,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、Ansible 介绍

1、Ansible 是一个配置管理和自动化运维工具。
基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。(如在多台服务器安装软件或者执行命令)
2、Ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。
真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。
Ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。
3、简单总结:一个能同时远程多台机器且调用命令执行程序的工具,方便管理多台服务器。

二、Ansible 安装

yum安装(推荐)

yum install ansible –y

pip程序安装

yum install python-pip (安装pip)
pip install --upgrade pip (升级pip)
pip install ansible (安装ansible)

三、Ansible 程序结构

配置文件目录:/etc/ansible/    #常用配置文件
执行文件目录:/usr/bin/
Lib库依赖目录:/usr/lib/pythonX.X/site-packages/ansible/
Help文档目录:/usr/share/doc/ansible-X.X.X/
Man文档目录:/usr/share/man/man1/

四、Ansible常用模块命令


 - ping #ping服务器是否能通
   如:ansible all -m ping
   
 - command #在远程主机上执行命令,并将结果返回本主机,hosts为定义的主机清单中的用户组
   如:ansible -i hosts test -m command -a "chdir=/home/wps ls"
   
 - shell  #shell命令
   如:ansible -i hosts test -m shell -a "whoami"
   
 - copy #复制文件到远程机器
   如:ansible hosts -m copy -a "src=/home/ansible/test.txt dest=/home/"
   
 - fetch #拉取远程机器文件
   如:ansible 192.168.1.1 -m fetch  -a "src=/home/test.sh dest=/home flat=yes" #flat=yes 指定路径目录进行保存
   
 - file #创建、修改、删除文件
   如:ansible hosts -m file -a "path=/home/file_dir/test.txt state=touch mode=0755 owner=root group=root"
   
 -  user #管理用户账号
    如:ansible hosts -m user -a "name=keer uid=11111"
    
 - cron #定时任务
    如:ansible hosts -m cron -a "name='crontab test' minute=5 hour=1 job='echo test'"
    
 - script #将本机的脚本在被管理端的机器上运行
   如:ansible hosts -m script -a 'df.sh'

五、Ansible 学习文档

官方地址:

https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

国内地址:

http://www.ansible.com.cn/

如果本文对您有一点点帮助,可以点赞、关注、收藏一下

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐