ansible 基本操作(初试)

ansible 初级使用
yum install ansible -y
设置本机免密钥登录到 192.168.1.21
ssh-copy-id 192.168.1.21
vim /etc/ansible/hosts 添加2行
[web]
192.168.1.21

命令行操作方法,:ansible 主机组或ip  -m  指定模块  -a 模块对应的参数

ansible 192.168.1.21 -m shell -a "echo hello"
192.168.1.21 | SUCCESS | rc=0 >>
hello

ansible 192.168.1.21 -m setup # 主机所有信息 
ansible 192.168.1.21 -m setup -a "filter=ansible_hostname" # 用setup模块取得主机名
192.168.1.21 | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "21hostname"
    }, 
    "changed": false
}

ansible web -m shell -a "echo hello"
192.168.1.21 | SUCCESS | rc=0 >>
hello

编写简单的play-book
cat test.yml
---

- hosts: 192.168.1.21
  tasks:
  - name: just test ansible
    shell: echo hello
  - name: get hostname
    setup: filter=ansible_hostname
    
执行 
ansible-play test.yml

PLAY [192.168.1.21] ********************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [192.168.1.21]

TASK [just test ansible] ***************************************************************************************************************************************************
changed: [192.168.1.21]

TASK [get hostname] ********************************************************************************************************************************************************
ok: [192.168.1.21]

PLAY RECAP *****************************************************************************************************************************************************************
192.168.1.21               : ok=3    changed=1    unreachable=0    failed=0

如需要指定其他ip,可以

---

- hosts: all  # 这改成all
  tasks:
  - name: just test ansible
    shell: echo hello
  - name: get hostname
    setup: filter=ansible_hostname
ansible-playbook -i '192.168.1.21,'  test.yml   # -i 指定执行清单 记得192.168.1.21, 后面有逗号.

play-book 中传入默认变量
---
- hosts: 192.168.1.146
  remote_user: root
  vars:
  - hello: hello-world          # 设定hello 变量的默认值为 hello-world
    gather_facts: false       # 可选参数
  tasks:
  - name: var test
    shell: echo {{ hello }} > /dev/pts/0
ansible-play a.yml  输出为hello-world
ansible-play a.yml -e hello=xiaoming 输出为小明

ansible play-book 一个套路,记得最基本的方法,使用其他模块一样的。

play-book 中做条件判断

- name: check kafka 
  shell: pip list | grep kafka | wc -l
  ignore_errors: True
  register: check_kafka
- name: pip install kafka-1.3.5 
  shell: pip install {{ item }}
  with_items:
  - kafka
  when: check_kafka.stdout == "0"

判断是否已经安装了kafka 没安装则安装,已安装则跳过。

- name: install zabbix-agent for centos-7
  yum: name=zabbix22-agent.x86_64 state=present
  when: ansible_os_family == "RedHat" and  ansible_distribution_major_version  == "7"
  
  判断是否是redhat或centos 7 版本,来安装对应的包。

原文地址:http://blog.51cto.com/19941018/2067632

时间: 2024-08-04 16:59:25

ansible 基本操作(初试)的相关文章

ansible基本操作

ansible优点:redhat自带工具,可通过rpm或yum直接安装:客户端免安装:操作通过ssh验证操作:可以通过hosts文件对可操作主机进行分类,方便批量操作 #ansible操作格式,默认hosts文件为/etc/ansible/hosts,默认用户名为rootansible [ -i hosts文件 ] 主机名.组名.域名 [ -u 用户名 ] -m 模块名 [ -k ] #hosts文件格式,默认路径为/etc/ansible/hosts[GROUPNAME]#默认格式 IP:PO

ansible快速入门

系统环境: >     [[email protected] ansible_home]$ python --version >     Python 2.7.5 >     [[email protected] ansible_home]$ cat /etc/redhat-release >     CentOS Linux release 7.1.1503 (Core) >     [[email protected] ansible_home]$ uname -a &g

ansible介绍+基本操作

1ansible介绍 - Ansible基于Python语言实现,由paramiko和PyYAML两个关键模块构建 - 不需要安装客户端,通过sshd去通信 - 基于模块工作,模块可以由任何语言开发 - 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 - 有提供UI(浏览器图形化)www.ansible.com/tower,收费的 - 官方文档  http://docs.ansible.com/ansible/latest/index.html - ansibl

ansible一些基本操作

一.介绍 特性 (1).no agents:不需要在被管控主机上安装任何客户端: (2).no server:无服务器端,使用时直接运行命令即可: (3).modules in any languages:基于模块工作,可使用任意语言开发模块: (4).yaml,not code:使用yaml语言定制剧本playbook: (5).ssh by default:基于SSH工作: (6).strong multi-tier solution:可实现多级指挥. 优点 (1).轻量级,无需在客户端安装

Ansible常用模块基本操作

Ansible是一个系列文章,我会尽量以通俗易懂.诙谐幽默的总结方式给大家呈现这些枯燥的知识点,让学习变的有趣一些. 前言 对于任何一个框架,一个应用,为了更便于推广,便于使用,便于商业化,都会顺便提供很多常用的模块,这样让大家也很容易使用起来.Ansible也是一样的,所以这些常用的模块,就好比基本功,基本招式一样,我们需要掌握这些基本功,掌握这些基本招式.这篇文章,就对这些常用的模块进行一个比较全面的总结. ping模块 ping是测试远程节点的SSH连接是否就绪的常用模块,但是它并不像Li

Ansible 常用模块详解(3)

title: Ansible 常用模块详解(3) date: 2018-12-01 15:22:11 tags: Ansible categories: Ansible copyright: true --- Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是an

ansible基础(1)

目录 一.基础概念 二.基本操作 安装 推送公钥 定义主机清单 ad-hoc(远程执行命令) 我叫张贺,贪财好色.一名合格的LINUX运维工程师,专注于LINUX的学习和研究,曾负责某中型企业的网站运维工作,爱好佛学和跑步. 个人博客:传送阵 笔者微信:zhanghe15069028807,非诚勿扰. 一.基础概念 ansible是什么? ansible是通过调用ssh协议进行批量配置和管理的软件. 为什么用ansible? 如果我们只有两台主机,根本没必要用ansible,但是假如我们有50多

ansible 原理 面试

ansible 简介 ansible是一款自动化的运维工具基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机更新等) 组件: Host Lnventory 英[??nv?ntri]:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等.可以通过file来加载,可以通过CMDB加载 Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版