代码改变世界

Ansible使用详解

2020-07-08 18:09  JZpeter  阅读(345)  评论(0)    收藏  举报

ansible使用详解

------------恢复内容开始------------

一、基础环境安装

1、Python安装

(1)wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz

2、Ansible安装

(1)配置yum源使用yum install安装

  yum install ansible -y

(2)检查是否安装成功

  [root@test2 software]# ansible --version
  ansible 2.4.2.0
    config file = /etc/ansible/ansible.cfg
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /usr/bin/ansible
    python version = 2.7.5

二、基本操作

(1)主机组定义,管理主机组清单配置

  [root@test2 software]# vim /etc/ansible/hosts

  #---添加远程主机--

  [storm_cluster]
  192.168.31.135

(2)简单测试

  [root@test2 software]# ansible storm_cluster -m command -a 'uptime'
  192.168.31.135 | SUCCESS | rc=0 >>
   16:34:13 up 2 days, 22:44,  2 users,  load average: 0.00, 0.00, 0.00
   [root@test2 software]# ansible storm_cluster -m command -a 'free -m'
  192.168.31.135 | SUCCESS | rc=0 >>
             total       used       free     shared    buffers     cached
  Mem:          1860        992        867          1        251        432
  -/+ buffers/cache:        309       1551
  Swap:         3999          0       3999
  [root@test2 ~]# ansible 192.168.31.135 -m command -a 'service network restart'
   [WARNING]: Consider using service module rather than running service
  192.168.31.135 | SUCCESS | rc=0 >>
  Shutting down interface eth0:  [  OK  ]
  Shutting down loopback interface:  [  OK  ]
  Bringing up loopback interface:  [  OK  ]
  Bringing up interface eth0:  Determining if ip address 192.168.31.135 is already in use for device eth0...
  [root@test2 software]# ansible storm_cluster -m ping
  192.168.31.135 | SUCCESS => {
      "changed": false,
      "ping": "pong"
  }

①-k参数提示输入密码

  [root@test2 software]# ansible storm_cluster -m ping -k 

  SSH password:
  192.168.31.135 | SUCCESS => {
      "changed": false,
      "ping": "pong"
  }

②all参数表示检查定义的所有主机

  [root@test2 software]# ansible all -m ping

  192.168.31.135 | SUCCESS => {
      "changed": false,
      "ping": "pong"
  }

③查看远程主机基本信息

[root@test2 software]# ansible storm_cluster -m setup
  。。。。。。。
        },
        "ansible_python_version": "2.6.6",
        "ansible_real_group_id": 0,
        "ansible_real_user_id": 0,
        "ansible_selinux": {
            "status": "disabled"
        },
        "ansible_selinux_python_present": true,
        "ansible_service_mgr": "upstart",
        "ansible_ssh_host_key_dsa_public":   "AAAAB3NzaC1kc3MAAACBAP9Irva3y2MsYf9XX3mMfqe6pgh45teqWQmYYd/c1f/b8ks+XpDSfi5/s3kqvRUKHfwcAS+Lltwce8OgP4BG9Pk84o7GN2x9CV2QsgnQZ7gXPlp9PvQomTbfBlXjhQL1So8kpMreUvUPU26Bat/6Pq2iumgFiqR9Yvcrtfj9apK3AAAAFQDNopQhZZhG6+hw+WAjqyyTfltiqwAAAIEAnr458l1qTHbHLMJ7gjf7N5T+lCCNNKkRePl0Q/MVUN6xcR5d0y17NRpLtDXzmtxaxHWuvWKDC/RRcd92Bx4zP7tUW8QqHu3g1TMoV3GILwPx5hg4+I3SGXz8xjMwdOlXETi06l1ryYuHEpt+k4MEDaP/uu8vgB6qGOgskXI1in8AAACBAJe8Ps6E3U+zk7U0VbtELb5E9L2cYcPdoMc+/owcYtUB1w+iU7PIDwvFnZlZVtcX4+k8s/bm073X4WlPXCRL9vg3oIFTDpYDL34vAKmRFzfzKs48m2kIFonWoy6mi7uszrEeDM54Clx57FID39Z4ph0quKkFPJUk8BTcjEn7uWLa",
        "ansible_ssh_host_key_rsa_public":   "AAAAB3NzaC1yc2EAAAABIwAAAQEAqcWEIgXP7Lg70T3Dbe3ozlaSJpFizqd87MLQ6INq5QDwo/gpjsEOQbs/xiC1TVlm/TbpkluQmQtxqx0ppbtPDxFX61HfblzQHJoVdqr9Wkx8Oy/r0j3ADhvB8zT2ESgINXOMF6wwQZn5Os5J/vAJaiBHHg+1HWwMmYZmVmgveytgu+KPw09AtD1HFnwCck3UJ9Podr5PC8BgWqQM+4HDUp5xdxakk7lzh8MxYzSuzEATxwBO21d973L5pS3XlvNC65Ehw3gRIhanBOuhzf7UX8XoxpmJZ5Ivnc/qhyggx1VhM+Bs8y9LzHWQ9ZeXjG/DCbgYlXFNj9MNcO7vwQMaxw==",
        "ansible_swapfree_mb": 3999,
        "ansible_swaptotal_mb": 3999,
  。。。。。。。。。。。。。。

