ansible 主要参数

-u  指定ssh 连接用户名 

-i   指定/etc/snsible/hosts 文件位置

-m  指定使用的模块  如 shell  yum copy script 

-f 10  指定并发数 

-k   用来输入密码

-a 指定模块的参数 可以是命令等等

ansible 安装

centos 直接是yum安装即可  安装之前 先安装epel源码

rpm -Uvh  

yum install ansible -y 安装即可

定义组 

vim /etc/ansible/hosts 

# 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.

# Ex 2: A collection of hosts belonging to the 'webservers' group

[webservers]   组1 

192.168.80.63

# If you have multiple hosts following a pattern you can specify

# them like this:

# Ex 3: A collection of database servers in the 'dbservers' group

[dbservers]    组2 

192.168.80.64   

# Here's another example of host ranges, this time there are no

# leading 0s:

远程执行命令

ansible -k -i /etc/ansible/hosts all -a "ls"   免秘钥不用-k

ansible -k -m shell -i /etc/ansible/hosts all -a 'ifconfig |grep Bcast' 

ansible 正则表达式使用 

ansible all -k -a 'df -h'

ansible -k -m shell all -a "ifconfig |grep Bcast" 

批量拷贝文件

ansible all -k -m copy -a 'src=/root/hanwei dest=/root/ mode=755 owner=root'

ansible yum 批量安装

ansible all -k -m yum -a "name=openssh*,httpd state=installed"

playbook配置

在/etc/ansible 新建一个以.yaml 结尾的脚本 

vim /etc/ansible/php.yaml 

- hosts: all            文件解析hosts: all        

  remote_user: root        用户

  tasks:                执行的任务

  - name: yum install php       名称

    shell: yum install php -y     执行命令可以有多个用;隔开

 执行脚本

ansible-playbook -k /etc/ansible/php.yaml 

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [192.168.80.64]

ok: [192.168.80.63]

TASK: [yum install php] ******************************************************* 

changed: [192.168.80.64]

changed: [192.168.80.63]

PLAY RECAP ******************************************************************** 

192.168.80.63              : ok=2    changed=1    unreachable=0    failed=0   

192.168.80.64              : ok=2    changed=1    unreachable=0    failed=0   

[root@han2 ~]#