3.1 配置

#vim /etc/ansible/hosts       //定义主机,支持IP和域名,支持分组

[local]

127.0.0.1

[nginx]

192.168.0.10                 //客户端IP地址

#vim /etc/ansible/ansible.cfg    //简单配置,需要根据自己的实际环境修改

[defaults]

# some basic default values...

hostfile      = /etc/ansible/hosts

library       = /usr/share/ansible

remote_tmp     = $HOME/.ansible/tmp

pattern       = *

forks         = 5

sudo_user      = root

#ask_sudo_pass = True

#ask_pass     = True

transport     = smart

remote_port    = 2211

### 配置说明:

hostfile=/etc/ansible/hosts #指定默认hosts配置的位置

host_key_checking = False   #不进行host_key检查,省去目标key发生变化时输入(yes/no)的步骤

ask_pass=True               # 每次执行ansible命令是否询问ssh密码

ask_sudo_pass=True          # 每次执行ansible命令时是否询问sudo密码

log_path= /var/log/ansible.log    # ansible用户时需要:chown -R ansible:ansible ansible.log

#visudo

设置sudo    这样就不用咨询sudo密码了

3.2测试ansible的使用

$ansible all -m ping     //使用ping模块测试,以下显示成功,可以继续其他操作

127.0.0.1 | success >> {

}

192.168.0.10 | success >> {

}

3.3模块测试

1、ansible命令

语法ansible <host-pattern> [options]

ansible <pattern_goes_here> -m<module_name> -a <arguments>

选项

-i 设备列表路径,可制定一些动态路径

-f 并行任务数

–private-key 私钥路径

-m 模块名

-M 模块夹在路径

-a 参数

-k 登陆密码

-K sudo密码

-t 输出结果保存路径

-B 后台运行超时时间

-P 调查后台程序时间

-u 执行用户

-U sudo用户

-l 限制设备范围

-s是此用户sudo无需输入密码

2、体验命令操作

(1)用户类操作

$ ansible webtest -m group -a"name=jjgame state=present" -s

$ ansible webtest -m user -a"name=utest groups=jjgame state=present" -s

$ ansible webtest -m user -a"name=utest state=absent remove=yes" -s

(2)服务安装管理

$ ansible local -a 'sudo apt-get -y installnginx'

$ ansible local -a "sudo/etc/init.d/nginx stop"

$ ansible local -a "sudo/etc/init.d/nginx start"

$ ansible webtest -m service -a "name=nginx state=running" -s

(3)其他测试过的

$ ansible datacenter -a 'ls -l /root' -s

$ ansible datacenter -a 'mv/root/old.tar.gz /root/new.tar.gz' -s

$ ansible datacenter -m file -a'path=/root/new.tar.gz' -s    //查看文件属性

$ ansible datacenter -a 'rm -rf/root/directory' -s    //删除目录

$ ansible all -m command -a "/bin/echohello" --ask-pass

$ ansible all -m ping --ask-pass -u root

$ ansible all -m script -a"/root/hequan/shell/t.sh" -k

$ ansible all -m copy -a"src=/root/hequan/shell/t.sh dest=/tmp/ mode=755 owner=rootgroup=root" -k -u root

$ ansible all -m file -a"dest=/tmp/t.sh mode=755 owner=root group=root" -k -u root

$ ansible all -i /etc/ansible/serverlist -mshell -a "/tmp/t.sh" -k -u root

$ ansible webtest -m get_url -a"url=http://192.168.0.8/sa/ sh.tar.gz dest=/tmp/" -s

$ ansible webtest -a "sudo tar zxvf/tmp/ sh.tar.gz -C /tmp" -s  //不指定解压目录,会解压到/home/ansible目录下

$ ansible webtest -a "sudo/tmp/lansa_sh/t.sh" –s

ansible的更多相关文章

  1. 如何利用ansible callback插件对执行结果进行解析

    最近在写一个批量巡检工具,利用ansible将脚本推到各个机器上执行,然后将执行的结果以json格式返回来. 如下所示: # ansible node2 -m script -a /root/pyth ...

  2. 《Ansible权威指南》笔记(2)——Inventory配置

    四.Inventory配置ansible通过Inventory来定义主机和组,使用时通过-i指定读取,默认/etc/ansible/hosts.可以存在多个Inventory,支持动态生成.1.定义主 ...

  3. useful Ansible commands

    This article includes some useful Ansible commands. I will try to write blogs by English. You may wa ...

  4. 《Ansible权威指南》笔记(4)——Playbook

    七.Playbook1.语法特性如下:(1)"---"首行顶格开始(2)#号注释(3)缩进统一,不同的缩进代表不同的级别,缩进要对齐,空格和tab不能混用(4)区别大小写,键值对k ...

  5. 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块

    五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options]    -v,--verbose  ...

  6. 《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令

    2016-12-23 读这本<Ansible权威指南>学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记.另,这本书内容很好,但印刷错误比较多,作者说第二版会 ...

  7. 自动化运维工具ansible部署以及使用

    测试环境master 192.168.16.74webserver1 192.168.16.70webserver2 192.168.16.72安装ansiblerpm -Uvh http://ftp ...

  8. Ansible Ubuntu 安装部署

    一.安装: $ sudo apt-get install ansible 二.配置: a.基本配置 $ cd /etc/ansible/ $ sudo cp hosts hosts_back 备份一个 ...

  9. Ansible 模块命令介绍

    copy模块: 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/' ...

  10. 用Vagrant和Ansible搭建持续交付平台

    这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...

随机推荐

  1. HTML: 仿写一个财经类静态的网页

    要求:仿写一个静态的网页,主要采用HTML+CSS+DIV的布局方式, 新建两个文件:demo.html.demo.css 图片素材:image.zip demo.html代码如下: <!doc ...

  2. MEF 松耦合

    MEF天生就有解耦合的特性,虽然它不是为解耦而生,而主要是为插件类应用的开发而设计.如果主要是为了解除耦合的话可以使用IoC,Unity等. Unity 微软的IOC 代码: using System ...

  3. shell各种执行方式区别

    shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别 原文出处:http://blog.csdn.net/dance_rise/article/deta ...

  4. RouteOS软路由HotSpot热点认证网关

    实现要求: 实现局域网有线无线需在网页输入用户名和密码登录,不同用户登录有不同的访问内外网权限. 环境要求: 一台PC机安装三张网卡,第一张网卡连接外网,第二张网卡配置局域网,第三张网卡做配置连接使用 ...

  5. iOS 隐藏系统的导航,使用自定义的导航

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  6. iOS UITableView点击按钮滚到顶部

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  7. CentOS安装Git

    准备安装Gityum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel #下载git-1 ...

  8. DG - 开启Active Data Guard

    在配置active data guard之前,phycial standby必须处于以下两个状态之一: (1)standby处于mount状态,并且redo apply正在运行中 (2)standby ...

  9. iOS架构网址

    http://casatwy.com/iosying-yong-jia-gou-tan-kai-pian.html

  10. Postgres数据库基本介绍

    最近一直在做一个和PostgreSQL数据库相关的项目,把自己在这个过程中学习的知识记录下来.关于PostgreSQL数据库网上已经有太多的相关介绍了,为了博文的系统性还是先看一下维基百科对Postg ...