④远程主机符号链接创建

  [root@test2 software]# ansible storm_cluster -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
  192.168.31.135 | SUCCESS => {
      "changed": true,
      "dest": "/tmp/resolv.conf",
      "gid": 0,
      "group": "root",
      "mode": "0777",
      "owner": "root",
      "size": 16,
      "src": "/etc/resolv.conf",
      "state": "link",
      "uid": 0
  }

⑤远程文件信息查看

  [root@test2 software]# ansible storm_cluster -m command -a "cat /tmp/resolv.conf"
  192.168.31.135 | SUCCESS | rc=0 >>
  # Generated by NetworkManager
  search example.com
  ..................
  nameserver 114.114.114.114
  [root@test2 software]# ansible storm_cluster -m command -a "ls -al /tmp/resolv.conf"
  192.168.31.135 | SUCCESS | rc=0 >>
  lrwxrwxrwx 1 root root 16 Apr  1 16:46 /tmp/resolv.conf -> /etc/resolv.conf
⑥删除远程目录或符号链接文件
  [root@test2 software]# ansible storm_cluster -m file -a "path=/tmp/resolv.conf state=absent"
  192.168.31.135 | SUCCESS => {
      "changed": true,
      "path": "/tmp/resolv.conf",
      "state": "absent"
  }
⑦将本地文件/etc/ansible/ansible.cfg拷贝到远程主机/tmp目录下
  [root@test2 software]#  ansible storm_cluster -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
  #---192.168.31.135为远程主机---
  192.168.31.135 | SUCCESS => {
      "changed": true,
      "checksum": "3c3ab06279d6a41562cf059a88fdef66ebf913ab",
      "dest": "/tmp/ansible.cfg",
      "gid": 0,
      "group": "root",
      "md5sum": "f0095343b8331d666cd6f479ded77dff",
      "mode": "0644",
      "owner": "root",
      "size": 19179,
      "src": "/root/.ansible/tmp/ansible-tmp-1554110774.48-58231611971665/source",
      "state": "file",
      "uid": 0
  }
⑧远程文件信息查看
  [root@test2 software]# ansible storm_cluster -m command -a "ls -al /tmp/ansible.cfg"
  192.168.31.135 | SUCCESS | rc=0 >>
  -rw-r--r-- 1 root root 19179 Apr  1 17:25 /tmp/ansible.cfg
⑨将创建的脚本文件分发到远程主机上面  
  vim /tmp/rocketzhang_test.sh
   #!/bin/sh
  date +%F_%H:%M:%S
  1108  chmod +x /tmp/rocketzhang_test.sh
  [root@test2 software]# ansible storm_cluster -m copy -a "src=/tmp/rocketzhang_test.sh dest=/tmp/rocketzhang_test.sh owner=root group=root mode=0755"
  192.168.31.135 | SUCCESS => {
      "changed": true,
      "checksum": "f9bb79acfc5e75eca0e1dcf8b2c850da1d8ee8e2",
      "dest": "/tmp/rocketzhang_test.sh",
      "gid": 0,
      "group": "root",
      "md5sum": "d37ebca9d57ec4948bae6ba34e8dd157",
      "mode": "0755",
      "owner": "root",
      "size": 29,
      "src": "/root/.ansible/tmp/ansible-tmp-1554111597.11-261204682055482/source",
      "state": "file",
      "uid": 0
    }
⑩执行远程脚本文件
  [root@test2 software]# ansible storm_cluster -m shell -a "/tmp/rocketzhang_test.sh"
  192.168.31.135 | SUCCESS | rc=0 >>
  2019-04-01_17:41:33
  [root@test2 ~]# ssh-keyscan 192.168.31.128 >> /root/.ssh/known_hosts
  # 192.168.31.128:22 SSH-2.0-OpenSSH_5.3
  # 192.168.31.128:22 SSH-2.0-OpenSSH_5.3
  # 192.168.31.128:22 SSH-2.0-OpenSSH_5.3
  [root@test2 ~]# vim /etc/ansible/hosts
  192.168.31.128 ansible_ssh_pass=1234567
  192.168.31.135 ansible_ssh_pass=1234567
  [root@test2 ~]# cat main.yam
  - hosts: all
    gather_facts: no
    tasks:
    - name: install ssh key
      authorized_key: user=root
                    key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
                    state=present
  [root@test2 ~]# ansible-playbook main.yam
  PLAY [all] **********************************************************************************************************************************************
  TASK [install ssh key] **********************************************************************************************************************************
  ok: [192.168.31.135]
  fatal: [192.168.31.128]: UNREACHABLE! => {"changed": false, "msg": "Authentication failure.", "unreachable": true}
        to retry, use: --limit @/root/main.retry
  PLAY RECAP **********************************************************************************************************************************************
  192.168.31.128             : ok=0    changed=0    unreachable=1    failed=0  
  192.168.31.135             : ok=1    changed=0    unreachable=0    failed=0  
 
  ssh-keygen -t rsa -b 2048 -P '' -f /root/.ssh/id_rsa
  ssh-keyscan 192.168.31.135 192.168.31.128 >> /root/.ssh/known_hosts
配置palybook
 

------------恢复内容结束------------