ansible进阶二

ansible:
优点:无需安装agent(客户端),是通过ssh通道进行管理,目前最流行的自动化软件,没有之一

jenkins:可视化运维(主要是可视化部署),持续构建,可以和git,svn(两个是存放开发代码的仓库)结合,可结合sn’wei’sh实现可视化运维,可结合ansible实现可视化运维

Centos7.3 :(yum -y install net-tools vim)
关闭防火墙,(systemctl stop firewalld,systemctl disable firewalld)
关闭selinux (vim /etc/selinux/config)

#Python3与ansible的安装
安装支持包 yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl

#源码编译
tar xf Python-3.5.2.tgz -C /usr/src
cd /usr/src/Python-3.5.2.tgz
./configure --prefix=/usr/local/python
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3
which python3
#查看并**版本(Python 3.5.2)(python 2.6.6版本不对,只在centos6.5适用)
python3 -v

#安装ansible最新版本
/usr/local/python/bin/pip3 install ansible
ln -s /usr/loca/python/bin/ansible /usr/local/bin
#查ansible版本号(最新版本号)
ansible --version

#查看ansible帮助
/usr/local/python/bin/ansible-doc -l       #查看总帮助
/usr/local/python/bin/ansible-doc -s shell #查看shell模块帮助
#查看raw模块帮助,仅通过ssh使用,不需要通过python,比如客户端没安装python,那么不能使用shell等其他模块了,用不了,只能先安装python,就需要用到raw模块来安装
/usr/local/python/bin/asnible-doc -s raw   #查看raw模块帮助

简单使用

ansible -i /etc/ansible/hosts  #-i 引用配置文件hosts,有这个参数,hosts可以换地方
#如果加入特殊符号还需要加入转义符\ 这样ansible才能正常运行
ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print \$1}'"

ansible-playbook 使用(剧本)

剧本:可以像拍戏一样把各个模块编成一个故事,把所有模块帮我按我想要的顺序执行
ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/
剧本要用yaml结尾。如:test_shell.yaml—>playbook的执行模板

[[email protected] ~]# vim test_shell.yaml
#playbook使用规范格式,python对格式要求非常严格
#hosts tasks name register 都是内置变量 
--- #必须有三个减号"-" 里面冒号后面的空格是必须的
- hosts: ansible01 #这行开头必须有个减号,然后空格 hosts: 代表分发范围,和后边的内容空格
  tasks:           #表示接下来要执行的具体任务,和hosts平级
  - name: test     #name,是tasks的下级,后面test随便写,表示要执行的具体名字
    shell: echo "welcome to yunjisuan" >> /tmp/username #模块具体命令
    register: print_result  #register对上面命令赋值给本行的变量  后面内容是变量名(随意)
  - debug: var=print_result #单独的任务:debug: var= 是固定格式,debug方式输出变量名。
---
- hosts: ansible01
  tasks:
  - name: test
    shell: echo "welcome to yunjisuan" >> /tmp/username
  - name: test2
    shell: echo "welcome to yunjisuan" >> /tmp/username
    register: print_result
  - debug: var=print_result

ansible进阶二

自定义变量

[[email protected] ~]# vim test_shell.yaml
#vars自定义变量,自定义的变量不要和内置变量名冲突
##特别注意,引用变量一定要在双引号里##
---
- hosts: ansible01
  vars:                 #自己定义变量时需要这个模块了
  - Name: "yunjisuan"  #前面时变量名,后面是值
    age: "3"            #前面时变量名,后面是值
  tasks:
  - name: {{ Name }}   #使用变量名时必须两个大括号并且两边空格
    shell: echo "my name {{ Name }} is {{ age }}" >> /tmp/username
    register: qqq
  - debug: var=qqq

ansible进阶二

内置变量调用和查看

获取本地内置变量python方式取的:
ansible 127.0.0.1 -m setup |less

