前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ansible-playbook小记

Ansible-playbook小记

作者头像
禹都一只猫olei
发布2018-12-27 16:23:24
9470
发布2018-12-27 16:23:24
举报

就是将很多的ad-hoc,以yaml格式的形式集合到了一起

hello world

代码语言:javascript
复制
---
- hosts: test
  remote_user: root
  vars:
    com: /root
  tasks:
  - name: hello world
    shell: ls {{ com }}
  • vars自定义变量,引用的时候需要使用"{{}}",注意都使用双引号吧,避免报错
  • tasks是用来指定需要执行的任务

系统变量

代码语言:javascript
复制
{{ ansible_devices.sda.model }}

条件语句

  • when语句
代码语言:javascript
复制
tasks:
- name: "shutdown Debian flavored systems"
  command: /sbin/shutdown -t now
  when: ansible_os_family == "Debian"
  • bool
代码语言:javascript
复制
vars:
  epic: true
tasks:
- shell: echo "This certainly is epic"
  when: peic
- shell: echo "This certainly is epic"
  when: not peic
  • with_items循环语句
代码语言:javascript
复制
- name: add_several users
  user: name={{ item }} state=present groups=wheel
  with_items:
    - testuser1
    - testuser2
  • with_nested嵌套循环
代码语言:javascript
复制
- name: users access control
  mysql_user: name={{ item[0] }}
              priv={{ item[2] }}.*.:ALL
              append_privs=yes
              password=foo
  with_nested:
      - ['alice','bob']
      - ['clientdb','employeedb','providerdb']
  • 有条件的循环
代码语言:javascript
复制
tasts:
   - command: echo {{ item }}
   with_items: [0,2,4,6,8,10]
   when: item > 5

实战

编写一个安装Python flask环境的yml

代码语言:javascript
复制
---
- hosts: test
  remote_user: root
  become: true
  tasks:
  - name: install python for ubuntu
    apt:
      name: "{{ item }}"
      state: latest
      update_cache: yes
    with_items:
      - python-dev
      - python-setuptools
    when: ansible_distribution == 'Ubuntu'
  - name: install python for centos
    yum:
      name: "{{ item }}"
      state: installed
    with_items:
      - python-devel
      - python-setuptools
    when: ansible_distribution == 'CentOS'
  - name: install pip
    shell: easy_install pip
  - name: pip install flask and redis
    pip:
      name: "{{ item }}"
    with_items:
      - flask
      - redis

ansible_distribution系统变量用来检测机器是哪种操作系统

playbook编写zabbix

  • zabbixserver端,以及agent
  • 两台机器,一台centos,一台ubuntu
  • hosts文件编写
代码语言:javascript
复制
centos ansible_ssh_port=22 ansible_ssh_host=xx.xx.xx.1 ansible_ssh_user=root
ubuntu ansible_ssh_port=22 ansible_ssh_host=xx.xx.xx.2 ansible_ssh_user=simon

[test]
centos
ubuntu
  • yml文件编写
代码语言:javascript
复制
---
- hosts: test
  remote_user: root
  become: true
  tasks:
  - name: install zabbix rpm source
    yum:
      name: https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
      state: installed
    when: ansible_distribution == 'CentOS'
  - name: download zabbix deb for ubuntu
    get_url:
      url: https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-2+xenial_all.deb
      dest: /tmp/zabbix.deb
    when: ansible_distribution == 'Ubuntu'
  - name: install zabbix deb for ubuntu
    apt:
      # name: /tmp/zabbix.deb
      deb: /tmp/zabbix.deb
      # state: installed
  - name: install zabbix server -> centos
    yum:
      name: "{{ item }}"
      state: installed
    with_items:
      - zabbix-server-mysql
      - zabbix-proxy-mysql
      - zabbix-web-mysql
    when: ansible_distribution == 'CentOS'
  - name: install zabbix agent -> ubuntu
    apt:
      name: zabbix-agent
      update_cache: yes
      # state: installed
    when: ansible_distribution == 'Ubuntu'
  - name: config zabbix server
    replace:
      path: /etc/zabbix/zabbix_server.conf
      regexp: DBUser=zabbix
      replace: DBUser=root
    when: ansible_distribution == 'CentOS'
  - name: import db format for server
    shell: zcat /usr/share/doc/zabbix-server-mysql-4.0/create.sql.gz | mysql -uroot -p zabbix
    when: ansible_distribution == 'CentOS'
  - name: disable selinux
    selinux:
      state: disabled
    when: ansible_distribution == 'CentOS'
  - name: start zabbix server
    systemd:
      name: zabbix-server
      state: started
    when: ansible_distribution == 'CentOS'
  - name: start zabbix agent
    systemd:
      name: zabbix-agent
      state: started
    when: ansible_distribution == 'Ubuntu'

apt模块,本地的deb文件,参数是debstate参数没有installed状态的。name参数指定不到一个url,需要get_urldeb下载下来,dest指定目录,来安装

后记

若是很多的tasks写在一个yaml文件里面,太臃肿,不好维护,怎么解决呢?那么下次说说它的roles~~peace yo~

本文作者为olei,转载请注明。

ansible ansible-playbook

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • hello world
  • 系统变量
  • 条件语句
  • 实战
    • 编写一个安装Python flask环境的yml
      • playbook编写zabbix
      • 后记
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档