ansible的功能:

  模块化任务,调用特定的模块,完成特定的任务

  基于python语言实现,由paramiko、pyyaml和jinja2三个模块构建

  部署简单,agentless,ansible基于ssh协议实现的
  主从模式
  支持自定义模块
  支持playbook
  允许重复执行
ansible的安装,ansible提供的rpm包在epel源上
  note:epel源安装
  cd /etc/yum.repos.d
  rpm -ivh epel-release-latest-6.noarch.rpm
  yum install ansible -y
 
ansible主端:192.168.223.136
节点1:192.168.223.146
1、ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
2、ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.223.143
3、远程连接ssh root@192.168.223.146或者ssh 192.168.223.146
4、在节点上执行命令
[root@node1 ~]# ssh 192.168.223.146 'date'
Sun Jul 30 20:02:58 CST 2017
[root@node1 ~]# date
Sun Jul 30 12:03:00 CST 2017
 
ansible基础用法:
ansible <host-pattern> [-m module_name] [-a args] [options]
ansible-doc -l 显示ansible支持的模块
[root@node1 ~]# ansible-doc -s command 显示模块的具体用法
- name: Executes a command on a remote node
 
解决:定义host清单
1、cp hosts hosts.bak
2、修改hoats文件
[nodes]
192.168.223.146
 
command模块:
  ansible nodes -m command -a 'ifconfig'
 
ping模块:
  [root@node1 ansible]# ansible all -m ping
  192.168.223.146 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  }
 
user模块:
  #ansible-doc -s user
  新建某个用户:
  [root@node1 ansible]# ansible nodes -m user -a "name=wadeson state=present"
  192.168.223.146 | SUCCESS => {
  "changed": true,
  "comment": "",
  "createhome": true,
  "group": 500,
  "home": "/home/wadeson",
  "name": "wadeson",
  "shell": "/bin/bash",
  "state": "present",
  "system": false,
  "uid": 500
  }
-a后面接模块的参数:
name=:表示需要创建的新用户的名字
state:表示用户存在的状态(由于是新建用户,所以状态为present)
删除某个用户: absent不在的
[root@node1 ansible]# ansible nodes -m user -a "name=wadeson state=absent"
192.168.223.146 | SUCCESS => {
"changed": true,
"force": false,
"name": "wadeson",
"remove": false,
"state": "absent"
}
如果state不填写,默认是present,创建一个新的
 
