ansible 기초용법 ~ 8주
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.