ansible 기초용법 ~ 8주

7661 단어
ansible
ansible            ,  python  ,         (puppet,cfengfunc,fabric)   ,         、      、         。  
ansible        ,           。         ansible      。ansible         /    :  
    ①,    connnection plugins:          ;
    ②,host inventory:       ,                ;
    ③,      、command  、     ;
    ④,                ;
    ⑤,playbook:         ,                。 

특성
①   :      ,      
② paramkio,pyyaml,jinja2(    )  
    
③       
④  python  
⑤  playbook    
⑥   
⑦       PKI(  ssl)
⑧            
⑨YAML  ,    

설치하다.
yum install ansible -y

관련 파일
    
    /etc/ansible/ansible.cfg      ,  ansible    
    /etc/ansible/hosts     
    /etc/ansible/roles/       
  
    /usr/bin/ansible    ,        
    /usr/bin/ansible-doc       ,        
    /usr/bin/ansible-playbook        ,      
    /usr/bin/ansible-pull         
    /usr/bin/ansible-vault       
    /usr/bin/ansible-console   console            

ansible 구문 형식
ansible “    ” -m “  ” -a “    ”

ansible 기본 파일 클래스 모듈 매개 변수
src  :      copy   ]   

dest  :                     ,dest     

content  :    src        ,    content        ,src content        ,     。

force :                    ,   ansible           ,      ,    yes no,    yes,    ,     no,           ,            。

backup  :                     ,   ansible           ,              ,    yes no,    yes ,            ,    ansible             。

owner  :                ,               ,     。

group  :                ,              ,     。

mode  :                ,          "rw-r--r--",     mode=0644  ,      user             ,     mode=u+x  。

encrypt   
decrypt   
view   
edit       
rekey     
create      

ansible 기본 프로그램 클래스 모듈 매개 변수
name  :       

state  :           
      present
      absent
      started
      stopped
      restarted
          reloaded
        enabled=yes

ansible 일반 모듈
Shell:          ,     ,          -m   
    ansible all -m shell -a ‘echo magedu |passwd –stdin wang’ 
 :   all              ,  。
 shell      :
    vim /etc/ansible.cfg
        module_name=shell

Script:        ansible      
    ansible all -m script -a /data/f1.sh

Copy:             
    ansible all -m copy -a “src=/root/f1.sh dest=/tmp/f2.sh owner=wang mode=600 backup=yes”

Fetch:             , Copy  
    ansible all -m fetch -a ‘src=/root/a.sh dest=/data/scripts’ 

File:      
    ansible all -m file -a "path=/root/a.sh owner=wang mode=755“
    ansible all -m file -a ‘src=/app/testfile dest=/app/testfile-link state=link’

Cron:    ,    :minute,hour,day,month,weekday
    ansible all -m cron -a "minute=*/5 job='/usr/sbin/ntpdate
    172.16.0.1 &>/dev/null' name=Synctime" 

Yum:   
    ansible all -m yum -a 'name=httpd state=present'
    ansible all -m yum -a 'name=httpd state=absent'

Service:    
    ansible all -m service -a 'name=httpd state=stopped'
    ansible all -m service -a 'name=httpd state=started enabled=yes'
    ansible all -m service -a 'name=httpd state=reloaded’
    ansible all -m service -a 'name=httpd state=restarted'

User:     
    ansible srv -m user -a 'name=user1 comment=“test user” uid=2048
    home=/app/user1 group=root‘
    ansible srv -m user -a 'name=sysuser1 system=yes home=/app/sysuser1 ’

Group:   
    ansible srv -m group -a name=testgroup system=yes“
    ansible srv -m group -a name=testgroup state=absent" 

Vault       yml  
    ansible-vault encrypt hello.yml 
    ansible-vault decrypt hello.yml 
    ansible-vault view hello.yml 
    ansible-vault edit hello.yml 
    ansible-vault rekey hello.yml
    ansible-vault create new.yml

YML 구문
    
    ①      ,         (——)      。  ,            ( ... )        
    ②       Playbook   ,       playbook   
    ③  #     
    ④        ,     tab  
    ⑤            ,            ,                      
    ⑥YAML           ,k/v         
    ⑦  k/v         ,    ,  v      ,        
    ⑧                   name   task
    ⑨  name      task
    ⑩YAML        yml yaml

  ①
    name: John Smith
    age: 41
    gender: Male
    spouse:
      name: Jane Smith
      age: 37
      gender: Female
    children:
      - name: Jimmy Smith
        age: 17
        gender: Male
      - name: Jenny Smith
        age 13
        gender: Female

  ②:
        ---
    #test httpd role
    - hosts: websrvs:appsrvs

    roles:
      - role: httpd
        tags: web
        when: ansible_distribution_major_version == "6"
      - role: nginx
        tags: web2
        when: ansible_distribution_major_version == "7"

playbook
    :
    hosts:              ,           
           :
        one.example.com
        one.example.com:two.example.com
        192.168.1.50
        192.168.1.*
        Websrvs:dbsrvs   ,      
        Websrvs:&dbsrvs  ,      
        webservers:!phoenix  websrvs ,   dbsrvs 
          : - hosts: websrvs:dbsrvs

    tasks:    
          task     name,  playbook       。
      :
        tasks:
        - name: disable selinux
          command: /sbin/setenforce 0

    
    --check -C            ,        
    --list-hosts          
    --list-tags   tag
    --list-tasks   task
    --limit                   
    -v -vv -vvv     
  
    ansible-playbook file.yml --check    
    ansible-playbook file.yml
    ansible-playbook file.yml --limit websrvs

Roles
roles       、  、  、              ,      include       。                ,                 

     
/roles/project/:    ,     
    files/ :   copy script        
    templates/:template              
    tasks/:  task,role     ,          main.yml   ;              include    
    handlers/:          main.yml   ;              include    
    vars/:    ,          main.yml   ;              include   z
    meta/:                 ,          
    main.yml   ,            include    
    default/:              main.yml  

  role
    1,   roles     
    2, roles            , httpd,nginx 
    3,        files,handlers,meta,tasks,templates,vars 
    4, playbook   ,     

      
    -hosts: all
     remote_user: root

     roles:
       - mysql
       - nginx

  :      httpd  
    tree roles/httpd/
        roles/httpd/
        ├── files
        │   ├── httpd6.conf
        │   ├── httpd7.conf
        │   └── index.html
        └── tasks
            ├── conf.yml
            ├── data.yml
            ├── install.yml
            ├── main.yml
            └── service.yml

        2 directories, 8 files

    cat roles/httpd/tasks/*.yml
        - name: conf6
          copy: src=files/httpd6.conf dest=/etc/httpd/conf/httpd.conf 
          when: ansible_distribution_major_version ==  "6"

        - name: conf7
          copy: src=files/httpd7.conf dest=/etc/httpd/conf/httpd.conf 
          when: ansible_distribution_major_version ==  "7"
        - name: data
          copy: src=files/index.html dest=/var/www/html/

        - name: install
          yum: name=httpd

        - include: install.yml
        - include: data.yml
        - include: conf.yml
        - include: service.yml

        - name: service
        service: name=httpd state=started enabled=yes

    cat role-httpd.yml 
        - hosts: all
          roles: 

            - role: httpd

            
        ansible-playbook role-httpd.yml -C 

            
        ansible-playbook role-httpd.yml

  。
  !

전재 대상:https://blog.51cto.com/14234169/2385755

좋은 웹페이지 즐겨찾기