Ansible常用指令
动机
记录一下常用的Ansible指令、模块,方便检索。
Ansible主机
/etc/ansible/hosts中,server是目标服务器列表名,包含两个服务器
[spark]
10.0.0.1
10.0.0.2
Ansible Shell模块
# 在spark集群上执行ls指令
$ ansible spark -m shell -a "ls"
Ansible-playbook
执行较大的复杂任务时,以YAML
的声明语法来配置,并且可以放置一些模板类文件和资源文件等。
.
|___python.yml
|___python.host
|___roles
| |___python
| | |___defaults # 默认变量
| | |___handlers # 可以被任务使用或者任何该任务之外的地方
| | |___files
| | |___vars # 其它变量
| | |___templates # 模板
| | |___meta # 元数据
| | |___tasks # 主要任务列表
| | | |___main.yml
| |___airflow
| | |___tasks
| | | |___main.yml
| | | |___templates
| | | | |___airflow.cfg
变量
---
# 声明变量
foo:
field1: one
field2: two
# 使用base_path
- hosts: app_servers
vars:
app_path: { { base_path } }/22
# 在命令行传入变量
$ ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"
# 文件形式
$ ansible-playbook release.yml --extra-vars "@some_file.json"
更多的变量还有3类作用域等,以后用到再加。
文件
# 将files目录下的conf/发送到目标节点
- name: copy conf files
copy: src=conf/agent.conf dest=/opt/flume/apache-flume-1.7.0-bin/conf/
handler
# handlers/main.yml
---
- name: start telegraf
service: name=telegraf state=started
# 拷贝docker配置文件并触发docker重启
- name: copy docker conf to dest host
copy: src=conf/docker.conf dest=/etc/telegraf/telegraf.d/
when: "'docker' in group_names"
notify: restart telegraf
任务
roles
目录就是按具体安装的功能模块划分,比如python模块、jdk模块、spark模块等等,他们相互不重复,并且可以有依赖关系,比如jdk -> spark,通过多个role的组合搭配出各种环境的配置方法。
# python/tasks/main.yml
- name: Install pip
apt:
name: python-pip
state: present
become: false
- name: Upgrade pip
pip:
name: pip
extra_args: --upgrade -i https://pypi.doubanio.com/simple/
become: false
# airflow/tasks/main.yml
- name: Create virtualenv for airflow
command: virtualenv /home/ubuntu/airflow_v creates=/home/ubuntu/airflow_v
become: false
- name: Install mysqlclient
apt:
name: libmysqlclient-dev
state: present
become: true
- name: Install airflow within `airflow_v`
pip:
name: apache-airflow[all]
extra_args: -i https://pypi.doubanio.com/simple/
virtualenv: /home/ubuntu/airflow_v
- name: Install celery[redis] within `airflow_v`
pip:
name: celery[redis]
extra_args: -i https://pypi.doubanio.com/simple/
virtualenv: /home/ubuntu/airflow_v
- name: Create Airflow home
file:
path: /home/ubuntu/airflow
state: directory
mode: 0755
- name: Initialize airflow database
command: source /home/ubuntu/airflow_v/bin/activate && /home/ubuntu/airflow_v/bin/airflow initdb creates=/home/ubuntu/airflow/airflow.cfg
environment:
AIRFLOW_HOME: /home/ubuntu/airflow
- name: Syncronize airflow configuration
synchronize:
src: templates/airflow.cfg
dest: /home/ubuntu/airflow/airflow.cfg
become: false
入口文件
python.yml
是ansible入口文件,包含目标host、运行的具体任务。
---
# 发布机上的代码初始化
- hosts: spark
roles:
- python
- airflow
下面是任务中常用的模块
apt安装模块
# 安装pip
- name: Install pip
apt:
name: python-pip
state: present
become: false
Pip安装模块
# 使用pip升级pip
- name: Upgrade pip
pip:
name: pip
extra_args: --upgrade -i https://pypi.doubanio.com/simple/
become: false
# 使用指定虚拟环境安装依赖
- name: Install celery[redis] within `airflow_v`
pip:
name: celery[redis]
extra_args: -i https://pypi.doubanio.com/simple/
virtualenv: /home/ubuntu/airflow_v
Shell模块
- name: Initialize airflow database
command: source /home/ubuntu/airflow_v/bin/activate && /home/ubuntu/airflow_v/bin/airflow initdb creates=/home/ubuntu/airflow/airflow.cfg
environment:
AIRFLOW_HOME: /home/ubuntu/airflow
File模块
# 在目标服务器上创建权限为0755的目录
- name: Create Airflow home
file:
path: /home/ubuntu/airflow
state: directory
mode: 0755
Synchronize模块
# 将本地文件同步到目标服务器上
- name: Syncronize airflow configuration
synchronize:
src: templates/airflow.cfg
dest: /home/ubuntu/airflow/airflow.cfg
become: false
下载模块
# 从指定URL下载文件到目标服务器指定目录
- name: Download the Go tarball
get_url:
url: "{{ go_download_location }}"
dest: /usr/local/src/{{ go_tarball }}
checksum: "{{ go_tarball_checksum }}"
Systemd模块
# 重启docker服务
- name: restart docker
systemd:
state: restarted
daemon_reload: yes
name: docker
become: true
Template模块
# 将templates目录下的docker.my模板复制到目标服务器的/etc/default/docker目录
- name: Configure docker mirror registry to Aliyun
template: src=docker.my dest=/etc/default/docker
notify: restart docker
become: true
Blockinfile模块
# 一个官网例子,在/etc/hosts里添加映射
- name: Add mappings to /etc/hosts
blockinfile:
path: /etc/hosts
block: |
{{ item.ip }} {{ item.name }}
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
with_items:
- { name: host1, ip: 10.10.1.10 }
- { name: host2, ip: 10.10.1.11 }
- { name: host3, ip: 10.10.1.12 }
Copy模块
# 拷贝zookeeper配置文件
- name: copy zookeeper conf to dest host
copy: src=conf/zookeeper.conf dest=/etc/zookeeper/conf/
Service模块
- name: restart telegraf
service: name=telegraf state=restarted
引用
I thirst for magic...