动机
记录一下常用的Ansible指令、模块,方便检索。
Ansible主机
/etc/ansible/hosts中,server是目标服务器列表名,包含两个服务器
1 2 3
| [spark] 10.0.0.1 10.0.0.2
|
Ansible Shell模块
1 2
| $ 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
| - 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
| - 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
| ---
- name: start telegraf service: name=telegraf state=started
|
1 2 3 4 5
| - 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
|
- 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
|
- 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
| - 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
| - 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
| - 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
| - 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
| - name: restart docker systemd: state: restarted daemon_reload: yes name: docker become: true
|
Template模块
1 2 3 4 5
| - 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
| - 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
| - 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
|
引用
- http://docs.ansible.com/ansible/latest/