ansible学习记录之ansible安装

1. CentOS6.x x64 服务器端安装

# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm    
# yum -y install ansible

配置文件查看:

[[email protected] ansible]# egrep -v "(#|^$)" /etc/ansible/ansible.cfg    
[defaults]    
hostfile       = /etc/ansible/hosts 
library        = /usr/share/ansible    
remote_tmp     = $HOME/.ansible/tmp    
pattern        = *    
forks          = 5   #默认并发5个。    
poll_interval  = 15    
sudo_user      = root    
transport      = smart    
remote_port    = 22    
module_lang    = C    
gathering = implicit    
sudo_exe = sudo    
timeout = 10    
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}    
action_plugins     = /usr/share/ansible_plugins/action_plugins    
callback_plugins   = /usr/share/ansible_plugins/callback_plugins    
connection_plugins = /usr/share/ansible_plugins/connection_plugins    
lookup_plugins     = /usr/share/ansible_plugins/lookup_plugins    
vars_plugins       = /usr/share/ansible_plugins/vars_plugins    
filter_plugins     = /usr/share/ansible_plugins/filter_plugins    
[paramiko_connection]    
[ssh_connection]    
[accelerate]    
accelerate_port = 5099    
accelerate_timeout = 30    
accelerate_connect_timeout = 5.0    
accelerate_daemon_timeout = 30

2. 配置主机Inventory

ansible通过读取默认的主机清单文件/etc/ansible/hosts,该文件在/etc/ansible/ansible.cfg文件中指定,可以自定义主机,支持IP,域名,支持分组,便于对某

些主机或者某一组功能相同的主机进行操作,还有一个缺省的all组,代表Inventory中所有主机。

# vi /etc/ansible/hosts

[test]  
192.168.10.191    
192.168.10.192

示例:可以指定端口与用户与密码:  
192.168.10.191   ansible_ssh_port=2222  ansible_ssh_user=root ansible_ssh_pass=passwd    
192.168.10.191:2222

3. 免密钥方式配置

置ansible端能基于密钥认证的方式联系各被管理节点。master服务器生成ssh-key,并分发到所有客户端。

# ssh-keygen -t rsa    
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]    
 
在此过程提示输入客户端密码

4. RHEL/CentOS/OEL5.X 客户端安装

RHEL/CentOS/OEL5.X x64 默认采用 python2.4,ansible对python2.4需要安装python-simplejson安装包。

说明: python2.4/2.5版本太低会出现"msg": "Error: ansible requires a json module, none found!", 报错.  
要么升级python,要么安装python-simplejson

[[email protected] Server]# rpm -vih python-simplejson-2.0.9-8.el5.x86_64.rpm    
warning: python-simplejson-2.0.9-8.el5.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 1e5e0159    
Preparing...                ########################################### [100%]    
   1:python-simplejson      ########################################### [100%]

5. ansible命令模块以及参数查询

(1) ansible命令语法

语法 ansible <host-pattern> [options]  
ansible <pattern_goes_here> -m <module_name> -a <arguments>

选项:  
-i 设备列表路径,可制定一些动态路径    
-f 并行任务数    
-private-key 私钥路径    
-m 模块名 默认不指定模块就是采用command模块    
-M 模块夹在路径    
-a 参数    
-k 登陆密码    
-K sudo密码    
-t 输出结果保存路径    
-B 后台运行超时时间    
-P 调查后台程序时间    
-u 执行用户    
-U sudo用户    
-l 限制设备范围    
-s 是此用户sudo无需输入密码

(2) 查看ansible的模块以及参数

# ansible-doc

-l 列出所有的ansible模块  
-s 列出该模块的相关指令

[[email protected] ~]# ansible-doc -l    
            
boundary_meter       Manage boundary meters                                     
bzr                  Deploy software (or files) from bzr branches               
campfire             Send a message to Campfire                                 
capabilities         Manage Linux capabilities                                  
cloudformation       create a AWS CloudFormation stack                          
command              Executes a command on a remote node                        
composer             Dependency Manager for PHP                                 
copy                 Copies files to remote locations.                          
cpanm                Manages Perl library dependencies.                         
cron                 Manage cron.d and crontab entries.                         
datadog_event        Posts events to DataDog  service

[[email protected] ~]# ansible-doc -s command    
- name: E x e c u t e s   a   c o m m a n d   o n   a   r e m o t e   n o d e    
  action: command    
      chdir                  # cd into this directory before running the command    
      creates                # a filename, when it already exists, this step will *not* be run.    
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.    
      free_form=             # the command module takes a free form command to run.  There is no parameter actually named ‘free form‘. See the

examples!  
      removes                # a filename, when it does not exist, this step will *not* be run.    
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.

6. 通过密码方式进行验证,加入-k参数

#提供了三种方式:一个在hosts文件中加入密码,一种是免密钥方式,一种是采用-k方式手动输入密码,常用于临时测试。

[[email protected] ansible]# ansible all -m ping -k

SSH password:    
192.168.10.192 | success >> {    
    "changed": false,    
    "ping": "pong"    
}

192.168.10.191 | success >> {  
    "changed": false,    
    "ping": "pong"    
}

7. 常用示例:

[[email protected] ansible]# ansible test -a ‘df -h‘   #这个省略了-m command模块名,-a接参数,可以指行linux命令

#执行操作系统命令可以采用command,shell,raw等,其中shell可以使用管道以及命令带参,script通常用执行脚本等,具体可以查看相关参数。

