ansible快速入门

ansible

ansible是一款用python开发的自动化运维工具,只需要使用ssh协议连接,就可以进行系统管理,自动化执行命令,部署等任务。

ansible不需要被控机安装客户端,只需要一台ansible主操作机即可完成所有操作。

ansilbe安装

	yum install epel-release -y 
	yum install ansible -y 
	// 安装目录
	// 配置文件目录: /etc/ansible
	// 执行文件目录: /usr/bin	

ansible使用

ansible使用分为两种:
1.临时的快速执行使用ansible执行
2.长期的复杂的命令是用ansible-playbook执行

	//查看ansible帮助
	ansible -h
	//ansible-doc 查看模块列表
	ansible-doc -l 
	//过滤结果
	ansible-doc -l |grep filtername
	
	//模块说明
	//测试网络连通
	ping  Try to connect to host, verify a usable python and return `pong' on success 
	//执行命令
	command  Execute commands on targets  
	//执行命令shell脚本
	shell    Execute shell commands on targets 
	//拷贝文件
	copy   	 Copy files to remote locations 
	//文件操作
	file    Manage files and file properties
	
	//查看模块使用帮助
	ansible-doc modulename	
	

模块使用

配置主机

	//配置操作机
	cd /etc/ansible
	vim hosts
		
	[operator1]
	// ip地址  ansible_ssh_port=22 ansible_ssh_user_root ansible_ssh_pass="123456"
	192.168.1.1 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
		
//联通性测试
//ansible 配置的主机名 -m ping 
ansible operator1 -m ping
	//查看操作机的目录
	// ansible 配置的操作机的名称 -m shell  -a 'chdir=进入的目录 ls'
	ansible operator1 -m shell -a 'chdir=/etc ls'
	//-m 模块名 -a  模块操作参数 chdir:进入配置的目录

playbook简单使用

新建yaml文件

vim playbook0.yaml

---
- hosts: operator1 #执行任务的机器
  remote_user: root #执行任务的机器的用户
  vars:
    http_port: 8088 #变量 可以在模板中使用

  tasks: #任务
    - name: create new file # 任务名称
      file: name=/testdir/123.txt state=touch #任务调用的模块,执行那些操作
		
//先执行下检查,如果整个过程Ok
ansible-playbook playbook0.yaml -C
//语法没有问题,再执行
ansible-playbook playbook0.yaml

发布了121 篇原创文章 · 获赞 56 · 访问量 167万+

猜你喜欢

转载自blog.csdn.net/u013565163/article/details/104536317