cron模块:
ansible-doc -s cron
[root@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'
192.168.223.146 | FAILED! => {
"changed": false,
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
解决办法:在节点主机上安装libselinux-python
yum -y install libselinux-python
 
再次在ansible服务器上执行:
[root@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=*/2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'
192.168.223.146 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
在节点上查看:
[root@wadeson ~]# crontab -l
#Ansible: sync time
2 * * * * /usr/sbin/ntpdate time.nist.gov &> /dev/null
删除cron任务:
ansible all -m cron -a 'name="sync time" state=absent‘
 
copy模块:
ansible-doc -s copy
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fatab.bak mode=600"
节点上查看:
[root@wadeson ~]# ll /tmp/
总用量 8
-rw-------. 1 root root 899 7月 30 21:55 fatab.bak

[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
remote_user: root
tasks:
- name: create a new file
copy: content={{ node_var }} dest=/tmp/ansible_var.txt

file模块:
ansible-doc -s file,创建文件,目录,链接文件
ansible all -m file -a 'path=/tmp/testdir state=directory'
ansible all -m file -a 'src=/tmp/fstab.bak dest=/root/fstab.bak state=link'
[root@wadeson ~]# ll
总用量 16
-rw-------. 1 root root 1104 7月 30 19:54 anaconda-ks.cfg
lrwxrwxrwx. 1 root root 14 7月 30 22:03 fstab.bak -> /tmp/fstab.bak
state:file|absent|directory|link|hard|touch
在远程节点上创建一个新文件:
ansible nodes -m file -a 'path=/tmp/ansible_agent.txt state=touch'
path这里可以为dest或者name
 
yum模块:
ansible-doc -s yum
ansible all -m yum -a 'name="vim,wget" state=latest'
state:present|latest|absent
ansible all -m yum -a 'name=ntpdate state=latest'
按照ntpdate包,然后定义cron任务
 
service模块:
参数:
enabled:设置是否开机启动
state:started|stopped|restarted
ansible all -m yum -a 'name=httpd state=latest'
ansible all -m service -a 'name=httpd state=started enabled=no'
节点查看:
[root@wadeson ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.4.x86_64
[root@wadeson ~]# chkconfig --list|grep httpd
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
 
shell模块:
-a “command”,没有额外的参数
命令中带有管道,变量等等
[root@node1 ~]# ansible nodes -m shell -a 'echo "redhat"|passwd --stdin testuser1'
192.168.223.146 | SUCCESS | rc=0 >>
更改用户 testuser1 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
而command模块:
[root@node1 ~]# ansible nodes -m command -a 'echo "redhat"|passwd --stdin testuser1'
192.168.223.146 | SUCCESS | rc=0 >>
redhat|passwd --stdin testuser1
可以看出并没有修改用户的密码,command模块只能执行简单的命令
 
script模块:
1、将脚本传到node上
2、并在node上执行该脚本
ansible服务器:
[root@node1 ~]# cat echo.sh
#!/bin/bash
echo "ansible is better" > /tmp/echo.text
ansible all -m script -a "/root/echo.sh"
节点查看:
[root@wadeson ~]# ll /tmp/
总用量 16
-rw-r--r--. 1 root root 18 7月 30 14:46 echo.text
 
setup模块:查看facts
收集node上的系统信息,可以当作变量进行调用
ansible all -m setup
  

ansible模块学习的更多相关文章

  1. ansible笔记(3):ansible模块的基本使用

    ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...

  2. 审计系统---paramiko模块学习

    paramiko模块学习 [更多参考]http://www.cnblogs.com/wupeiqi/articles/4963027.html [paramiko的Demo实例]https://git ...

  3. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  4. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  5. ansible模块

    ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...

  6. # nodejs模块学习: express 解析

    # nodejs模块学习: express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子 ...

  7. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  8. pythone函数基础(7)第三方模块学习

    一,time模块学习 import time # print(int(time.time()))#时间戳# res = time.strftime('%Y-%m-%d %H:%M:%S')#取当前格式 ...

  9. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  2. Android 五种存储方式个人总结

    一 . 文件存储 FileOutputStream out = openFileOutput("data",Context.MODE_PRIVATE); BufferedWrite ...

  3. Javascript中的prototype与继承

    通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性列表.javascript创建对象时采用了写时复制的理念. 只有构造器才具有prototype属性,原型链继承 ...

  4. BZOJ3195: [Jxoi2012]奇怪的道路【状压DP】

    Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期有n座城市,编号为1..n.m条道路连接在这些城市之间,每 ...

  5. ECUST 12级 Practise

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26399#overview 果断开小号水过 CodeForces 58A #include ...

  6. VUE的使用方法

    vueInit: function() { var _this = this; this.vue = new Vue({ el: '#pa', data: { //存放初始化数据 sourceData ...

  7. 基于 jmeter 和 shell 的接口性能自动化

    jmeter+shell 1. 总体需求 由于性能测试中涉及的查询接口多,版本迭代频繁,版本更新后自动跑一轮查询业务的性能,可以及时发现一些开发修复bug触发的非预期的bug,利用晚上时间快速重测性能 ...

  8. Hystrix已经停止开发,官方推荐替代项目Resilience4j

    随着微服务的流行,熔断作为其中一项很重要的技术也广为人知.当微服务的运行质量低于某个临界值时,启动熔断机制,暂停微服务调用一段时间,以保障后端的微服务不会因为持续过负荷而宕机.本文介绍了新一代熔断器R ...

  9. ffmpeg C++程序编译时报__cxa_end_catch错误

    解决方法在编译sh中加上 -lsupc++ 即可. 2.STL模块函数找不到,链接失败stdc++/include/bits/stl_list.h:466: error: undefined refe ...

  10. Windows应用程序的VC链接器设置

    Windows应用程序的VC链接器设置 /*转载请注明出自 听风独奏 www.GbcDbj.com */ Windows应用程序分为GUI(Graphical User Interface)和CUI( ...