192.168.0.125 | success | rc=0 >>  
Filesystem                    Size  Used Avail Use% Mounted on    
/dev/mapper/vg_test2-lv_root   36G  3.6G   30G  11% /    
tmpfs                        1004M   72K 1004M   1% /dev/shm    
/dev/sda1                     485M   39M  421M   9% /boot

192.168.0.124 | success | rc=0 >>  
Filesystem                    Size  Used Avail Use% Mounted on    
/dev/mapper/vg_test1-lv_root   36G  3.6G   30G  11% /    
tmpfs                        1004M   72K 1004M   1% /dev/shm    
/dev/sda1                     485M   39M  421M   9% /boot

单条远程命令的执行,可以通过相关模块进行操作,也可以直接执行命令方式,命令方式将为后面play-book(可以简单理解为批处理)试打下基础。

ansible test -m yum -a "name=dstat state=latest"         #执行yum install dstat  
ansible test -m raw -a "rpm -qa|grep dstat"              #执行rpm -qa |grep dstat    
ansible test -m shell -a "service mysqld restart"        #通过命令方式执行service mysqld restart

单条命令方式:

ansible test -m service -a "name=mysqld state=stopped"   #通过服务模块执行service mysqld stop

play-book方式:

- name: stop mysqld service   #随便取名称  
  service: enabled=off name=rpcbind state=stopped    #模块与参数,单命令行方式也可很容易转成play-book方式。

时间: 2024-08-04 21:25:05

ansible学习记录之ansible安装的相关文章

Ansible学习记录五:PlayBook学习

0.介绍 Playbooks 是 Ansible 管理配置.部署应用和编排的语言,可以使用 Playbooks 来描述你想在远程主机执行的策略或者执行的一组步骤过程等 类似于一组任务集,定义好像项目,组织结构,配置文件等信息,通过task将所要做的事情一步一步的组织在一起就是完整的Playbook 官方Demo资料:https://github.com/ansible/ansible-examples Playbooks是采用YMAL语言结构,基础语法请参考:http://docs.ansibl

ansible学习记录

ansible相关 基于 paramiko 开发的.这个paramiko是什么呢?它是一个纯Python实现的ssh协议库.因此fabric和ansible还有一个共同点就是不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的. Inventory文件用来定义要管理的主机,默认位置/etc/ansible/hosts ansible是一个python package,是个完全的unpack and play软件,对客户端唯一的要求是有ssh有python,并且

Ansible 学习记录 (二)基础介绍

Ansible的优点: Agentless (去中心化) Stupied Simple SSH by default YAML no code Ansible 使用场景: 文件传输, (1)scp mv cp rsync 命令执行: (2) 剩下的其他命令 安装http服务: 安装() 配置() 启动服务(本机的服务启动) 让服务对用户提供服务(路由器配置,iptables) Ansible应用场景分析: (1).Dev开发环境 使用者:程序员     功能:程序员开发软件测试Bug的环境 管理

Ansible学习记录三:配置文件

0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块.插件等配置信息 hosts:机器清单,进行分组管理 1.ansible.cfg # config file for ansible -- http://ansible.com/ # ============================================== # nearly al

[ansible学习笔记]Error: ansible requires the stdlib json or simplejson module, neither was found!

1.执行ansible命令的时候客户端rhel56-192.168.209.133 出现以下错误: [[email protected] ansible]# ansible all -m command -a "python -V" -u reed SSH password: rhel56-192.168.209.133 | FAILED | rc=0 >> Error: ansible requires the stdlib json or simplejson modu

linux学习记录-----vsftpdf服务安装配置

1.挂载iso镜像到mnt下:mount /dev/cdrom /mnt 2.安装vsftpd服务器,在/mnt/Packages/下有所需要的安装包 rpm -ivh vsftpd-******tab提示即可 3.安装成功后,更改配置文件/etc/vsftpd/vsftpd.conf 1) 备份: mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak 2过滤掉注释内容: grep -v "#"  /etc/vsftpd/vsft

学习记录: 安装配置自动化工具ansible

学习记录:  安装配置ansible 更新日期: 2016-11-30 系统环境    :centos6.5 本机ip        :192.168.233.123 被管理机ip :192.168.233.124 ------------------------------------- py版本       :   默认python2.6 ansible版本:ansible 1.7.2 ansible参数:             -m  模块名(执行命令)              -a

ansible学习一(安装)

Ansible是一种集成系统的配置管理工具,使用Ansible可以批量管理服务器,应用的部署,批量的执行命令,并且Ansible还是一款轻量的开源软件,Ansible部署简单,使用yum包就可安装,但是功能的确相当的丰富,所以今天特地来聊聊Ansible.Ansible还有一个特点就是无需安装agent,使用ssh认证就可以远程控制服务器,这也是我喜欢他的一个原因,如果你python够牛的话,还可以自己定制模块,并且还有api接口,许多自动化运维平台都是调用他的.首先,先看一下安装过程: 系统:

Ansible学习 安装

对于运维人员来说,自动化工具是日常工作中比不可少的.Ansible是一个很好的自动化工具. Ansible默认使用SSH协议管理机器,在管理主机上安装Ansible,管理主机和被管理主机只要安装了python,即可使用 1.安装,管理主机环境如下 操作系统: [[email protected] ansible]# cat /etc/redhat-releaseCentOS Linux release 7.3.1611 (Core) python版本: [[email protected] an