一些ansible roles例子

#理解
changed_when
failed_when
become
become_user
ansible_become
ansible_become_user
static

#检查group_vars中某组是否存在主机
- name: ensure only one monitoring host exists
  fail: msg="One, or no monitoring host may be specified."
  when: "groups.get(‘monitoring_servers‘, [])|length > 1"

#如果不定义值,那么使用默认值
- name: set deploy dir if not presented
  set_fact: deploy_dir="/home/{{deploy_user}}/deploy"
  when: deploy_dir is not defined

#禁掉swap
- name: disable swap
  shell: "([ $(swapon -s | wc -l) -ge 1 ] && (swapoff -a && echo disable)) || echo already"
  ignore_errors: yes
  register: swapoff_result
  changed_when: "swapoff_result.stdout.strip() == ‘disable‘"

#当set_timezone定义,设置相应时区
- name: set timezone to {{timezone}}
  timezone: name={{ timezone }}
  when: set_timezone

#设置hostname
- name: set hostname by ip
  hostname: name=ip-{{ ansible_default_ipv4.address | replace(".","-") }}
  register: hostname_set
  when:
    - set_hostname
    - "ansible_default_ipv4.address | replace(‘.‘,‘-‘) not in ansible_hostname"

#设置/etc/hosts
- name: inject hostname to hosts file
  lineinfile: dest=/etc/hosts line=‘127.0.0.1  ip-{{ ansible_default_ipv4.address | replace(\".\",\"-\") }}‘
  when: set_hostname

‘#修改文件中匹配到的某行
- name:modify centos irqbalance configuration file
  lineinfile:
    dest=/etc/default/irqbalance
    regexp=‘(?<!_)ONESHOT=‘
    line=‘ONESHOT=yes‘
  when:
    - tuning_irqbalance_value
    - centos_irq_config_file.stat.exists

#检查umask
- name: get umask
  shell: umask
  register: umask
  changed_when: False

- name: does the system have a standard umask
  fail: ‘The umask of the system ({{ umask.stdout.strip() }}) prevents successful installation. We suggest a standard umask such as 0022.‘
  when: umask.stdout.strip()[-2:] not in (‘00‘, ‘02‘, ‘20‘, ‘22‘)

#根据facts值,检查系统版本
- name: check system version
  fail:
    msg: "Red Hat Enterprise Linux/CentOS 6 is deprecated"
  when: "ansible_os_family == ‘RedHat‘ and ansible_distribution_major_version == ‘6"

#检查文件挂载点
- name: detemine which mountpoint depoly dir exists on
  shell: "df {{ deploy_dir }} | tail -n1 | awk ‘{print $NF}‘"
  register: deploy_partition
  changed_when: False

#抓取log
- name: fetch pd log file
  fetch:
    src: "{{ log_dir }}/{{ inventory_hostname }}-pd.tar.gz"
    dest: "{{ fetch_tmp_dir }}/{{ inventory_hostname }}/"
    flat: yes
    validate_checksum: no
  when: "‘pd_servers‘ in group_names"

#下载tidb二进制
- name: download tidb binary
  get_url:
    url: "{{ item.url }}"
    dest: "{{ doenloads_dir }}/{{ item.name }}-{{ item.version }}.tar.gz"
    checksum: "{{ item.checksum | default(omit) }}"
    force: yes
    validate_certs: no
  register: get_url_result
  until: "‘OK‘ in get_url_result.msg or ‘file already exists‘ in get_url_result.msg"
  retries: 4
  delay: "{{ retry_stagger | random + 3 }}"
  with_items: "{{ tidb_packages }}"
  when: has_outband_network

#debug
- debug:
    msg: "run command on server: {{ disk_randread_iops.cmd }}"

原文地址:https://www.cnblogs.com/nineep/p/9065874.html

时间: 2024-10-04 23:05:48

一些ansible roles例子的相关文章

Ansible roles实现LAMP部署wordpress

ansible lamp ansible roles实现lamp安装wordpress 整个role目录的结构: .├── ansible.cfg├── hosts├── httpd-php_roles.retry├── httpd-php_roles.yml├── roles│   ├── httpd│   │   ├── files│   │   │   ├── mysql_wp.sh│   │   │   ├── wordpress-4.7.4-zh_CN.tar.gz│   │   │

详解Ansible(Roles)自动化部署配置LAMP架构

Roles简介Ansible为了层次化.结构化地组织Playbook,使用了角色(roles).Roles能够根据层次型结构自动装载变量文件.task以及handlers等.简单来讲,roles就是通过分别将变量.文件.任务.模块及处理器放置于单独的目录中,并可以便捷地include它们,roles一般用于基于主机构建服务的场景中,但也可以用于构建守护进程等场景中.? 创建Roles 创建roles时一般需要以下步骤:首先创建以roles命名的目录.然后在roles目标下分别创建以个角色名称命令

ansible roles 自动化安装

例:  ansible roles 自动化安装memcached 文件目录结构如下: cat memcached_role.yml - hosts: memcached remote_user: root roles: - memcached cat roles/memcached/vars/main.yml username: memcached groupname: memcached memcached_port: 11211 cat roles/memcached/templates/m

五:Ansible Roles

五:Ansible Roles 一:Ansible Roles目录结构 1. 官方推荐最佳实践目录结构定义方式 roles/ common/ # this hierarchy represents a "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file templates/ # <

运维自动化-Ansible roles

一.简介 Ansible1.2版本后引入的新特性,用于层次性.结构化地组织playbook.roles能够根据层次型结构自动装载变量文件.tasks以及handlers等.要使用roles只需要在playbook中使用include指令即可.简单来讲,roles就是通过分别将变量.文件.任务.模板及处理器放置于单独的目录中,并可以便捷地include它们的一种机制.角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中 二.roles目录结构 官方推荐在/etc/ansible

Ansible Roles和最佳实践

角色(roles):把playbook根据功能,如handler,tasks等分门别类的放在在各自的子目录下,形成一个集合,就是角色. Roles目录可以是ansible.cfg中roles_path定义的路径,也可以和入口Playbook文件存放在同级目录.建议使用roles_path,方便统一管理.这篇的示例使用和入口Playbook文件存放在同级目录. Roles are ways of automatically loading certain vars_files, tasks, an

ansible roles实践——安装java

1.tasks/main.yml ---- name: mkdir necessary directory file: path=/usr/java/ state=directory mode=0755 - name: unzip jdk unarchive: src={{ jdk_package_name }} dest=/usr/java/ - name: set env lineinfile: dest={{ env_file }} insertafter={{ item.position

ansible 使用例子

寻找/etc/ 名为"hosts" 递归查找 ansible webserver -m find -a ' path=/etc/ file_type=any recurse=yes  pattern="hosts"' /etc/下包含xiaowei的文件 ansible webserver -m find -a ' path=/etc/ contains='.*xiaowei.*' recurse=yes ' 原文地址:https://www.cnblogs.com

Ansible 之 roles使用

1  概述 角色(roles):把playbook根据功能,如handler,tasks等分门别类的放在在各自的子目录下,形成一个集合,就是角色,建议把角色放在固定的目录下,目录可以ansible的配置文件中的roles_path来定义. role模板查看链接:https://github.com/ansible/ansible-examples 2  配置介绍 以层级结构组织Playbook的核心元素,成为roles. 核心元素包括以下几点: Hosts:主机 Tasks:任务列表 Varia