Centos7安装ansible

一、安装ansible软件

1、安装yum源

rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpmyum install epel-release -yyum install ansible

要是报错:epel源与python版本冲突原因,有些包是需要依赖python2.6的版本,此主机的python版本是2.7.5。

2、那就先卸载 epel-release源

yum  install  epel-release  -y

3、到 /etc/yum.repos.d 目录下,将epel.repo源备份,

mv  epel.repo  epel.repo.bak

4、清理yum源缓存和新建缓存,

 yum clean allyum makecache

5、再执行安装命令

yum  install  ansible  -y

6、查看安装的版本

ansible --version

7、配置主机组

Ansible工具默认主目录为/etc/ansible/,其中hosts文件为被管理机IP或者主机名列

二、配置免秘钥登录

1、管理主机上生成秘钥

ssh-keygen -t rsaGenerating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
73:80:07:fa:9a:0d:e0:0e:d1:c2:44:d2:d2:61:67:21 root@ansible
The key's randomart image is:
+--[ RSA 2048]----+
|o=E.+..          |
|=oo+ . o         |
|ooo . . o        |
| + . . . .       |
|. . . . S .      |
| o   =   o       |
|  . o .          |
|                 |
|                 |
+-----------------+

2、将管理机上生成的秘钥发送到被管理机

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.207.137
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.207.132

3、测试是否配置成功

ansible -k all -m ping

三、Ansible模块详细介绍

1、Ansible command模块为Ansible默认模块,主要用于执行Linux基础命令,可以执行远程服务器命令执行、任务执行等操作。

ansible -k -i /etc/ansible/hosts web -m command -a "date"
ansible -k all -m command -a "ping -c 1 www.baidu.com"
ansible -k 192.168.207.137 -m command -a "df -h"                            指定单个IP执行任务

2、Ansible copy模块主要用于文件或者目录复制,支持文件、目录、权限、用户组功能。

ansible -k all -m copy -a 'src=/opt/test.txt dest=/tmp/ mode=755 owner=root'
ansible -k all -m copy -a 'content="Hello World" dest=/tmp/jfedu.txt mode=755 owner=root'
ansible -k all -m copy -a 'content="Hello World" dest=/tmp/xiaoxin.txt backup=yes mode=755 owner=root'

3、Ansible YUM模块主要用于软件的安装、升级、卸载,支持红帽rpm软件包的管理。

ansible all -k -m yum -a "name=xinetd,screen state=installed"
ansible all -k -m yum -a "name=sysstat,screen state=installed"                installed表示安装服务
ansible all -k -m yum -a "name=sysstat,screen state=absent"                   absent表示卸载服务
ansible 192.168.207.137 -k -m yum -a "name=sysstat,screen state installed disable_gpg_check=no"   表示不检查key

如有报以下错:Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again

处理方法

编辑epel.repo, 去除epel段中baseurl行的注释符, 并注释metalink行

