zoukankan      html  css  js  c++  java
  • ansible基本操作

    一、ansible部署

    1.dns resolve(解析)                                      2.install ansible

    [ansible-server]# vim /etc/hosts                         #yum install -y epel-release     

    192.168.0.115 ansible                                        #yum install -y ansible (只需一台机子安装即可)
    192.168.0.104 host1                                          #rpm -qc ansible(查看配置文件)           
    192.168.0.105 host2                                          # ansible-doc -l   (查看所有模块)
    192.168.0.108 host3                                          #ansible-doc -s yum  (看yum模块,了解其功能) 
    192.168.0.109 host4

    注:该文件夹各台机子相同

    3.ssh-key(可选)  使各台机子免密登录              

    #ssh-keygen

    #ssh-copy-id IP地址

    二、基本操作

    Inventory -主机清单

    1、 #vim /etc/ansible/hosts(增加主机组)

    [webserver]
    host1
    host2
    host3
    host4

    2、子分组

    [apache]
    host[1:2]
    [nginx]
    host[3:4]
    [webserver:children]
    apache
    nginx
    [webserver:vars]
    ansible_ssh_user='root'
    ansible_ssh_pass='666666'

    3、自定义主机列表

    #vim hostlist

    [dockers]
    host1
    host2
    [dockers:vars]
    ansible_ssh_user='root'
    ansible_ssh_pass='666666'

    #ansible -i  hostlist dockers  -m ping  -o (测试)

    三、Ad-Hoc-点对点模式

    1.shell模块(-m指定模块,-a为追加操作 -o为压缩输出)

    # ansible all -m shell -a "echo 'mage'|passwd --stdin cui" (改密要交给shell模块,command模块很像它,但是不能改密,因为识别不了“|”管道

    # ansible all -m command -a "echo 'mage'|passwd --stdin cui"  (command模块只能识别最左边的一个shell命令)

    #ansible host2 -m shell -a 'yum -y install httpd' -o  部署apache

    #ansible host3 -m shell -a 'uptime' -o   查询系统负载

    #ansible webserver -m shell -a 'hostname' -o -f 2   -f 2   指定线程数

    #ansible webserver -m shell -a 'hostname' -o    获取主机名

    #ansible-doc shell  (帮助)

    2.复制模块

    #ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'  (复制带备份带指定相关属性)

    # ansible all -m copy -a "content='hello world' dest=/tmp/zjz.ansible mode=640"  (content='hello world'  表示直接生成源文件, 表回车)

    3.用户模块

    创建用户 #ansible webserver -m user -a 'name=qianfeng state=present'

    删除用户 #ansible webserver -m user -a 'name=qianfeng state=absent'

    修改密码  1.生成加密密码  #echo '777777' | openssl passwd -1 -stdin

    ($1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.)

                   2.修改密码  #ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'

    修改#shell ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes' 追加

    4.软件包管理

    #ansible host2 -m yum -a 'name="httpd" state=latest' 安装apache。latest最新的

    [root@localhost ~]# ansible-doc yum

    - state
            install (`present' or `installed', `latest')

           remove (`absent' or`removed') a package
    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'

    # ansible all -m file -a 'path=/tmp/fstab.ansible state=absent'

    # ansible all -m file -a 'path=/tmp/zjz.ansible.link src=/tmp/zjz.ansible state=link' 设置链接文件

    7.收集模块

    #ansible host3 -m setup

    #ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'(filter过滤)

    8、cron模块

    # ansible all -m cron -a "minute='*/5' job='/usr/sbin/ntpdate 192.168.40.132 &>/dev/null' name='sync time'" (sync time为任务名)

    # ansible all -m cron -a " name='sync time' state=absent"  (取消计划任务)

     9、hostname

    [root@localhost ~]# ansible all -m hostname -a name=zjz

    10、setup模块

    # ansible 192.168.40.129 -m setup

    。。。会有很多信息

    11、fetch 拉取模块

    # ansible all -m file -a "content=zjz.tx  dest=/root"  (先创建一个共同的文件)

    # ansible all -m fetch -a "src=/root/zjz.tx dest=/tmp/ flat=yes"

    注:不能拉取文件夹,flat=yes用于区分这是个目录还是路径

     12、command 命令模块

    它是ansible的默认模块,可以允许远程主机范围内的所有shell命令。

    注意: 在command的命令中含有像`$ HOME'这样的变量和像``<“',`”>“, `“”“”,“”;“”和“”&“'将无法正常工作(如果需要这些功能,请使用[shell]模块)

  • 相关阅读:
    JDK的几种分析工具
    心理价值
    通过Proxool辅助数据库优化
    人生缄言
    grep 用法
    多服务器快速定位
    RandomAccessFile读取远程系统日志
    20101116 视频处理几个常用指令
    Flickr架构
    JAVA正则表达式语法
  • 原文地址:https://www.cnblogs.com/zjz20/p/11417734.html
Copyright © 2011-2022 走看看