美文网首页
ansible基础使用(二)

ansible基础使用(二)

作者: Anson前行 | 来源:发表于2018-02-01 18:18 被阅读17次

    vim /etc/ansible/ansible.cfg

    host_key_checking = False 注释去掉

    ##定义主机组

    vim /etc/ansible/hosts

    [test]

    192.168.1.104 ansible_ssh_user=root ansible_ssh_pass=123456

    192.168.1.103 ansible_ssh_user=root ansible_ssh_pass=123456

    ##查看test主机组主机名

    ansible test  -a "hostname"

    ##查看内存使用情况的命令

    ansible test  -a "free -m"

    ##检测是否正常通信

    ansible test -m ping -o    -f  5 ##    -f 默认进程5  -o 压缩传输

    ##复制文件

    copy ansible test -m copy -a 'src=/etc/ansible/xx dest=/root/xx owner=root group=root mode=644 backup=yes'  -o

    ##安装软件包

     ansible test -m yum -a "name=httpd state=latest" -f5 -o

    ##检测软件包是否安装

    ansible test -m shell -a "rpm -qa httpd" -f 5 -o  #默认模块shell

    ##启动服务

     ansible test -m service -a "name=httpd  state=started    enabled=yes " -f 5 -o

    ##批量创建用户

    ansible test -m user -a "name=xx" -f 5 -o

    ##删除系统用户

     ansible test  -m user -a "name=xx  state=absent remove=yes"

    ## 查看资产信息

    ansible test -m setup

    ##限制命令只在一个服务器上生效

    $ ansible test  -a "service ntpd restart" --limit "10.0.0.132"123

    # Limit hosts with a simple pattern (asterisk is a wildcard).

    $ ansible test -a "service ntpd restart" --limit "*.4" #以4结尾的ip地址,将会执行命令

    ##创建目录

    $ ansible test  -m file -a "dest=/tmp/test mode=644 state=directory"

    ##删除目录和文件

    $ ansible test  -m file -a "dest=/tmp/test state=absent"

    ##管理crontab 任务

    $ ansible test  -m cron -a "name='daily-cron-all-servers' \

    hour=4 job='/path/to/daily-script.sh'"

    ##删除crontab任务

    $ansible test  -m cron -a "name='daily-cron-all-servers' state=absent"

    相关文章

      网友评论

          本文标题:ansible基础使用(二)

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