一、添加ansible主機
# vim /etc/ansible/hosts
[server1]
添加以下內容:
192.168.61.130 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=redhat
192.168.61.131 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=redhat
二、查看ansible模塊
# ansible-doc -l
三、ansible常用模塊
1.遠程命令模塊
command
Ansible默認模塊,執行遠程主機上的shell命令
# ansible server1 -m command -a "df -h"
scripts
在遠程主機上執行主控端上的腳本
# ansible server1 -m script -a "/wh_k/test.sh"
shell
在遠程主機上執行遠程主機的腳本
# ansible server1 -m command -a "ls -l /wh_k/test.sh"
# ansible server1 -m command -a "chmod 751 /wh_k/test.sh"
# ansible server -m shell -a "/wh_k/test.sh"
2.copy模塊
向遠程主機拷貝文件
# ansible server1 -m copy -a "src=/wh_k/jmxcmd.jar dest=/wh_k/"
3.stat模塊
獲取遠程主機文件的狀態ctime、mtime等
# ansible server1 -m stat -a 'path=/wh_k/test.sh'
4.yum模塊
遠程主機軟件包管理操作,yum、apt等
# ansible server1 -m yum -a 'name=httpd state=latest'
5.service模塊
遠程主機系統服務管理
# ansible server1 -m service -a 'name=httpd state=start'
6.user模塊
遠程主機用戶
# ansible server1 -m user -a "name=wangwang"