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. php常用转义字符‘ “ {} $ \n

    php 单引号和双引号的区别: 双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符. <?php $a = 'abc'; echo "<br>&quo ...

  2. Linux是怎么启动的

    按下电源按钮的直到欢迎页出来之后,linux总共做的事可以分为五步来完成. 1. BIOS加电自检: 加电自检,检测硬件设备.然后按照cmos上面的顺序来搜索处在活动状态下的可以引导的设备.可以是光驱 ...

  3. Android --ListView分页

    参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...

  4. IDisplayTransformation

    IDisplayTransformation Bounds Full extent in world coordinates. The Bounds property controls the ful ...

  5. MyEclipse6.5注册码(转)

    新装的MyEclipse6.5在网上找了半天的注册码,都不可用.将下面的代码放在MyEclipse下运行后,可以得到注册码.这相当于一个注册机.我得到的如下: administrator nLR8ZC ...

  6. JQuery Delay Hover效果

    CSS代码 .tbui_aside_float_bar { position: fixed; left: 50%; bottom: 120px; margin-left: 608px; border- ...

  7. Unity3D 基于预设(Prefab)的泛型对象池实现

    背景 在研究Inventory Pro插件的时候,发现老外实现的一个泛型对象池,觉得设计的小巧实用,不敢私藏,特此共享出来. 以前也看过很多博友关于对象池的总结分享,但是世界这么大,这么复杂到底什么样 ...

  8. SLF4简介

    The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logg ...

  9. 建立一个Hello World级别的Spring项目

    package com.sevenhu.domain; import org.springframework.context.ApplicationContext; import org.spring ...

  10. 反射认识_04_反射调用类方法Method

    包1: package ReflectionMethod; public class ReflectionMethod { String str1="str1"; public v ...