ansible是一款轻量级的自动化管理工具,相对于puppet,saltstack来讲它更加的轻量化,用python编写。支持多种指令操做,同时也支持playbook。经过ssh进行通讯,客户端无需安装客户端便可进行批量管理,ansible对远程主机的操做具备幂等性,因此能够重复执行而不用担忧有问题。node
# 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
#vim /etc/ansible/hosts [webservers] 192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat'
ansible (all|主机组名|主机名|ip地址) -m 模块名 -a '要调用的模块选项'
ansible-doc -s 模块名
python
command:用于执行命令,可是不能应用变量,管道等
ansible test -m command -a 'date'
web
shell:相似command,能够用变量,管道等
ansible test -m shell -a 'echo 1234567a |passwd test --stdin'
shell
user经常使用选项:vim
ansible test -m user -a 'name=hello password=123456 state=present system=yes createhome=no'
group经常使用选项:服务器
ansible all -m group -a 'name=hello system=yes state=present'
cron经常使用选项:并发
ansible all -m cron -a 'minute=*/1 name="echo hello world" job="echo hello world" state=present'
copy经常使用选项:dom
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
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模块经常使用选项:
setup用于收集指定远程主机的facts信息,这些facts信息能够做为变量被调用:
ansible test -m setup
service经常使用模块,用于控制服务:
ansible test -m service -a 'name=httpd enabled=yes state=started'
ping,用于测试远程主机是否在线,回复pong表示在线
ansible test -m ping