美文网首页
ansible学习笔记

ansible学习笔记

作者: 蝉溪一梦 | 来源:发表于2019-10-05 11:04 被阅读0次

cobbler 批量安装系统

ansible 简单配置之inventory(清单)

  1. 免秘钥登录系统

    ssh-keygen
    ssh-copy-id -i id_rsa.pub xzdnsaipso@111.11.252.132
    iptables -I INPUT -s 111.11.208.2  -p tcp --dport 22 -j ACCEPT
    
  • 配置文件

    vim /etc/ansible/hosts

ansible modules (模块)

  1. 参考:

    https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

  2. commad (命令)

    /usr/local/bin/ansible DNS-cache -m command -a "df -h"
    /usr/local/bin/ansible DNS-cache -m command -a "free -g"
    /usr/local/bin/ansible all -m command -a "ifconfig eth0"
    
  3. shell (管道)

    /usr/local/bin/ansible DNS -m shell -a "ifconfig |grep eth*"
    /usr/local/bin/ansible DNS -m shell -a "ifconfig eth0 | awk 'NR==2'"    
    /usr/local/bin/ansible DNS -m shell -a "ifconfig eth0 | awk 'NR==2';df -h;free -g;w"
    
  4. yum 安装软件,仅限root用户

    /usr/local/bin/ansible other -m yum -a "name=httpd state=installed"
    /usr/local/bin/ansible other -m shell -a "rpm -qa|grep httpd"
    /usr/local/bin/ansible other -m yum -a "name=httpd state=removed"
    
  5. copy (copy文件 向文件写东西)

    ansible-doc copy  #查看copy模块用法
    /usr/local/bin/ansible other -m copy -a "src=/etc/hosts dest=/tmp/test"
    /usr/local/bin/ansible other -m command -a "ls /tmp/test -l"
    /usr/local/bin/ansible other -m command -a "cat /tmp/test"
    /usr/local/bin/ansible other -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=alipms group=alipms mode=0600"
    /usr/local/bin/ansible other -m copy -a "src=/etc/hosts dest=/tmp/test backup=yes"  
    /usr/local/bin/ansible other -m copy -a "content='ansible 123 com' dest=/tmp/test backup=yes" 
    
  6. service 启动服务

    /usr/local/bin/ansible other -m service -a "name=httpd state=started|stopped|restarted|reloaded enabled=yes" 
    enabled  是否让服务器开启自动启动
    /usr/local/bin/ansible other -m copy -a "content='This is ansible test.' dest=/var/www/html/index.html"
    /usr/local/bin/ansible other -m service -a "name=httpd state=restarted"
    
  7. script (直接执行shell脚本,但是少用)
    /usr/local/bin/ansible other -m script -a "script.sh"

  8. file 创建文件和目录

    /usr/local/bin/ansible other -m file -a "path=/tmp/file_test state=directory"
    /usr/local/bin/ansible other -m file -a "path=/tmp/file_test/test.txt state=touch"
    path 
    recurse 递归
    state
        directory 在远端创建目录
        touch     在远端创建文件
        link      创建软连接文件
        hard      创建硬连接文件
        mode      设置文件或目录券商
        owner
        group
    
  9. group(root用户)
    /usr/local/bin/ansible other -m group -a "name=gps gid=888"

  10. user(root用户)

    /usr/local/bin/ansible other -m user -a "name=test uid=999 group=888 shell=/sbin/nologin create_home=no"
    /usr/local/bin/ansible other -m user -a "name=test uid=999 group=888 shell=/sbin/nologin state=absen"   删除用户
    uid
    group
    groups       指定附加组
    password     给用户添加密码
    shell        指定用户登录的shell
    create_home  是否创建家目录
    -----------------------------------------------------------------------
    echo 123.com | openssl passwd -salt 'ok.' -stdin ; echo 123.com | openssl passwd -salt 'no.' -stdin
    $1$123456$ubeM.omJvWFVdZGIzK6BV.
    /usr/local/bin/ansible other -m user -a "name=xlm password='$1$NWCVGKb/$uozMg09Bsz2FNGjW72HGA.'"  创建xlm用户并设定密码为:123.com
    
  11. cron

    ansible-doc cron 查看帮助
    /usr/local/bin/ansible other -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh test.sh'"
    /usr/local/bin/ansible other -m cron -a "job='/bin/sh test.sh'"
    /usr/local/bin/ansible other -m cron -a "name='ansible add crontd.' job='/bin/sh test.sh'"
    /usr/local/bin/ansible other -m cron -a "name='ansible add crontd.' job='/bin/sh test.sh' state=absent" 
    /usr/local/bin/ansible other -m cron -a "name='ansible add crontd.' job='/bin/sh test.sh' disabled=yes" 注释定时任务
    
  12. mount (挂载目录设备仅限root用户)

    /usr/local/bin/ansible other -m mount -a "path=/backup src=111.11.252.86:/data fstype=nfs opts=defaults state=present"  只写配置文件 /etc/fstab,并不会挂载
    /usr/local/bin/ansible other -m mount -a "path=/backup src=111.11.252.86:/data fstype=nfs opts=defaults state=mounted"
    state
        absent     卸载设备并清理写入到/etc/fstab配置
        mounted    挂载设备并将配置写入/etc/fstab配置
        present    仅将挂载配置写入到/etc/fstab配置,并不会挂载(鸡肋,不用)
        unmounted  临时卸载,不会清理/etc/fstab配置
    
  1. 检查主机是否ping
    /usr/local/bin/ansible all -m ping
    /usr/local/bin/ansible DNS-cache -m ping
    /usr/local/bin/ansible 111.11.252.86 -m ping
    

相关文章

  • ansible 基础学习

    [TOC] ** 部分内容源于网络,文档是个人学习笔记与记录** 什么是ansible? Ansible is S...

  • ansible学习笔记

    https://gitee.com/dushougudu/learning_notes

  • Ansible 学习笔记

    一、安装 Ansible 是默认来通过 ssh 协议来管理机器的。安装好 ansible 之后不需要启动或者允许一...

  • Ansible学习笔记

    安装好ansible后,检查安装是否成功: 配置文件 Ansible通过以下顺序应用配置文件: ANSIBLE_C...

  • Ansible 学习笔记

    一、简介 Ansible is a radically simple configuration-manageme...

  • ansible学习笔记

    链接 http://docs.ansible.com http://ansible.com.cn

  • Ansible学习笔记

    1. 前言 随着企业服务器数量越来越多,当达到几百台,上千台服务器之后,服务器日常管理也逐渐繁杂,每天如果通过人工...

  • ansible学习笔记

    cobbler 批量安装系统 ansible 简单配置之inventory(清单) 参考:https://docs...

  • ansible学习笔记

    inventory配置 主机变量 组变量 把一个组作为另一个组的子成员

  • ansible 学习笔记

    是什么 自动化工具。用来自动化安装,部署,测试。 为什么 让人专注于做什么,而不是怎么做。(不太关心怎么连接,连接...

网友评论

      本文标题:ansible学习笔记

      本文链接:https://www.haomeiwen.com/subject/qivepctx.html