vim /etc/yum.repos.d/epel.repo[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

4、Ansible file模块主要用于对文件的创建、删除、修改、权限、属性的维护和管理。

ansible -k 192.168.207.* -m file -a "path=/tmp/test state=directory mode=755"               创建目录
ansible -k 192.168.207.* -m file -a "path=/tmp/abc.txt state=touch mode=755"                创建文件

5、Ansible user模块主要用于操作系统用户、组、权限、密码等操作

ansible -k 192.168.207.* -m user -a "name=jfedu home=/tmp/"                           home指定家目录
ansible -k 192.168.207.* -m user -a "name=jfedu home=/tmp/ shell=/sbin/nologin"    指定家目录,并指定shell
ansible -k 192.168.207.* -m user -a "name=jfedu state=absent force=yes"               删除用户

6、Ansible cron模块主要用于添加、删除、更新操作系统crontab任务计划

ansible -k all -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' job='/usr/sbin/ntpdate 139.224.227.121'"    定时同步时间
ansible -k all -m cron -a "minute=* hour=* day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='/usr/sbin/ntpdate pool.ntp.org'"   开启备份,备份目录在/tmp下
ansible -k all -m cron -a "name='Ntpdate server for sync time' state=absent"              删除备份计划

7、Ansible synchronize模块主要用于目录、文件同步,主要基于rsync命令工具同步目录和文件。

ansible -k all -m synchronize -a 'src=/tmp/ dest=/tmp/'                  同步/tmp目录下的内容
ansible -k all -m synchronize -a 'src=/tmp/ dest=/tmp/ compress=yes delete=yes rsync_opts=--no-motd,--exclude=.txt'

8、Ansible shell模块主要用于远程客户端上执行各种shell命令或者运行脚本,远程执行命令通过/bin/sh 环境来执行,支持比command更多的指令。

ansible -k all -m shell -a "/bin/sh /tmp/date.sh >>/tmp/var.log"               执行date.sh文件,并把执行结果追加到var.log文件里面去
ansible -k all -m shell -a "mkdir -p abc chdir=/tmp/ state=directory warn=no"  创建目录
ansible -k all -m shell -a "ps -ef |grep http"                                 远程查看http进程是否启动
ansible -k all -m shell -a "crontab -l"                                        查看定时任务

9、Ansible service模块主要用于远程客户端各种服务管理,包括启动、停止、重启、重新加载等。

 ansible -k all -m service -a "name=mysql state=restarted"               重启mysql服务ansible -k all -m service -a "name=network args=eth0  state=restarted"  重启网卡服务ansible -k all -m service -a "name=nfs enabled=yes runlevel=3.5"        远程开启nfs服务,设置3,5级别自动启动

10、Ansible PlayBook剧情模块

主要参数详解

ansible-playbook nginx_install.yaml
nginx_install.yaml文件
---
- hosts: alltasks:- name: Installs nginx web serveryum: name=nginx state=installed update_cache=truenotify:- start nginxhandlers:- name: start nginxservice: name=nginx state=started

执行结果:

centos7安装ansible相关推荐

  1. Ansible(1) Centos7安装Ansible

    角色 IP 系统 ansible server 120.53.13.240 Centos7.6 client 123.207.166.69 Centos7.6 确保Python的版本在2.6以上,如果 ...

  2. centos7安装ansible AWX17.1.0

    wget -c https://github.com/ansible/awx/archive/17.1.0.tar.gz 需要docker docker-compose环境 Linux:~ # pip ...

  3. centos7离线安装ansible

    centos7离线安装ansible: 1.通过在线的centos7将rpm包下载好了,上传到指定服务器. 下载官方repo,rpm -iUvh http://dl.Fedoraproject.org ...

  4. 阿里云ECS服务器 Centos7.2 使用 yum 安装 ansible 报错

    #####################使用阿里云的ECS服务器Centos7.2系统安装ansible提示安装不上########### 原因 通过Yum安装最新发布版本 通过Yum安装RPMs适 ...

  5. CentOS7.9奶妈级教程安装Ansible AWX 17.1.0

    AWX提供了基于web的用户界面.REST API和建立在Ansible之上的任务引擎.本教程将指导您在CentOS/RHEL 7的Docker容器中设置Ansible AWX的步骤. 请注意,这些说 ...

  6. centos 7使用tar包安装ansible

    由于我的yum源没有ansible软件包,防止以后内网服务器也没有ansible的yum源,干脆一点,直接tar包安装!!!! 下载软件包 - # 1.python安装 # python2.7安装 [ ...

  7. openshift介绍及centos7安装单节点openshift、Redhat安装openshift集群完全教程

    Centos7中openshift_3.11单节点安装及配置开机自启详解 本次openshift安装使用最简单便捷的单节点安装,适用于本地开发及测试 openshift简介 OpenShift是红帽公 ...

  8. 教你十分钟在Linux系统上快速装机并安装Ansible

    PS:本教程建立在VMware软件上的使用上,Linux版本为centos7或者centos8都可以. 一.看发行版本 cat  /etc/redhat-release 二.修改主机名 hostnam ...

  9. centos7 nginx配置php7,centos7安装并配置nginx+php,centos7nginx

    centos7安装并配置nginx+php,centos7nginx centos7安装并配置nginx+php 安装nginx yum install nginx 设置nginx开启起动 syste ...

最新文章

  1. sklearn FutureWarning: numpy not_equal will not check..., The comparison did not return the same
  2. 在直播问题上,智能电视们不应该沉默
  3. DOS下获得当前文件夹目录的命令
  4. 《MFC游戏开发》笔记七 游戏特效的实现(一):背景滚动
  5. c语言答案选择题,C语言选择题(附答案)
  6. tomcat access log pattern
  7. 如何将显示器连接到 Mac?
  8. 软件工程----项目的进度安排
  9. 【Python量化】风险平价策略
  10. [文档和源码分享]C++实现的基于α-β剪枝算法的井字棋游戏
  11. basic计算机编程基础,QBASIC编程语言基础
  12. 骑士cms文件包含getshell复现
  13. Remove Element
  14. ACCP6.0 教程课件,可用
  15. mac虚拟机桌面图标隐藏_「MAC软件推荐」MAC实用软件
  16. 微信小程序中使用 iconfont 图标的四种方法
  17. 电子招标系统源码之了解电子招标投标全流程
  18. 查高考成绩服务器维护,各省高考成绩查询时间公布,但这个时段不建议查看,过来人吃亏过...
  19. 杂题记录及简要题解(一)
  20. 关于引起stop:c000021a unknown hard error部分问题及解决。

热门文章

  1. rust(34)-Rust and WebAssembly(2)
  2. C指针原理(28)-垃圾回收-内存泄露
  3. 【GNN】2022年最新3篇GNN领域综述!
  4. 【深度学习】遗传算法优化GAN
  5. 与优秀的人在一起进步:我发起的“乐学”分享活动
  6. 【论文学习】mixup系列(mixup、cutMix、manifold mixup、patchUp、saliencyMix、puzzleMix、co-Mixup、FMix)
  7. 大盘点|卷积神经网络必读的 100 篇经典论文,包含检测 / 识别 / 分类 / 分割多个领域
  8. Python——爬虫
  9. KDD Cup 2021:时间序列异常检测问题开源方案
  10. 推荐系统炼丹笔记:令人着迷的时间动态CF算法