ansible-playbook操作记录

一、检查环境

系统版本
[root@ansible-master ~]#cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
Ansible版本
[root@ansible-master ~]# ansible --version ansible 2.7.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

检查环境是否安装python
[root@ansible-master ~]#python -V
Python 2.7.5

配置yum源
yum install -y epel-release

在需要的主机上做免密登录!

二、安装Ansible

使用yum安装ansible
yum install -y ansible

进行测试
ansible -C -i /etc/ansible/hosts 192.168.1.1. -m shell -a "ls -la /"

如果可以看到目的主机根目录下的文件夹,表示ansible已经可以正常使用。

三、Ansible简单命令介绍

检查主机的连通性
ansible all -m ping

重启主机
ansible -i /etc/ansible/hosts hostname -a "/sbin/reboot"

传输文件
ansible all -m copy -a "src=my file dest=host flie"

yum包管理
ansible all -m yum -a "yum命令"

基本命令都可以通过模板shell来完成,
ansible all - m shell -a "shell command"

四:使用Playbook批量操作添加多主机
添加主机和主机相关变量

cat test.ini
[test-client]
192.168.1.1
192.168.2.2.

[test-client:vars]
Variable = test_variable

(test-client的主机的变量)

[all:vars]
ansible-user=root
ansible-ssh-pass=123456789

编写用户具体操作步骤

cat test-playbook.yml

  • hosts : test-client
    gather_facts: false
    vars :
    touch_file : devops.file (变量名 : 值)
    tasks :
    - name : touch file (名字)
    shell: “touch /tmp/{{touch_file}}”(模板 :命令)
使用playbook的-i 参数调用ini文件

ansible-playboos -i test.ini test-playbook.yml

如有错误烦请各位大牛多多指正,感谢各位!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值