[[email protected] ~]# vim test_shell.yaml
---
- hosts: ansible01
  gather_facts: True #开启系统变量的模块和参数,前面的都是固定格式
  tasks:
  - name: setup var
    shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu{{ ansible_processor_count }}" #里面的就是具体的变量,[0]:学python就知道了
    register: var_result
  - debug: var=var_result

但是有些是需要取值的

python里中括号[],叫做列表,大括号叫字典
python里:整数叫int,带小数点的叫浮点,双引号的叫字符串
ansible进阶二

ansible进阶二

ansible的setup模块非常好用,但给出的信息十分全面,有时候我们并不需要全部的信息。
过滤出指定的信息:例->ansible all -m setup -a "filter=ansible_os_family"
ansible_all_ipv4_addresses:仅显示ipv4的信息
ansible_devices:仅显示磁盘设备信息
ansible_distribution:显示是什么系统,例:centos,suse等
ansible_distribution_major_version:显示是系统主版本
ansible_distribution_version:仅显示系统版本
ansible_machine:显示系统类型,例:32位,还是64位
ansible_eth0:仅显示eth0的信息
ansible_hostname:仅显示主机名
ansible_kernel:仅显示内核版本
ansible_lvm:显示lvm相关信息
ansible_memtotal_mb:显示系统总内存
ansible_memfree_mb:显示可用系统内存
ansible_memory_mb:详细显示内存情况
ansible_swaptotal_mb:显示总的swap内存
ansible_swapfree_mb:显示swap内存的可用内存
ansible_mounts:显示系统磁盘挂载情况
ansible_processor:显示cpu个数(具体显示每个cpu的型号)
ansible_processor_vcpus:显示cpu个数(只显示总的个数)
ansible_python_version:显示python版本

Playbook下发可变配置文件

利用template模块下发可变的配置文件:就是一个普通文件里写入变量,然后批量下发这个文件,并让这个文件里的变量触发
ansible进阶二

[[email protected] ~]# vim test_shell.yaml
---
- hosts: ansible01
  gather_facts: True
  vars:
  - myname: "yunjisuan"
  tasks:
  - name: templat test
    template: src=/tmp/test dest=/root/
    register: var_result
  - debug: var=var_result

asible-playbook test_shell.yaml
ansible进阶二

下发中加入判断语句

[[email protected] ~]# vim /tmp/if.j2
#这是java scripts 的语言,文件名结尾必须.j2 (jinja2)
{% if PORT %}         #假如 PORT 存在
ip=0.0.0.0:{{ PORT }} #则输出。。。
{% else %}            #否则
ip=0.0.0.0:80         #则输出。。。
{% endif %}           #结束语
[[email protected] ~]# vim test_shell.yaml
---
- hosts: ansible01
  gather_facts: True
  vars:
  - PORT: 90
  tasks:
  - name: jinja2 if test
    template: src=/tmp/if.j2 dest=/root/
    register: var_result
  - debug: var=var_result
~                          

Playbook的notify通知和下发nginx配置

ansible进阶二

# 实战下发可执行动作的可变的nginx配置文件
[[email protected] ~]# cat nginx 
worker_porcesses {{ ansible_processor_count }}

[[email protected] ~]# cat test_shell.yaml 
---
- hosts: ansible01
  gather_facts: True
  tasks:
  - name: nginx conf
    template: src=nginx dest=/root/
    notify: #上面的template发生变化触发这个
    - stop nginx
  handlers: #触发了notify,然后触发handlers
  - name: stop nginx
    shell: /usr/local/nginx/sbin/nginx -s stop
    register: var_result
  - debug: var=var_result

使用roles标准化Playbook

roles:规范playbook的编写

#创建roles基本原型的目录结构(有些目录和文件名字是固定的)
#创建一个总目录(名字任意)
[[email protected] ~]# mkdir /myroles
[[email protected] ~]# cd /myroles
#目录里创建文件名作为入口触发文件(文件名任意)
[[email protected] myroles]# touch nginx.yaml
#playbook的原型配置文件目录 (目录名字固定)
[[email protected] myroles]# mkdir roles
[[email protected] myroles]# ls
   nginx.yaml  roles
