tt
ansible
安装 卸载
yum -y install ansible
yum remove ansibel
命令集
ansible
ansible-config
ansible-doc
ansible-galaxy
ansible-inventory
ansible-playbook
重要文件
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles
inventory 配置
ansible all --list
vim /etc/ansible/hosts
192.168.36.1
teset1 ansible_host= ansible_port=
[apache]
httpd1.cnedu.com ansible_host=
httpd2.cn.com
[nginx]
ngx1.cndu.com
ngx2.cndu.com
[webservers:children]
apache
nginx
[webservers:vars]
ntp_server=ntp.cndu.com
ansible_ssh_user=root
简单playbook
---
- hosts:
remote_user:root
tasks:
- name: name
yum:
- name:
copy:
notify:
- restart ll
- name:
service:
handles:
- name: restart
service:
ansibles 结构目录:
-------roles
| |-------docker
| | |---defaults 默认变量
| | |---files 普通文件
| | | |---daemon.josn
| | |---handles
| | |---meta
| | |---tasks
| | | |---main.yml
| | |---templates
| | |---vars 其他变量
| |-------mariadb
| | |_____tasks
| | | |____main.yml
| |-------mongo
| | |-----tasks
| | |---main.yml
|-------site.yml
roles剧本
site.yml
---
- hosts: server
roles:
- docker
- {role:mongo,port:xxx}
roles/docker/tasks/main.yml
- name:xxxx
yum:
- name:xxx
copy:
notify:
- restart xxx
- name:
service:
roles/docker/handles/main.yml
- name: restart
service: xx
定义变量:
inventory中定义:
example.com http_port=80
playbookz中定义
---
- hosts: 192.168.36.1
vars:
port:
roles:
- docker
- mongo
在role中定义
---
- hosts: 192.168.35.3
roles:
- docker
- { role:mongo,port:2222}
系统信息变量:
ansible hostname -m setup
属性 {{ ansible_hostname }}
层级属性 {{ ansible_devices.vda.size }}
属组 {{ ansible_dns.nameservers[0] }}
使用变量
- name:
docker:
name: mongo
image: mongo:latest
state: started
ports:
- "{{ port }}"
模板
{
{{ mirror_url}}
}
使用
- name:
template: src=daemon.json.j2 dest=/etc/docker/daemon.json
notify:
- restart
注册变量
循环 loops
-name: add serveral users
user:
name: "{{ item }}"
# name: "{{ item.name }}"
state: present
groups: "wheel"
# groups: "{{ item.groups }}"
loop:
- testuser1
- testuser2
- {name:'xx',groups:'wheel'}
- {name:'xx',groups:'wheel'}
条件判断
tasks:
- name: shutdown
command: /sbin/shutdown -t now
when: ansible_os_famliy == ""
- name: xxx
command:
when: or
and
tasks:
- name: shutdown
command: /sbin/shutdown -t now
when:
- ansible_distribution == "centos"
- ansible_distribution_major_version ==""
block