之前了解了hosts、vars、tasks、模块、handlers
下面结合使用下
cat test.yml
---
- hosts: 192.168.80.20
tasks:
- name: install httpd
yum: name=httpd state=installed
notify:
- restart httpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
这个例子很简单,对20这台机器做操作,yum安装httpd服务,安装完后执行重启httpd的操作
通过-vvv我们可以看到更加详细的信息
ansible-playbook test.yml -vvv Using /etc/ansible/ansible.cfg as config file PLAYBOOK: test.yml ************************************************************* 1 plays in test.yml PLAY [192.168.80.20] *********************************************************** TASK [setup] ******************************************************************* <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.80.20 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1463550446.91-262274803314712 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1463550446.91-262274803314712 `" )'"'"'' <192.168.80.20> PUT /tmp/tmpJiMRZA TO /root/.ansible/tmp/ansible-tmp-1463550446.91-262274803314712/setup <192.168.80.20> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.80.20]' <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.80.20 '/bin/sh -c '"'"'LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1463550446.91-262274803314712/setup; rm -rf "/root/.ansible/tmp/ansible-tmp-1463550446.91-262274803314712/" > /dev/null 2>&1'"'"'' ok: [192.168.80.20] TASK [install httpd] *********************************************************** task path: /etc/ansible/test.yml:4 <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.80.20 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1463550447.45-214190886104092 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1463550447.45-214190886104092 `" )'"'"'' <192.168.80.20> PUT /tmp/tmpJiMRZA TO /root/.ansible/tmp/ansible-tmp-1463550447.45-214190886104092/yum <192.168.80.20> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.80.20]' <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.80.20 '/bin/sh -c '"'"'LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8 /usr/bin/python -tt /root/.ansible/tmp/ansible-tmp-1463550447.45-214190886104092/yum; rm -rf "/root/.ansible/tmp/ansible-tmp-1463550447.45-214190886104092/" > /dev/null 2>&1'"'"'' NOTIFIED HANDLER restart httpd changed: [192.168.80.20] => {"changed": true, "invocation": {"module_args": {"conf_file": null, "disable_gpg_check": false, "disablerepo": null, "enablerepo": null, "exclude": null, "install_repoquery": true, "list": null, "name": ["httpd"], "state": "installed", "update_cache": false}, "module_name": "yum"}, "msg": "Warning: RPMDB altered outside of yum.\n", "rc": 0, "results": ["Loaded plugins: fastestmirror, refresh-packagekit, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * extras: mirrors.yun-idc.com\n * updates: mirrors.sina.cn\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-47.el6.centos.4 will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-47.el6.centos.4 for package: httpd-2.2.15-47.el6.centos.4.x86_64\n--> Running transaction check\n---> Package httpd-tools.x86_64 0:2.2.15-47.el6.centos.4 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-47.el6.centos.4 updates 831 k\nInstalling for dependencies:\n httpd-tools x86_64 2.2.15-47.el6.centos.4 updates 77 k\n\nTransaction Summary\n================================================================================\nInstall 2 Package(s)\n\nTotal download size: 908 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 78 kB/s | 908 kB 00:11 \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-tools-2.2.15-47.el6.centos.4.x86_64 1/2 \n\r Installing : httpd-2.2.15-47.el6.centos.4.x86_64 2/2 \n\r Verifying : httpd-2.2.15-47.el6.centos.4.x86_64 1/2 \n\r Verifying : httpd-tools-2.2.15-47.el6.centos.4.x86_64 2/2 \n\nInstalled:\n httpd.x86_64 0:2.2.15-47.el6.centos.4 \n\nDependency Installed:\n httpd-tools.x86_64 0:2.2.15-47.el6.centos.4 \n\nComplete!\n"]} RUNNING HANDLER [restart httpd] ************************************************ <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.80.20 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1463550464.02-33682846723600 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1463550464.02-33682846723600 `" )'"'"'' <192.168.80.20> PUT /tmp/tmpJiMRZA TO /root/.ansible/tmp/ansible-tmp-1463550464.02-33682846723600/service <192.168.80.20> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.80.20]' <192.168.80.20> ESTABLISH SSH CONNECTION FOR USER: None <192.168.80.20> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.80.20 '/bin/sh -c '"'"'LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1463550464.02-33682846723600/service; rm -rf "/root/.ansible/tmp/ansible-tmp-1463550464.02-33682846723600/" > /dev/null 2>&1'"'"'' changed: [192.168.80.20] => {"changed": true, "invocation": {"module_args": {"arguments": "", "enabled": null, "name": "httpd", "pattern": null, "runlevel": "default", "sleep": null, "state": "restarted"}, "module_name": "service"}, "name": "httpd", "state": "started"} PLAY RECAP ********************************************************************* 192.168.80.20 : ok=3 changed=2 unreachable=0 failed=0
本文出自 “八英里” 博客,谢绝转载!