Ansible请君入门

一、简介
    基于Python开发的自动化运维工具,实现了批量系统配置,批量程序部署,批量运行命令等功能。无客户端
二、安装
    yum install -y epel-release   ansible的扩展源
    yum install -y ansible
    扩展:查看ansible支持的模块:ansible-doc -l
三、使用
    1、ssh-key
        1)免密方式
        生成密钥:ssh-keygen
        发送密钥:ssh-copy-id ip
    2、主机清单
        配置需要管理的主机清单文件:/etc/ansible/hosts
        测试连接(实际依赖ssh进程,而非ping指令):ansible ip -m ping -o -u root -k
        说明:-m为指定使用的模块,-o为简洁输出,ip可为目标ip或组名,-u指定登录用户,-k使用密码登录
        1)设置组
            [webserver]    //组名
            alpha.example.org
            192.168.1.110
        2)声明账户和密码
            [webserver]
            alpha.example.org
            192.168.1.110 ansible_ssh_user='root' ansible_ssh_pass='Changeme_123'
        3)定义连接端口
            ssh服务端口被变更(/etc/ssh/sshd_config)为2222
            [webserver]
            alpha.example.org
            192.168.1.110 ansible_ssh_user='root' ansible_ssh_pass='Changeme_123' ansible_ssh_port='2222'
        4)组变量(以webserver组为例,应用给组内所有成员)
            [webserver:vars]
            ansible_ssh_user='root'
            ansible_ssh_pass='Changeme_123' 
        5)子分组
            [webservers:children]
            webserver
            ...其他组名
        6)自定义清单文件
            ansible -i filepath groupname|ip -m ping -o
    3、点对点模式
        查看指令说明:ansible-doc 模块名
        1)复制模块copy
            ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/test.txt owner=root group=root mode=755 baskup=yes'
            说明:-a表示补充参数,backup表示是否在对同名文件操作时进行备份
        2)用户模块user
            创建用户:ansible webserver -m user -a 'name=test'
            修改密码:openssl  passwd -1 -table '密码'  //用来生成密码密文
                      ansible webserver -m user -a 'name=test password="密文"'
            修改登录shell:ansible webserver -m user -a 'name=test shell=/sbin/nologin append=yes'
                           当前设置成了不能登录
            删除用户:ansible webserver -m user -a 'name=test state=absent remove=yes'
            说明:state默认值为present,表示存在或创建用户,absent表示删除用户
        3)软件包管理yum
            ansible webserver -m yum -a 'name="软件名|*" state=latest'
            说明:软件安装时state值为present或installed或latest,软件更新使用latest,软件删除使用absent或removed
        4)服务模块service
            ansible webserver -m service -a 'name="服务名" state=started enabled=yes'
            说明:enabled表示是否设置为开机启动,state的值为started或stopped或restarted或reloaded
        5)文件模块file
            ansible webserver -m file -a 'path=/temp/test.txt mode=755 state=touch'
            说明:state的取值为删除操作absent, 创建目录directory, 修改文件file, 硬链接hard, 软链接link, 创建文件touch
        6)收集模块setup
            ansible webserver -m setup   //收集所有信息
            ansible webserver -m setup -a 'filter=属性名'
        7)拉取文件fetch
            ansible ip -m fetch -a 'src=文件源地址 dest=文件目标地址'
            说明:拉去的文件会以ip分类存储
        8)定时任务管理cron
            创建定时任务:ansible webserver -m cron -a 'name="任务说明" minute="*/10" job="ls -alh > /dev/null"'
            删除定时任务:ansible webserver -m cron -a 'name="任务说明" state=absent'
            说明:时间属性包括minute,hour,day,month,weekday,还可以设置special_time属性的值在特殊时间点执行(annually, daily, hourly, monthly, reboot, weekly, yearly)
        9)用户组模块group
            用法用2)用户模块
        10)脚本模块script
            将本地脚本在指定机器上执行
            ansible webserver -m script -a '/some/local/script.sh --some-argument 1234'
            说明:chdir参数可以指定远端执行脚本的目录
        11)解压缩模块unarchive
            ansible webserver -m unarchive -a 'src=压缩包路径 dest=解压到指定的目录'
            说明:默认压缩包在本地,加压给各远端机器,如果将remote_src参数值设置为yes,则表示压缩包在远端,直接解压即可
        12)shell模块
            可以直接在远端执行shell命令
            ansible webserver -m shell -a 'cat /etc/passwd'
    4、YAML        
        1)语法
            列表
                Animal:
                  - Cat
                  - Dog
                  - Tigger
                注:字段对齐要求,前面有两个空格,-后有一个空格
            字典
                Cat:
                  name: mimi
                  age: 6
                注:字段对其要求,前面有两个空格,:后有一个空格
        2)示例
            ansivle服务器上安装httpd服务:yum install -y httpd
            创建临时存放目录:mkdir temp
            进入临时目录:cd temp
            拷贝http服务的配置文件:cp -rf /etc/httpd/conf/httpd.conf .
            修改httpd.conf文件中的Listen端口80为8080
            编写剧本temp.yaml,内容如下
            - hosts: 指定的IP或组名
              tasks:
              - name: install httpd package
                yum: name=httpd state=present
              - name: copy httpd conf
                copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
              - name: start httpd service
                service: name=httpd state=started enabled=yes
            检查语法:ansible-playbook temp.yaml --syntax-check
            列出任务:ansible-playbook temp.yaml --list-tasks
            列出主机:ansible-playbook temp.yaml --list-hosts
            执行任务:ansible-playbook temp.yaml
            扩展:handlers 用来定义处理程序,比如不能重复restart服务时
            - hosts: 指定的IP或组名
              tasks:
              - name: install httpd package
                yum: name=httpd state=present
              - name: copy httpd conf
                copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
                notify: restart httpd service
              - name: start httpd service
                service: name=httpd state=started enabled=yes
              handlers:
              - name: restart httpd service
                service: name=httpd state=restarted
            说明:只有当notify所配置的操作块执行时,才会触发执行handlers块
    5、角色        
        roles是ansible中playbooks的目录组织结构,将代码或文件进行模块化,成为roles的文件目录组织结构,提高可读性和重用性    
        示例:使用role远程部署nginx并配置
        1)目录结构示例
            roles
                nginx   //角色名
                    files   //普通文件
                        index.html
                    handlers   //触发器任务
                        main.yaml
                    tasks   //主任务
                        main.yaml
                    templates   //金甲模板(变量文件)
                        nginx.conf
                    vars   //自定义变量
                        main.yaml
                site.yaml   //定义执行的剧本
            创建文件夹:mkdir -p roles/nginx/{files,handlers,tasks,templates,vars}
            创建文件:touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
                      echo HelloWorld > roles/nginx/files/index.html
            部署nginx:yum install -y nginx
            拷贝配置文件:cp /usr/local/nginx/conf/nginx.conf roles/nginx/templates/nginx.conf
        2)任务编写
            a)tasks/main.yaml
                ---
                - name: install epel-release package
                yum: name=epel-release state=latest
                
                - name: install nginx package
                yum: name=nginx state=latest
                
                - name: copy index.html
                copy: src=index.html dest=/usr/local/nginx/html/index.html
                
                - name: copy nginx.conf
                template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
                notify: restat nginx
                
                - name: start nginx server
                service: name=nginx state=started enabled=yes
            b)handlers/main.yaml
                ---
                - name: restat nginx
                service: name=nginx state=restarted
            c)templates/nginx.conf
                worker_processes {{ ansible_processor_cores }}   //使用ansible内置变量
                worker_connections  {{ worker_connections }}   //使用自定义变量
                说明:template主要是用来结合变量推送配置,推送前会检查并替换变量
            d)vars/main.yaml
                worker_connections: 10240
            e)site.yaml
                - host:webserver
                  roles:
                  - nginx
        3)任务测试
            cd roles
            检查语法:ansible-playbook site.yaml --syntax-check
            执行任务:ansible-playbook site.yaml    
            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值