天天看点

Ansible 简单使用

安装

rpm -Uvh https://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm

yum install -y ansible

配置hosts

vi /etc/ansible/hosts

aly ansible_ssh_port=10011 ansible_ssh_host=127.0.0.1

kaiping ansible_ssh_port=10013 ansible_ssh_host=127.0.0.1

hosts一些参数

ansible_ssh_host

#用于指定被管理的主机的真实IP

ansible_ssh_port

#用于指定连接到被管理主机的ssh端口号,默认是22

ansible_ssh_user

#ssh连接时默认使用的用户名

ansible_ssh_pass

#ssh连接时的密码

ansible_python_interpreter

#用来指定python解释器的路径,默认为/usr/bin/python 同样可以指定ruby 、perl 的路径

执行命令,说明:all 是所以都执行,可以改成组名

ansible all -m command -a 'mkdir -p /usr/local/nba'

执行复制

ansible all -m copy -a "src=/usr/local/1.txt dest=/usr/"

验证

ansible all -a "ls /usr/"

定义cron任务

ansible all -m cron -a'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate

ansible all -a "crontab -l"

yum安装

ansible all -m yum -a "state=present name=nload"

ansible all -a "rpm -qa nload"

启动服务

ansible all -m service -a "state=started name=httpd enabled=yes"

ansible all -m command -a 'rpm -Uvh https://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm'