zoukankan      html  css  js  c++  java
  • ansible的模块使用说明

    参考官方链接:

    https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html#parallelism-and-shell-commands

    • shell模块使用
      提权操作:

    [root@ansible-test ~]# ansible test1 -m shell -a "ls /root" -u zhouyuqiang -k --become -K
    SSH password:
    SUDO password[defaults to SSH password]:
    test1 | CHANGED | rc=0 >>
    anaconda-ks.cfg
    GateOne-master
    master.zip

    • copy操作:
      官方:
      $ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

    实际:
    [root@ansible-test ~]# ansible web1 -m copy -a "src=/root/redis-5.0.3.tar.gz dest=/usr/local/src"

    查询:
    [root@ansible-test ~]# ansible web1 -m shell -a "ls /usr/local/src"
    172.16.188.191 | CHANGED | rc=0 >>
    redis-5.0.3.tar.gz


    • file模块:

    创建文件夹:
    state 一定需要指定:
    状态:
    创建文件夹:directory
    (递归)删除文件:absent
    创建一个文件:touch

    [root@ansible-test ansible-yaml]# ansible web -m file -a "dest=/etc/yum.repos.d/nginx.repo state=absent"
    test2 | CHANGED => {
    "changed": true,
    "path": "/etc/yum.repos.d/nginx.repo",
    "state": "absent"
    }
    test1 | CHANGED => {
    "changed": true,
    "path": "/etc/yum.repos.d/nginx.repo",
    "state": "absent"
    }
    test3 | CHANGED => {
    "changed": true,
    "path": "/etc/yum.repos.d/nginx.repo",
    "state": "absent"
    }


    • yum模块

    state 一定需要
    状态:
    具体版本:present
    最新版本:latest
    name 是指定需要安装的包名

    [root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=present"

    卸载一个版本:
    指定状态就可以:absent
    [root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=absent"


    • user模块
      创建一个用户并设置密码:
      [root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"

    查询:
    [root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"
    删除用户:
    指定状态即可:absent
    [root@ansible-test ~]# ansible web1 -m user -a "name=user1 state=absent"

    指定创建用户不能登录,一般是服务用户:
    [root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=123 shell=/sbin/nologin "
    如下:
    user1❌1002:1003::/home/user1:/sbin/nologin


    • git 模块
      需要指定用户:-uroot
      repo指定git仓库地址
      目标主机需要有git命令,没有进行安装
      [root@ansible-test ~]# ansible web1 -m yum -a "name=git state=latest"

    [root@ansible-test ~]# ansible web1 -vvvv -m git -a "repo=https://github.com/ansible/ansible.git dest=/opt/ansibleå-source" -uroot


    • Managing Services启动服务模块
      启动服务:
      state=started
      state=started
      [root@ansible-test ~]# ansible web -m service -a "name=memcached state=started" -uroot

    关闭服务:
    state=stopped
    [root@ansible-test ~]# ansible web -m service -a "name=memcached state=stopped" -uroot

    重新启动:
    state=restarted

    [root@ansible-test ~]# ansible web -m service -a "name=memcached state=restarted" -uroot

    加入开机启动:
    enabled=true

    [root@ansible-test ~]# ansible web -m service -a "name=memcached enabled=true" -uroot


    • setup模块
      [root@ansible-test ~]# ansible web -m setup

    如:
    "ansible_system_capabilities_enforced": "True",
    "ansible_system_vendor": "VMware, Inc.",
    "ansible_uptime_seconds": 82564,
    "ansible_user_dir": "/root",
    "ansible_user_gecos": "root",
    "ansible_user_gid": 0,
    "ansible_user_id": "root",
    "ansible_user_shell": "/bin/bash",
    "ansible_user_uid": 0,
    "ansible_userspace_architecture": "x86_64",
    "ansible_userspace_bits": "64",
    "ansible_virtualization_role": "guest",

      进行过滤操作:
      
      "filter=ansible_user_shell"
      
      [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_user_shell"
    

    test2 | SUCCESS => {
    "ansible_facts": {
    "ansible_user_shell": "/bin/bash"

        [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_all_ipv4_addresses"
    

    test2 | SUCCESS => {
    "ansible_facts": {
    "ansible_all_ipv4_addresses": [
    "172.16.188.191"
    ]
    },
    "changed": false
    }

    可以通过统配符进行匹配:

    如:filter=ipv4

    [root@ansible-test ~]# ansible web -m setup -a "filter=ipv4"
    test2 | SUCCESS => {
    "ansible_facts": {
    "ansible_all_ipv4_addresses": [
    "172.16.188.191"
    ],
    "ansible_default_ipv4": {
    "address": "172.16.188.191",
    "alias": "eth0",
    "broadcast": "172.16.188.255",
    "gateway": "172.16.188.1",


    • playbook的模块

    yaml的语法,采用yaml语法,具体语法其实很简单,主要是空格区分目录。每个目录层级是2个空格,每个:后面一定要需要空格,否则语法报错

  • 相关阅读:
    Java Jsoup Spider抓取数据入库
    DevOps详解
    七款做好DevOps的强大工具
    DevOps 初学者的入门指南
    DevOps必备的20款顶级工具
    Docker 三大核心工具
    Deploying Docker images via SSH
    Shell实现判断进程是否存在并重新启动脚本
    记录:50多行程序中找出多写的一个字母e
    Android 平板中 自己定义键盘(popuwindow) 居于屏幕左下方 仿微信的password输入界面
  • 原文地址:https://www.cnblogs.com/qiangyuzhou/p/10573194.html
Copyright © 2011-2022 走看看