Ansible 설치 및 초기 사용

설치 하 다
1.yum 설치
1.1 epel 소스 설치
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto

1.2,설치 ansible
yum install ansible

2.컴 파일 설치
2.1 설치 의존
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto

2.2 다운로드
wget https://codeload.github.com/ansible/ansible/zip/stable-2.5

설치
python setup.py build && python setup.py install
mkdir /etc/ansible && cp -r examples/* /etc/ansible

2.ansible 의 관리 가능 한 호스트 설정
1.ansible hosts 파일 설정,호스트 그룹 추가
echo -e "[webserver]
192.168.12.157
192.168.12.158" >>/etc/ansible/hosts

2,비밀 키 등록 면제 설정
ssh-copy-id  -i  ~/.ssh/id_rsa.pub  [email protected]
ssh-copy-id  -i  ~/.ssh/id_rsa.pub  [email protected]

3.테스트 실행 명령
[root@linux-test-node1 ~]# ansible webserver -m command -a who
192.168.12.157 | SUCCESS | rc=0 >>
root     tty1         2018-06-06 09:33
root     pts/0        2018-06-06 09:34 (192.168.12.136)
root     pts/1        2018-06-06 10:19 (192.168.12.156)

192.168.12.158 | SUCCESS | rc=0 >>
root     tty1         2018-02-27 01:14
root     pts/0        2018-02-27 01:15 (192.168.12.136)
root     pts/1        2018-02-27 01:59 (192.168.12.156)

4.ansible 의 상용 모듈
4.1,command 모듈
[root@linux-test-node1 ~]# ansible webserver  -a "echo john@2018 |passwd root"        
192.168.12.157 | SUCCESS | rc=0 >>
john@2018 |passwd root

192.168.12.158 | SUCCESS | rc=0 >>
john@2018 |passwd root

주:-m  command 는 생략 할 수 있 습 니 다.command 모듈 은 명령 파 이 프 를 지원 하지 않 습 니 다.셸 모듈 을 사용 해 야 합 니 다.위의 명령 은 암 호 를 변경 하지 않 았 습 니 다.
4.2 셸 모듈
[root@linux-test-node1 ~]# ansible webserver -m shell -a "echo john@2018 |passwd --stdin root" 
192.168.12.157 | SUCCESS | rc=0 >>
Changing password for user root.
passwd: all authentication tokens updated successfully.

192.168.12.158 | SUCCESS | rc=0 >>
     root     。
passwd:                。

4.3,copy 모듈,로 컬 파일 을 대상 서버 에 복사,grou,owner,mode 필요 에 따라 추가
[root@linux-test-node1 ~]# ansible webserver -m copy -a "src=~/ansible_test.file dest=/root/ mode=644 owner=root group=root"
192.168.12.158 | SUCCESS => {
    "changed": true, 
    "checksum": "172c1d29d7392d0959d56c947052f4fe19095f1a", 
    "dest": "/root/ansible_test.file", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "24786862312ecb05a2d09613dff5f1e0", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "src": "/root/.ansible/tmp/ansible-tmp-1510458019.96-84135187555991/source", 
    "state": "file", 
    "uid": 0
}
192.168.12.157 | SUCCESS => {
    "changed": true, 
    "checksum": "172c1d29d7392d0959d56c947052f4fe19095f1a", 
    "dest": "/root/ansible_test.file", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "24786862312ecb05a2d09613dff5f1e0", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "src": "/root/.ansible/tmp/ansible-tmp-1510458019.95-16654666556198/source", 
    "state": "file", 
    "uid": 0
}

4.4 fetch 모듈,원 격 호스트 에서 로 컬 로 파일 복사
[root@linux-test-node1 ~]# ansible webserver -m fetch -a "src=/root/remote_test.file dest=/root/ flat=yes"  
192.168.12.158 | FAILED! => {
    "changed": false, 
    "msg": "file not found: /root/remote_test.file"
}
192.168.12.157 | SUCCESS => {
    "changed": true, 
    "checksum": "76d7d1e26b7507b6aa5f7add865b7585e6a4435c", 
    "dest": "/root/remote_test.file", 
    "md5sum": "27c3eb98c1f99ff12a1184af1113481d", 
    "remote_checksum": "76d7d1e26b7507b6aa5f7add865b7585e6a4435c", 
    "remote_md5sum": null
}
flat=yes  :
 dest=/root/,abc.txt    /root/   
 dest=/root/file,   abc.txt  ,    file

4.5,cron 모듈
1.새 작업
[root@linux-test-node1 ~]# ansible  webserver  -m  cron  -a  "minute=*/10  job='/sbin/ntpdate 10.10.10.10 &> /dev/null'  name=Synctime"   
192.168.12.158 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}
192.168.12.157 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}

주:name 은 계획 작업 의 주석 입 니 다.minute 은 실행 시간 입 니 다.job 는 완전한 작업 입 니 다.
2、퀘 스 트 삭제
[root@linux-test-node1 ~]# ansible webserver -m cron -a "state=absent name=Synctime"
192.168.12.158 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
192.168.12.157 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

4.6,file 모듈,파일,폴 더 삭제,생 성,이동 복사 및 링크 생 성 작업
    
[root@linux-test-node1 ~]# ansible webserver -m file -a "path=/root/test_touch_file state=touch owner=root group=root mode=644"

    
[root@linux-test-node1 ~]# ansible webserver -m file -a "path=/root/test_touch_file state=absent"

    
[root@linux-test-node1 ~]# ansible webserver -m file -a "src=/root/ansible_test.file dest=/home/ansible_test.file state=link"

4.7,yum 모듈
  
[root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd"

  
[root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=absent"

4.8 서비스 모듈
  httpd  
[root@linux-test-node1 ~]# ansible webserver -m service -a "name=httpd state=started"

    
[root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=stopped"

    
[root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=restarted"

4.9.user\group 모듈 은 사용자 와 그룹 을 관리 하 는 데 사 용 됩 니 다.
    
[root@linux-test-node1 ~]# ansible webserver -m user -a "name=john group=root"

    
[root@linux-test-node1 ~]# ansible webserver -m user -a "name=john state=absent remove=yes"

   
[root@linux-test-node1 ~]# ansible webserver -m group -a "name=johngrp"

   
[root@linux-test-node1 ~]# ansible webserver -m group -a "name=johngrp state=absent

좋은 웹페이지 즐겨찾기