Ansible2016总结,


2016年8月到12月一直在做ansible开发,加班加班加班,学到了不少。。。
本篇总结,主要覆盖了ansible比较基础的入门配置和一些需要注意的Tips,例子基本来自于自己的代码,但是现在官方代码的格式已经完全变成了yaml格式的,大家可以去参考Ansible官网。

一、配置文件

1、ansible.cfg
在ansible.cfg文件中配置远程登录用户、密钥等。
■例

[defaults]
hostfile = ./hosts
remote_user = testadmin
private_key_file=/home/testadmin/.ssh/id_rsa

2、hosts
在hosts文件中配置对象或对象组servers。

vagrant4 ansible_ssh_host=10.456.123.32

[web]
vagrant1

二、Ad-Hoc cmd命令

Ad-Hoc直接在cmd中执行,常用选项:-m指定模块,-a指定参数,-u指定用户等等。
■例

$ ansible NodeName -a "/usr/bin/foo" -u username

$ ansible SVname -m shell -a 'echo $TERM'

$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"

三、playbook中的常用模块

1、file模块:创建或删除文件或文件夹,设置文件的权限、所属用户群组、软链接等。
■例

- name: create abc dir
  file: path={{ abc_dir }} state=directory owner=abc group=cde

Tips:{{ abc_dir }}是指playbook中的变量,可以定义在变量文件中,或者通过-e选项传入。

2、shell模块:执行command
■例

- name: unzip abc.zip
  shell: sudo su - abc_user -c "unzip /mnt/abc/module/abc.zip -d {{ abc_dir }}"
  args:
    creates: "{{ abc_dir }}/abc.sh"

Tips:creates后面的文件若已出现,则不执行此task。

3、yum模块:对packages进行安装、更新、卸载等操作。
■例

- name: install unzip
  yum: name=unzip state=present

4、lineinfile模块:修改文件内容
■例

- name: set serverconfig.properties
  copy: src=serverconfig.properties dest={{ svf_dir }}/UniConX/conf/serverconfig.properties
  notify: restart SVFWebService_UCX

Tips:notify关键字,在task执行完了后,进行handler任务。

5、template模块:可在j2文件中定义变量,转化为想要的配置文件。
■例

- name: set system.properties
  template: src=system.properties.j2 dest={{ apex_dir }}/system.properties

6、copy模块:从本地拷贝文件到远程机器上【远程拷贝到本地使用fetch模块】
■例

- name: copy some jars
  copy: src={{ item }} dest=/usr/target/ owner=root group=root mode=0644
  with_items:
    - abc.jar
    - def.jar

Tips:with_items关键字用于循环,对items逐个进行操作。

7、service模块:对service进行start、stop、enable、restart等操作。
■例

- name: enable and start httpd service
  service: name=httpd enabled=yes state=started

8、postgresql_user模块:对psql的用户进行增删改等操作。
■例

- name: create role/user & grant database
  postgresql_user:
    login_host: "{{ intst_DBhost }}"
    login_user: admin
    login_password: "{{ intst_DBpwd }}"
    db: "{{ intst_DBname }}"
    name: "{{ intst_DBuser }}"
    password: "{{ intst_DBpwd }}"
    priv: "CREATE,CONNECT,TEMPORARY"
  when: CREATE_DB

Tips:when根据变量CREATE_DB的值判断是否执行该task。

9、mount模块:进行mount操作,注意mount模块会修改/etc/fstab文件。

- name: mount Something_v6_1_0.iso
  mount:
    name: "{{ mountdir_any }}"
    src: "{{ target_dir_any }}/Something_v6_1_0.iso"
    fstype: iso9660
    state: mounted

四、条件分支

1、when判断:当满足条件时,执行该task。
■例(下面没使用模块,是因为ansible好像没有创建tablespace的psql相关模块,所以需要判断是否已经创建过了。)

- name: check tablespace created or not
  shell: psql -h '{{ intst_DBhost }}' -U admin postgres -c "select spcname from pg_tablespace;" | grep '{{ intst_TSname }}'
  environment:
    PGPASSWORD: "{{ intst_DBpwd }}"
  when: CREATE_DB
  ignore_errors: True
  register: tablespace_created

- name: create tablespace
  shell: psql -h '{{ intst_DBhost }}' -U admin postgres -c "CREATE TABLESPACE {{ intst_TSname }} LOCATION '{{ intst_DBlocation }}'" 
  environment:
    PGPASSWORD: "{{ intst_DBpwd }}"
  when: CREATE_DB and tablespace_created.rc !=0

2、在template中使用if else判断【playbook中不可用】
■例

{% if SERVER_NAME | search("web") %}
set somthing for web servers
...
{% else %}
set somthing for other servers
...
{% endif %}
{% if UNIT_NAME == "abcd" %}
Listen 18081
{% elif UNIT_NAME == "efgh" %}
Listen 18083
{% endif %}

相关内容

    暂无相关文章