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