Ansible常用命令

Ansible常用命令

动机

记录一下常用的Ansible指令、模块,方便检索。

Ansible主机

/etc/ansible/hosts中,server是目标服务器列表名,包含两个服务器

1
2
3
[spark]
10.0.0.1
10.0.0.2

Ansible Shell模块

1
2
# 在spark集群上执行ls指令
$ ansible spark -m shell -a 'ls'

Ansible-playbook

执行较大的复杂任务时,以YAML的声明语法来配置,并且可以放置一些模板类文件和资源文件等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.
|___python.yml
|___python.host
|___roles
| |___python
| | |___defaults # 默认变量
| | |___handlers # 可以被任务使用或者任何该任务之外的地方
| | |___files
| | |___vars # 其它变量
| | |___templates # 模板
| | |___meta # 元数据
| | |___tasks # 主要任务列表
| | | |___main.yml
| |___airflow
| | |___tasks
| | | |___main.yml
| | | |___templates
| | | | |___airflow.cfg

变量

1
2
3
4
5
---
# 声明变量
foo:
field1: one
field2: two
1
2
3
4
# 使用base_path
- hosts: app_servers
vars:
app_path: { { base_path } }/22
1
2
3
4
5
# 在命令行传入变量
$ ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"

# 文件形式
$ ansible-playbook release.yml --extra-vars "@some_file.json"

更多的变量还有3类作用域等,以后用到再加。

文件

1
2
3
# 将files目录下的conf/发送到目标节点
- name: copy conf files
copy: src=conf/agent.conf dest=/opt/flume/apache-flume-1.7.0-bin/conf/

handler

1
2
3
4
5
# handlers/main.yml
---

- name: start telegraf
service: name=telegraf state=started
1
2
3
4
5
# 拷贝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的组合搭配出各种环境的配置方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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、运行的具体任务。

1
2
3
4
5
6
---
# 发布机上的代码初始化
- hosts: spark
roles:
- python
- airflow

下面是任务中常用的模块


apt安装模块

1
2
3
4
5
6
# 安装pip
- name: Install pip
apt:
name: python-pip
state: present
become: false

Pip安装模块

1
2
3
4
5
6
7
8
9
10
11
12
13
# 使用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模块

1
2
3
4
- 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模块

1
2
3
4
5
6
# 在目标服务器上创建权限为0755的目录
- name: Create Airflow home
file:
path: /home/ubuntu/airflow
state: directory
mode: 0755

Synchronize模块

1
2
3
4
5
6
# 将本地文件同步到目标服务器上
- name: Syncronize airflow configuration
synchronize:
src: templates/airflow.cfg
dest: /home/ubuntu/airflow/airflow.cfg
become: false

下载模块

1
2
3
4
5
6
# 从指定URL下载文件到目标服务器指定目录
- name: Download the Go tarball
get_url:
url: "{{ go_download_location }}"
dest: /usr/local/src/{{ go_tarball }}
checksum: "{{ go_tarball_checksum }}"

Systemd模块

1
2
3
4
5
6
7
# 重启docker服务
- name: restart docker
systemd:
state: restarted
daemon_reload: yes
name: docker
become: true

Template模块

1
2
3
4
5
# 将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模块

1
2
3
4
5
6
7
8
9
10
11
# 一个官网例子,在/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模块

1
2
3
# 拷贝zookeeper配置文件
- name: copy zookeeper conf to dest host
copy: src=conf/zookeeper.conf dest=/etc/zookeeper/conf/

Service模块

1
2
- name: restart telegraf
service: name=telegraf state=restarted

引用

  1. http://docs.ansible.com/ansible/latest/

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×