#进入目录roles,创建相关模组配置目录(名字任意,但是要有含义,让人一目了然知道在干嘛)
[[email protected] myroles]# cd roles
[[email protected] roles]# mkdir nginx
#进入模组配置目录,创建相关模组等任务
[[email protected] roles]# cd nginx/
[[email protected] nginx]# mkdir files handles tasks templates vars
[[email protected] nginx]# ls
  files  handles  tasks  templates  vars
#tree,默认不安装,yum -y install tree
[[email protected] nginx]# tree /myroles/
/myroles/
├── nginx.yaml     #入口触发配置文件
└── roles          #playbook的原型配置目录
    └── nginx      #nginx相关模组配置目录
        ├── files      #copy模块和script模块的参数src默认会从这个文件夹查找
        ├── handles    #用来存放notify通知的,notify触发了,就会从这里找
        ├── tasks      #用来存放ansible模块任务的
        ├── templates  #用来存放j2,可变的变量的,以python语言的变量,ansible变量
        └── vars       #用来存放变量的

7 directories, 1 file

####开始演示####
#######备注:
tasks/:至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
handlers/:至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
vars/:至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
#入口触发配置文件
[[email protected] nginx]# cat /myroles/nginx.yaml
---
- hosts: all         #执行的之际范围
  gather_facts: True #开启系统内置变量
  roles:             #启用roles原型配置
  - nginx            #执行nginx原型模组
#在nginx模组添加tasks任务配置文件
[[email protected] tasks]# cat main.yaml
---
- name: check alived   # 任务1的名字
  ping:                # 执行ping模块
- name:                # 执行2的任务
  shell: ls /          # 执行shell模块
  register: ls_result  # 将执行结果保存给变量(任意起名)
- debug: var=ls_result # 变量的值赋值给debug进行输出

#创建自定义变量vars模组的配置文件
[[email protected] vars]# cat main.yaml
---
my_name: "yunjisuan" #python语言,字符串必须带双引号
phone: 18000000000
[[email protected] tasks]# cat main.yaml
---
- name:
  shell: echo "{{ phone }}" #输出ansible自定义变量,自动寻找vars的内容
  register: echo_result     #自己的任务自己输出给我们看,上面任务也有这个
- debug: var=echo_result

#使用copy,script模块的演化(跟files用)
#脚本使用shell变量就行,不用vars变量
[[email protected] files]# cat test
welcome to yunjisuan
[[email protected] files]# cat test.sh 
echo "aaa" >> /tmp/test
[[email protected] tasks]# cat main.yaml
---
- name: copy  
  copy: src=test dest=/root/
- name: sh    #虽然放在了files里,但是如果脚本使用了vars变量,就放在template里
  scripts: test.sh  #用这个模块直接执行,记得+x权限

### template notify hadlers vars 一起用 ###  
#使用template模块的使用,使用vars里的变量
[[email protected] templates]# cat test.j2 
myname is {{ my_name }},my phone is {{ phone }}
my ipaddress is {{ ansible_all_ipv4_assresses[0] }}
[[email protected] tasks]# cat main.yaml 
---
- name: template_vars
  tamplate: src=test.j2 dest=/root/test2
  
#使用notify模块,默认从/roles/nginx/handles里面找
[[email protected] handlers]# cat main.yaml
---  #handlers 里的动作 ,下面是定义类型
- name: start_nginx
  shell: /usr/local/nginx/sbin/nginx
- name: stop_nginx
  shell: /usr/local/nginx/sbin/nginx -s stop
- name: reload_nginx
  shell: /usr/local/nginx/sbin/nginx -s reload
[[email protected] tasks]# cat main.yaml 
---  
- include: 当前路径的文件名
- name: template_vars
  tamplate: src=test.j2 dest=/root/test2
  notify: start_nginx  #notify通知 这个内容要和handlers里有一样的,否则不能找到