ansible详解-经常使用模块

Ansible介绍

ansible是一款轻量级的自动化管理工具,相对于puppet,saltstack来讲它更加的轻量化,用python编写。支持多种指令操做,同时也支持playbook。经过ssh进行通讯,客户端无需安装客户端便可进行批量管理,ansible对远程主机的操做具备幂等性,因此能够重复执行而不用担忧有问题。node

ansible组成

  • ansible:主要的服务,用于调用其余各类组件;
  • inventoy:用于存储要控制的主机,包括主机用户名密码等信息;
  • playbooks:用于制定各类playbook;
  • core modules:ansible的核心模块,ansible依赖各类模块进行服务器控制;
  • custome modules:自定义模块;
  • connect plugins:调用python的并发链接器进行服务器链接;
  • other plugins:其余插件,例如日志记录,邮件等;

ansible inventoy配置文件组成

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com    #也可使用此种方法来表示一个地址段,此处表示从001到006

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

[test]
192.168.189.129

ansible控制方式

  • 免密钥控制
  • 经过inventoy记录主机ip地址(主机名)、用户名密码进行控制
#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat'

ansible命令执行方式

  • 经过调用各类模块进行命令执行
  • 经过编写playbook进行服务器各类管理工做
    ansible (all|主机组名|主机名|ip地址) -m 模块名 -a '要调用的模块选项'

ansible配置文件

  • /etc/ansible/ansible.cfg:ansible配置文件
  • /etc/ansible/hosts:inventoy配置文件

ansible获取帮助信息

ansible-doc -s 模块名python

ansible经常使用模块

command:用于执行命令,可是不能应用变量,管道等
ansible test -m command -a 'date'web

shell:相似command,能够用变量,管道等
ansible test -m shell -a 'echo 1234567a |passwd test --stdin'shell

user经常使用选项:vim

  • name:用户名
  • password:密码
  • state:present为添加,absent为删除
  • remove:删除用户,yes|no
  • system:是否为系统用户
  • createhome:是否建立家目录
  • shell:指定用户shell
  • group:设置基本组
  • groups:设置夫家族
  • uid:指定uid
    ansible test -m user -a 'name=hello password=123456 state=present system=yes createhome=no'

group经常使用选项:服务器

  • name:组名
  • gid:gid
  • state:present为建立,absent为删除
  • system:是否为系统组
    ansible all -m group -a 'name=hello system=yes state=present'

cron经常使用选项:并发

  • day:天,1-9,,/3
  • hour:小时
  • month:月
  • name:cron任务名
  • state:present,absent
  • weekday:周
  • minute:分钟
  • job:工做内容
    ansible all -m cron -a 'minute=*/1 name="echo hello world" job="echo hello world" state=present'

copy经常使用选项:dom

  • src:源文件绝对路径或相对路径
  • dest:目标地址,必须是绝对路径
  • content:能够代替src,要写入到dest文件中的内容,会echo追加进去
  • owner:属主
  • group:属组
  • backup:覆盖文件以前是否备份,yes|no
  • directory_mode:递归设定目录权限
  • force:yes,若是文件存在,可是内容不一样,则强行覆盖,默认选项;no,若是文件不存在才复制
ansible test -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=test group=hello'
ansible test -m copy -a 'content="hello world" dest=/tmp/log.ansible'    #把hello world写入目标文件

file经常使用模块,对被控主机文件进行操做:ssh

  • owner:属主
  • group:属组
  • mode:权限
  • recurse:递归设置,对目录有限
  • state
    • touch:建立一个空白文件
    • directory:建立一个新目录,目录存在则不会修改
    • link:建立软连接
    • hard:建立硬连接
    • absent:删除
  • src:当state=link的时候,要被连接的源文件
ansible test -m file -a 'path="/tmp/log.ansible" owner=root group=wheel mode=640'
ansible test -m file -a 'src="/etc/fstab" path="/tmp/fstab.link" state=link'

script,再被控主机执行ansible控制端的脚本:
ansible test -m script -a '/root/test.sh'工具

yum模块经常使用选项:

  • name:要安装的软件名
  • state
    • present:安装
    • absent:卸载
    • lastest:安装为最新版本

setup用于收集指定远程主机的facts信息,这些facts信息能够做为变量被调用:
ansible test -m setup

service经常使用模块,用于控制服务:

  • enabled:该服务是否开机自启
  • name:服务名
  • state
    • started:启动
    • stopped:中止
    • restarted:重启
  • sleep:启动和中止中间sleep时间
  • runlevel:在哪些级别能够自启动
  • arguments:向命令行传递参数
    ansible test -m service -a 'name=httpd enabled=yes state=started'

ping,用于测试远程主机是否在线,回复pong表示在线
ansible test -m ping

相关文章
相关标签/搜索