ansible基础 -- 常用模块

2016-11-11  本文已影响0人  梦想做小猿

一:运行shell命令(command,shell)

ansible默认使用command模块运行,但该模块不支持shell变量和管道,此时则使用shell模块运行命令

ansible all -a "ifconfig"
ansible all -m shell -a "ifconfig | grep 'inet addr'"
ansible all -a "reboot" -f 2
ansible all -a "echo 'hello world'" -u test
ansible all -a "iptables -nL" -u test --sudo [--ask-sudo-pass]

二:传输文件(copy)

ansible all -m copy -a "src=/etc/hosts dest=/home"

三:更改文件权限(file)

ansible all -m file -a "dest=/home/hosts mode=600"
ansible all -m file -a "dest=/home/hosts owner=www group=www"
ansible all -m file -a "dest=/home/a/b/c owner=www group=www state=directory"
ansible all -m file -a "dest=/home/a state=absent"

四:软件包管理(apt,yum)

ansible all -m yum -a "name=nginx state=present"
ansible all -m yum -a "name=nginx1.8 state=present"
ansible all -m yum -a "name=nginx state=latest"
ansible all -m yum -a "name=nginx state=absent"

五:用户和用户组(user,group)

ansible all -m group -a "name=test gid=1000"
ansible all -m user -a "name=foo uid=1000 group=test"
ansible all -m user -a "name=foo state=absent"

六:代码发布(git)

ansible all -m git -a "repo=https://github.com/xxx/xxx.git dest=/var/www/webapps version=HEAD"

七:服务管理(service)

ansible all -m service -a "name=nginx state=started"
ansible all -m service -a "name=nginx state=restarted"
ansible all -m service -a "name=nginx state=stoped"

八:计划任务(cron)

ansible all -m cron -a 'name="ntpdate time" minute=*/5 hour=* day=* mouth=* weekday=* job="/usr/sbin/ntpdate time.windows.com"'
ansible all -m  cron -a 'name="ntpdate time" minute=*/5 hour=* day=* mouth=* weekday=* job="/usr/sbin/ntpdate time.windows.com" user=www'
ansible all -m cron -a 'name="ntpdate time" state=absent'

九:运行脚本(script)

ansible all -m script -a "/home/ansible.sh"

十:下载文件(get_url)

ansible all -m get_url -a "url=http://www.baidu.com/favicon.ico dest=/tmp"

十一:文件推送或拉取(synchronize)

ansible all -m synchronize -a "src=/home/test.txt dest=/tmp compress=yes"
ansible all -m synchronize -a "mode=pull src=/hoome/abc.txt dest=/root"

十二:收集系统信息(setup)

ansible all -m setup
ansible all -m setup --tree /tmp/facts
ansible all -m setup -a "filter=ansible_*_mb"
ansible all -m setup -a "filter=ansible_eth[0-2]"
上一篇 下一篇

猜你喜欢

热点阅读