实践操作
目录结构
建立playbooks目录的结构:
cd /data/ansible/ mkdir -p somebook/{group_vars,host_vars,roles} touch somebook/{P_hosts,site.yml,websrvs.yml,appsrvs.yml} tree -C --dirsfirst somebook/
添加主机
vim somebook/P_hosts
# file: P_hosts # All host in here. [hz-srvs] fm_dev1 [sh-srvs] fm_dev2 # appsrvs in all geos [appsrvs:children] hz-srvs sh-srvs # websrvs in all geos [websrvs:children] hz-srvs sh-srvs
主机/组 变量
所有主机都适用的变量:
vim somebook/group_vars/all
--- # file group_vars/all ansible_ssh_port: 22 ansible_ssh_user: root ansible_ssh_private_key_file: /data/ansible/.ssh/id_rsa
设置appsrvs组变量:
vim somebook/group_vars/appsrvs
--- # file: group_vars/appsrvs JAVA_HOME: /usr/local/jdk1.8.0_121
设置websrvs组变量:
vim somebook/group_vars/websrvs
--- # file: group_vars/websrvs nginx: /data/nginx pidfile: /data/nginx/logs/nginx.pid
Playbook
剧本主文件:
vim somebook/site.yml
--- # file: site.yml - import_playbook: appsrvs.yml - import_playbook: websrvs.yml
剧本副文件1:
vim somebook/appsrvs.yml
--- # file: appsrvs.yml - hosts: appsrvs roles: - common - tomcat_spider
剧本副文件2:
vim somebook/websrvs.yml
--- # file: websrvs.yml - hosts: websrvs roles: - common - nginx_check
角色&处理程序
创建目录:
mkdir -p somebook/roles/{common,tomcat_spider,nginx_check}/{tasks,handlers,vars} touch somebook/roles/{common,tomcat_spider,nginx_check}/{tasks,handlers,vars}/main.yml tree -C --dirsfirst somebook/roles/
角色roles/common任务配置:
vim somebook/roles/common/tasks/main.yml
--- # file: roles/common/tasks/main.yml - name: be sure ntpdate installed yum: name: ntpdate state: installed tags: ntpdate - name: syncing ntpdate command: ntpdate cn.pool.ntp.org tags: ntpdate
角色roles/tomcat_spider任务配置:
vim somebook/roles/tomcat_spider/tasks/main.yml
--- # file: roles/tomcat_spider/tasks/main.yml - name: kill tomcat_spider old process shell: ps -ef | grep tomcat_spider/bin | grep -v grep | awk '{print $2}' | xargs kill -9 args: executable: /bin/bash register: result ignore_errors: True - name: start tomcat_spider shell: JAVA_HOME={{JAVA_HOME}} && nohup ../bin/startup.sh args: executable: /bin/bash chdir: /data/tomcat_spider/webapps/ - command: /bin/true when: result is succeeded notify: - tomcat restarted - command: /bin/true when: result is failed notify: - tomcat started
角色roles/tomcat_spider的处理程序:
vim somebook/roles/tomcat_spider/handlers/main.yml
--- # file: roles/tomcat_spider/handlers/main.yml - name: tomcat restarted debug: msg: tomcat_spider has been restarted - name: tomcat started debug: msg: tomcat_spider has been started
角色roles/nginx_check任务配置:
vim somebook/roles/nginx_check/tasks/main.yml
--- # file: roles/nginx_check/tasks/main.yml - name: be sure nginx is started and enabled systemd: name: nginx state: started enabled: yes
运行
声明Ansible的配置文件:
export ANSIBLE_CONFIG=/data/ansible/etc/ansible.cfg
检查主机连接
检查所有主机连接:
ansible -i somebook/P_hosts all -m ping
检查websrvs组的成员主机连接状态:
ansible -i somebook/P_hosts websrvs -m ping
执行临时任务
复制文件:
echo 123 > somefile ansible -i somebook/P_hosts all -m copy -a "src=somefile dest=/home/somefile owner=ngx"
查看:
ansible -i somebook/P_hosts all -a "ls -l /home/somefile" ansible -i somebook/P_hosts all -a "cat /home/somefile"
执行Playbook
查看整个playbook影响主机:
ansible-playbook -i somebook/P_hosts somebook/site.yml --list-hosts
查看限定运行组的playbook影响主机:
ansible-playbook -i somebook/P_hosts somebook/appsrvs.yml --list-hosts
执行整个playbook:
ansible-playbook -i somebook/P_hosts somebook/site.yml
执行单个特定playbook:
ansible-playbook -i somebook/P_hosts somebook/appsrvs.yml
这是个简单的示例,Ansible还可以联合Jenkins整合,通过Jenkins提供参数,来实现复数机器上程序的更新和回滚。
Very interesting details you have noted, appreciate it for posting.