ansible 的安装及常见模块使用
ansible 基础keys的ssh协议配置的
特性:幂等性:一个任务执行1遍和执行n遍效果一样。
ansible是个管理软件不是服务,不需要长期运行
一、通过epel源安装ansible,
1、下载阿里云base源和epel源
1 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
2 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
二、主机清单(将需要管理的机器放到这个文件里)(在最后一行添加即可)
/etc/ansible/hosts
三、以组的方式管理主机
[webserver]
172.16.1.100
172.16.1.101
#webserver是组名,下面是要管理的机器的IP地址
[webserver]
172.16.1.100
172.16.1.101 [appserver]
172.16.1.10[1:2]
#按组分,172.16.1.10[1:2] 表示 172.16.1.101和172.16.1.102
四、比如想了解hostname相关的模块信息
ansible-doc hostname
-s 以简单方式查看的选项
[10:35:56 root@ansible ~]$ansible-doc -s hostname
- name: Manage hostname
hostname:
name: # (required) Name of the host
use: # Which strategy to use to update the hostname. If not set
we try to autodetect, but
this can be problematic,
specially with containers
as they can present
misleading information.
五、ansible单条命令执行
1、列出当前管理的主机清单
ansible all --list-host
hosts (3):
172.16.1.101
172.16.1.102
172.16.1.100
2、列出webserver下的主机
ansible webserver --list-hosts
hosts (2):
172.16.1.100
六、
1、首次远程控制会问你yes or no 去掉配置文件中的host_key_checking = False注释即可
vim /etc/ansible/ansible.cfg host_key_checking = False
2、在配置中开启日志
log_path = /var/log/ansible.log
七、两种连接方式
1、手工输入密码 ansible 172.16.1.101 -m ping -k ,-k输入对方主机的root密码即可
ansible 172.16.1.101 -m ping -k
172.16.1.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2、基于key验证(不需要每次都输入密码)
1)、ssh-keygen
ssh-keygen
Generating 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:
SHA256:d01XYNc5GBGsLKkTACOsJC5kGKb/QD/gigfJ0J5zZMA root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|+=.o. .+B.=|
|=*E... + +o|
|X + o . o . . o|
|=B * . o o o . |
|+.B + S o . . |
|.o = . o . . |
|o . . . |
| . |
| |
+----[SHA256]-----+
2、拷贝刚刚生成的key到需要远程的主机(只需要ansible主机到其他机器,不需要其他机器到ansible主机)(输入密码即可)
[16:42:43 root@ansible ~]$ssh-copy-id 172.16.1.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.100's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '172.16.1.100'"
and check to make sure that only the key(s) you wanted were added.
八、ansible 的使用
ansible "webser:&appser" -m ping -C
#逻辑与webser组和appser组中都有的主机,执行ping模块 ansible 'webser:!appser' -m ping -C
#逻辑非,在webser组合但不在appser组中
九、ansible常用模块使用步骤(模块名-m,模块参数-a)
1、command 默认模块(可以不加command)
ansible-doc -s command
1)、在所有主机上执行ls /data
ansible all -m command -a "ls /data"
2)、creates判断文件是否存在,如果存在就不执行后面的ls /data
ansible all -a "creates=/etc/fstab ls /data"
#command模块不支持 $VARNAME < > | ;&这些
2、shell 模块 和command相似,用shell命令执行
1)、在所有主机上创建用户
ansible all -m shell -a 'useradd alex'
2)对所有主机,重置alex用户密码为China123
ansible all -m shell -a 'echo China123 |passwd --stdin alex'
3)、对webser这个组里的所有主机显示$hostname
ansible webser -m shell -a 'echo $HOSTNAME'
4)、将默认模块由command修改为shell模块(shell模块可以替代command)
module_name = command
修改为
module_name = shell
5)、通过ansible批量修改所有主机的selinux为disabled(当然他有专门的模块做,这里只是演式)
ansible all -a "sed -i 's@SELINUX=enforcing@SELINUX=Disabled@' /etc/selinux/config"
3、script 模块在远程节点上运行本地脚本(最好绝对路径)
ansible all -m script -a /root/test1.sh
4、copy 模块、从当前主机拷贝文件到远程主机
1)、将本机的/etc/fstab 拷贝到/data/下
ansible all -m copy -a 'src=/etc/fstab dest=/data/'
# src 源目录(支持相对路径和绝对路径),dest 目的目录 (仅支持绝对路径)再次执行相同的文件拷贝命令,源文件有变化内容属性等变化才会重新拷贝一遍,文件一模一样的将不拷贝
2)、将本机的/etc/fstab 拷贝到所有主机下的/data/fstab 下将所有者改为alex,所有组改为bin,文件权限改为777
ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin'
3)、避免文件存在呗覆盖,加backup,备份
ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin backup=yes'
4)、content 指定内容,职级生成目标文件
ansible all -m copy -a 'content="line1\nline2\nline3\n" dest=/data/test.txt'
5、fetch 从远程主机提取文件至ansible主机(不支持目录)(会在/data/messages下生成远程主机IP+/var/log/messages的文件)(用来抓取远程主机的配置、日志等)(执行多个文件的抓取也不会覆盖文件IP文件夹)
ansible all -m fetch -a 'src=/var/log/messages dest=/data/messages/'
6、file文件模块(都是直接操作目标主机的文件)
1)、path指定要管理的文件,设置所有者所有组权限。
ansible all -m file -a 'path=/data/fstab owner=alex mode=777'
2)、创建软链接(hard硬链接,link软链接)
ansible all -m file -a 'src=/data/fstab path=/data/fstab.link state=link'
3)、创建空文件state=touch
ansible all -m file -a 'path=/data/f1.txt state=touch'
4)、删除文件 state=absent
ansible all -m file -a 'path=/data/fstab.link state=absent'
5)、清空目录,/data/是挂载点事删不掉的(最好不用,以防删根)
ansible all -m file -a 'path=/data/ state=absent'
7、unarchive:解包解压缩,
1)、在ansible这台主机上的压缩包在本地解压然后拷贝到远程主机上去(默认copy=yes 可以不写)
ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ owner=alex group=bin mode=777'
#将本地的/data/sysconfig.tar.gz 拷贝到所有主机的/data/下,所有者改为alex,所有组该为bin 权限改为777
2)、将远程主机上的压缩包,解压缩到指定目录上去
ansible all -m copy -a 'src=/data/sysconfig.tar.gz dest=/data/ ' ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ copy=no'
ansible 的安装及常见模块使用的更多相关文章
- CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机
使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...
- Ansible常见模块介绍
本节内容: ansible命令基础 常见模块举例 一.ansible命令基础 语法: ansible <host-pattern> [-f forks] [-m module_name] ...
- ansible入门二(Ansible常见模块介绍)
本节内容: ansible命令基础 常见模块举例 一.ansible命令基础 语法: ansible <host-pattern> [-f forks] [-m module_name] ...
- Ansible 常见模块介绍
目录 Ansible 常见模块介绍 ping 模块 command 模块 cron 模块 user 模块 group 模块 copy 模块 file 模块 service 模块 shell 模块 sc ...
- ANSIBLE安装和常用模块模块使用详细教程
目录 ANSIBLE安装和各种模块应用功能 安装配置ANSIBLE ANSIBLE使用 ansible-galaxy工具 ansible-pull工具 ansible-playbook ansible ...
- ansible的安装及命令相关模块
ansible 第一步:下载epel源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...
- Ansible安装及常用模块
配置文件:/etc/ansible/ansible.cfg 主机列表:/etc/ansible/hosts 安装anslibe wget -O /etc/yum.repos.d/epel.repo ...
- ansible环境部署及常用模块总结 - 运维笔记
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- ansible网络模块安装httplib2
ansible网络模块安装httplib2 在进行使用ansible的网络模块的时候,需要安装httplib2模块 下载地址: https://pypi.python.org/pypi?%3Aacti ...
随机推荐
- CentOS6.5修改镜像源问题
千呼万唤使出来阿,随着centos版本不断地更新好多镜像源已经被放弃了治疗,尤其是低版本的centos,下面以CentOS6.5为例进行刨析吧! 上干货: 配置文件 vi /etc/yum.repos ...
- 使用Redis实现购物车功能
增加购物车商品 假设ID为1001的向购物车中存放了3个商品,产品ID分别为10021.10025.10079 hset cart:1001 10021 1 hset cart:1001 10025 ...
- 微信小程序使用 ECharts
echarts-for-weixin 是 ECharts 官方维护的一个开源项目,提供了一个微信小程序组件(Component),我们可以通过这个组件在微信小程序中使用 ECharts 绘制图表. e ...
- java编程用大小写字母及数字输出五位数验证码
package day08; import java.util.Random;//导入util下的Random包 public class Yanzhengma { public static voi ...
- 重学ES系列之模版字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
redis 是使用 C 语言编写的,但是 C 语言是没有字典这个数据结构的,因此 C 语言自己使用结构体来自定义一个字典结构 typedef struct redisDb src\server.h 中 ...
- RPA应用场景-公积金贷款业务数据整合和报送
场景概述 公积金贷款业务数据整合和报送 所涉系统名称 个贷系统.公积金管理系统 人工操作(时间/次) 0.5小时 所涉人工数量1000操作频率 每日 场景流程 1.机器人整理个人贷款信息.个人贷款账户 ...
- 临近梯度下降算法(Proximal Gradient Method)的推导以及优势
邻近梯度下降法 对于无约束凸优化问题,当目标函数可微时,可以采用梯度下降法求解:当目标函数不可微时,可以采用次梯度下降法求解:当目标函数中同时包含可微项与不可微项时,常采用邻近梯度下降法求解.上述三种 ...
- SpringMVC-02
一.SSM整合[重点] 1 SSM整合配置 问题导入 请描述"SSM整合流程"中各个配置类的作用? 1.1 SSM整合流程 创建工程 SSM整合 Spring SpringConf ...
- ArrayDeque(JDK双端队列)源码深度剖析
ArrayDeque(JDK双端队列)源码深度剖析 前言 在本篇文章当中主要跟大家介绍JDK给我们提供的一种用数组实现的双端队列,在之前的文章LinkedList源码剖析当中我们已经介绍了一种双端队列 ...