ansible

install-部署

 

1.dns resolve


环境
ansible服务器
10.18.41.64
ansible客户机
10.18.41.84

10.18.41.82


10.18.41.83
10.18.41.95
ansible服务器
[ansible-server]#vim /etc/hosts
192.168.0.107 ansible
192.168.0.116 host1
192.168.0.117 host2
192.168.0.118 host3
192.168.0.119 host4
ansible客户机
无需配置
IP
YUM源

2.install ansible


ansible服务器
yum install -y epel-release
安装epel源,如果您在非学校环境,请使用下方阿里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 install -y ansible
rpm -ql ansible
列出所有文件
rpm -qc ansible
查看配置文件
ansible --help
查看ansible帮助
ansible-doc -l
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum
看yum模块,了解其功能
install (`present' or `installed', `latest'), or remove (`absent' or `removed')
yum list
Package name
enablerepo

 

基础

1.定义主机清单


vim /etc/ansible/hosts
host1
host2
host3
注意此处少一个主机。请留意

2.测试连通性


ansible localhost -m ping
测试host1连通性
-m 指定模块。什么功能
ping只是其中一个模块。还有shell,yum等等
ansible host1 -m ping
失败。因为要提供用户名密码

3.使用用户名密码测试连通性


ansible host1 -m ping -u root -k
交互密码

4.简洁输出 -o


ansible host1 -m ping -u root -k -o

5.know_hosts


ansible host2 -m ping -u root -k -o
失败的原因是主机认为host3不可靠,给拒绝了。
去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
ansible host2 -m ping -u root -k -o
成功

6.错误示范


ansible host4 -m ping -u root -k -o
失败,主机清单未标注主机。

 

 

shell模块


帮助
ansible-doc shell

ansible webserver -m shell -a 'hostname' -o
获取主机名
ansible webserver -m shell -a 'hostname' -o -f 2
-f 2 指定线程数
ansible host2 -m shell -a 'yum -y install httpd' -o
部署apache
ansible host3 -m shell -a 'uptime' -o
查询系统负载

 

复制模块


帮助
ansible-doc copy
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'
如果文件有多份,可以进行备份。
[root@localhost ~]# ls /tmp/
2.txt 2.txt.17037.2017-11-16@16:23:41~

 

用户模块


帮助
ansible-doc user
ansible webserver -m user -a 'name=qianfeng state=present'
创建
ansible webserver -m user -a 'name=qianfeng state=absent'
删除
ansible webserver -m user -a 'name=qianfeng shell=/sbin/noglogin append=yes'
追加
echo '777777' | openssl passwd -1 -stdin
生成加密密码值
$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'

 

软件包管理


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

 

服务模块


帮助
ansible-doc service
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'
开机禁止启动

 

  文件模块

帮助 ansible-doc file 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'

 

 收集模块

帮助 ansible-doc setup
ansible host3 -m setup
查询所有信息

ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'

 

 

 

YAML-YAML Ain’t Markup Language-非标记语言


语法
列表
fruits:
- Apple
- Orange
- Strawberry
- Mango
字典
martin:
name: Martin D'vloper
job: Developer
skill: Elite
示例1
ansible
ansible all -m yum -a 'name=httpd state=removed' -o
清理一下环境
yum install -y httpd
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf
Listen 8080
修改配置,用作推送
vim apache.yaml
- hosts: host2
tasks:
- name: install apache packges
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 --syntax-check
检验语法
ansible-playbook apache.yaml --list-tasks
列出任务
ansible-playbook apache.yaml --list-hosts
列出主机
ansible-playbook apache.yaml
执行
handlers
如果配置文件发生变化。
Listen 9000
ansible-playbook apache.yaml
再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器
vim apache.yaml

如果配置文件再发生变化。
Listen 9080
ansible-playbook apache.yaml
再次执行,配置生效,触发成功

 



Role-角色扮演


简介
roles类似于saltstack的state,state有一定的组织结构。
而roles则是在ansible中,playbooks的目录组织结构。
而模块化之后,成为roles的组织结构,易读,代码可重用,层次清晰。
目标
通过role远程部署nginx并配置

1.目录结构



准备目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2

2.准备配置文件


vim roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_cores }};
调用内部已知变量
worker_connections {{ worker_connections }};
自定义变量

3.编写剧本


vim roles/site.yaml
- hosts: host4
roles:
- nginx

4.编写任务


vim roles/nginx/tasks/main.yaml
---
- name: install nginx packge
yum: name={{ item }} state=latest
with_items:
- epel-release
- nginx

- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html

- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx

- name: make sure nginx service running
service: name=nginx state=started enabled=yes


5.编写处理程序


vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted

6.编写变量


vim roles/nginx/vars/main.yaml
worker_connections: 10240

7.实施


ansible-playbook site.yaml --syntax-check
测试
ansible-playbook site.yaml
实施剧本
验证host4

posted @ 2018-09-05 11:31  春江花月叶  阅读(104)  评论(0编辑  收藏  举报