Ansible

2019-05-30  本文已影响0人  带着小猪闯天下

Ansible

阿里YUM

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
yum -y install ansible

去掉yes/no询问

vim /etc/ssh/ssh_config
StrictHostKeyChecking no

定义主机清单

vim /etc/ansible/hosts
[webserver]
host1
host2
host3
host4
#增加用户名 密码
[webservers]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
#指定服务器端口
[webserver]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
ansible_ssh_port='2222'

测试连通性
-i :指定hosts文件
例:
ansible -i test hosts -m ping -o

ansible 主机名/ip -m ping -o

YAML

vim apache.yaml
- hosts: host2
  tasks:
  - name: install apache packages
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: ensure apache is running
    service: name=httpd state=started enabled=yes

执行:

ansible-playbook apache.yaml

模块:

Ad-Hoc-点对点模式(例)

1.shell模块

##部署apache
ansible host2 -m shell -a 'yum -y install httpd' -o
##查询系统负载
ansible host3 -m shell -a 'uptime' -o

2.复制模块

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'

3.用户模块

##创建用户
ansible webserver -m user -a 'name=xiaoyang state=present'
##删除用户
ansible webserver -m user -a 'name=xiaoyang state=absent'
##修改密码
#### 1.生成加密密码
echo '777777' | openssl passwd -1 -stdin
#### 生成加密密码值
$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.
#### 2.修改密码
ansible webserver -m user -a 'name=xiaoyang' password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
#### 修改shell
ansible webserver -m user -a 'name=xiaoyang shell=/sbin/noglogin append=yes'

4.软件包管理

## 升级所有包
ansible host1 -m yum -a 'name="*" state=latest'
## 安装apache
ansible host2 -m yum -a 'name="httpd" state=latest'

5.服务模块

##启动
ansible host2 -m service -a 'name=httpd state=started'
##开机启动
ansible host2 -m service -a 'name=httpd state=started enabled=yes'
##停止
ansible host2 -m service -a 'name=httpd state=stopped'
##重启
ansible host2 -m service -a 'name=httpd state=restarted'
##开机禁止启动
ansible host2 -m service -a 'name=httpd state=started enabled=no'

6.文件模块

##创建文件
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
##创建目录
ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'

7.收集模块

##查询所有信息
ansible host3 -m setup
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'

使用ansible-playbook遇到的一些小坑
错误写法:

- hosts: docker
  tasks:
  - name: copy docker.tar.gz
    copy: src=/root/hlh/packages/docker.tar.gz dest=/root/docker.tar.gz
    copy: src=/root/hlh/scripts/docker-install.sh dest=/root/docker-install.sh   ##不能连续写

正确写法

- hosts: docker
  tasks:
  - name: copy docker.tar.gz
    copy: src=/root/hlh/packages/docker.tar.gz
          dest=/root/docker.tar.gz
  - name: copy docker-install.sh
    copy: src=/root/hlh/scripts/docker-install.sh  ##分开写
          dest=/root/docker-install.sh
上一篇 下一篇

猜你喜欢

热点阅读