python3 ansible api 命令和playbook】的更多相关文章

一.api代码 # coding: utf-8 import os import sys from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars.manager import VariableManager from ansible.inventory.manager import InventoryManager from ansible.exe…
如何把动态生成资产信息.执行playbook以及自定义结果结合起来用呢? #!/usr/bin/env python # -*- coding: utf-8 -*- """ 通过ansible API动态生成ansible资产信息但不产生实际的hosts文件 主机信息都可以通过数据库获得,然后生成指定格式,最后调用这个类来 生成主机信息. """ import sys # 用于读取YAML和JSON格式的文件 from ansible.execu…
官网示例(python3) 说明: 在学习2.0 api的过程中遇到了一个坑,最新版的ansible(2.4)和2.3版本api引用时发生了变化,本文主要使用2.3 api进行操作,2.4只做分析 ansible api (ansible2.4) #!/usr/bin/env python #-*-coding:utf-8 -*- import json from collections import namedtuple from ansible.parsing.dataloader impo…
方法一:通过最原始的操作文件的方式 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 通过操作文件形式动态生成ansible的hosts文件 """ import sys class Inventory: def __init__(self): # ansible的hosts文件路径 self._hostsfile = "./aaa" self._data = self._ge…
一.ansible常用命令 一.ansible命令的常用参数 ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用. 1.命令语法 ansible '*' -m command -a 'uptime' '*':自己定义的主机 -m command:命令 2.命令常用参数 # 指定节点上的权限,属主和数组为r…
Ansible是一个系列文章,我会尽量以通俗易懂.诙谐幽默的总结方式给大家呈现这些枯燥的知识点,让学习变的有趣一些. Ansible系列博文直达链接:Ansible入门系列 前言 通过前面的文章,大家基本上也都知道了Ansible的基本使用了,但是阅读的过程中,大家会发现我有使用以下两种方式来玩这个Ansible. 通过ansible server1 -m ping这样的方式来使用: 通过ansible-playbook shellDemo.yaml这样的方式来使用. 这两种方式有什么区别的呢?…
发现在删掉 ~/.ssh/know_hosts 之后运行 ansible api 会出现以下提示 The authenticity of host '10.1.*.* (10.1.*.*)' can't be established. RSA key fingerprint is ::a5:c0:f8:4a:a2:d2:1a:ef::8f::f5:dd:4a. Are you sure you want to continue connecting (yes/no)? 因为在脚本里面自动运行调用…
7. ansible API(开发应用) 官网链接…
Window下python安装ansible,基于ansible api开发python程序 在windows下使用pycharm开发基于ansible api的python程序时,发现ansible的包无法import.本机安装的是3.7.0的python,cmd下使用pip安装ansible一直安装失败.pycharm中使用File | Settings | Project: runTasks | Project Interpreter也无法导入ansible的包.后来终于找到解决方法. 下…
在第二篇文章中虽然可以执行adhoc和playbook但是执行结果的输出并不是特别直观,虽然没有报错但是到底什么结果其实你是不知道的尤其是在执行adhoc的时候,这时候我们要利用callback来设置一下执行结果的输出. 执行adhoc #!/usr/bin/env python # -*- coding: utf-8 -*- from collections import namedtuple # 核心类 # 用于读取YAML和JSON格式的文件 import sys from ansible…
执行adhoc #!/usr/bin/env python # -*- coding: utf-8 -*- import sys from collections import namedtuple # 核心类 # 用于读取YAML和JSON格式的文件 from ansible.parsing.dataloader import DataLoader # 用于存储各类变量信息 from ansible.vars.manager import VariableManager # 用于导入资产文件…
ansible.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import tempfile from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars.manager import VariableManager from ansible.inventory.manager i…
# -*- coding:utf8 -*- ''' Created on 2017年1月13日 @author: qiancheng ''' import os import json from collections import namedtuple from ansible.inventory import Inventory from ansible.vars import VariableManager from ansible.parsing.dataloader import Da…
一 Ad-Hoc使用场景 Ad-Hoc更倾向于解决简单.临时性任务. 1.1 Ad-Hoc基础命令 基本语法: 1 ansible <host-pattern> [options] <host-pattern>:指Inventory中定义的主机或主机组,可以为ip.hostname.Inventory中的group组名等.同时支持具有“.”.“*”或“:”等特殊字符的匹配型字符串. 提示:<>表示该选项为必选,[]表述是该选项中的参数任选其一或多个. 1 [option…
Python API 2.0 从2.0的事情开始更复杂一些,但是你会得到更多离散和可读的类: #!/usr/bin/env python import json from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory fro…
ansible 命令集 #Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行 /usr/bin/ansible #Ansible 模块功能查看工具 /usr/bin/ansible-doc #下载/上传优秀代码或Roles模块 的官网平台,基于网络的 /usr/bin/ansible-galaxy #Ansible 定制自动化的任务集编排工具 /usr/bin/ansible-playbook #Ansible远程执行命令的工具,拉取配置而非推送配置(使用较少,海量机器时使用,对…
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary { display: block } audio,canvas,progress,video { display: inline-block; vertical-align: baseline } audio:not([co…
playbooks介绍 playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础.Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式.值得注意的是playbook是通过YAML格式来进行描述定义的. playbooks基础组件 Hosts:运行执行任务(task)的目标主机 remote_user:在远程…
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变量.引用.循环等功能,相比ad-hoc而言,其功能要强大的多. 1.1 yaml简单示例 ansible的playbook采用yaml语法.以下是一个yaml格式的文件: --- # Members in Bob's family name: Bob age: 30 gender: Male wif…
一.Ad-Hoc 介绍 1.了解下什么是Ad-Hoc ? Ad-Hoc 其实就是基于Ansible 运行的命令集,有些类似终端中敲入的shell命令,Ansible提供了两种运行完成任务的方式,一种是Ansible Ad-Hoc,一种是Ansible Playbook, 而Ad-Hoc 主要是相对与Ansible Playbook 来讲的,有点类似于shell和shell 脚本之间的关系. 2.Ad-Hoc 主要有那些作用? Ad-Hoc 主要完成一些简单的场景,不需要太多的操作步骤,基本每执行…
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : test2.py # @Author: Anthony.waa # @Date : 2018/12/6 0006 # @Desc : PyCharm # ansible推荐版本:ansible-2.4.1.0 import json import shutil from collections import namedtuple from ansible.parsing.dataloa…
ansible 命令总结 1. Ad-HOC: 适合临时执行任务2. Playbook: 适合一些复杂的部署和配置环境 一 . Ad-HOC: 适合临时执行任务ansible-doc -l 查看ansible的可用模块 1.pnig 检查是否能联通 2.yum 选项:name 安装包名字 state 可以写一些安装或者卸载的命令 例子 ansible webservers -m yum -a 'name=httpd state=latest/absent' 3.command ansible的默…
#!/usr/bin/env python # -*- coding:utf-8 -*- import json import shutil from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.inventory.manager import InventoryManager from ansible.vars.manager import Variab…
copy模块: 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/' file模块: 目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root 命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root" cron模块: 目的:在指定节点上定义一个计划任…
##一个简单的python脚本,通过ansible的api调用get_url模块实现远程下载功能 #!/usr/bin/env python import json import ansible.runner runner = ansible.runner.Runner( module_name='get_url', #模块 module_args='url="http://192.168.1.100/Operation/zhao/script/falcon-agent.sh" des…
sudo apt-get install python3.6 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 150 nohup your_command & #(符号&使程序在后台运行)exit #(退出nohup模式) 启动后,会将程序运行输出记录在当前目录下的nohup.out文件下,如果当前目录不可写,则会被记录在Home目录下的nohup.out文件中 nohup your_…
你想让ansible工作首先就需要设置资产信息,那么我们如何通过使用Python调取Ansible的API来获取资产信息呢? 要提前准备一个hosts文件 获取组或者主机 #!/usr/bin/env python # -*- coding: utf-8 -*- import sys from collections import namedtuple # 核心类 # 用于读取YAML和JSON格式的文件 from ansible.parsing.dataloader import DataLo…
参考:https://www.cnblogs.com/ilurker/p/6421624.html 1. 使用自定义的hosts 格式: ansible  组机匹配  -i  自定义的hosts  -m modulename  -a  "一些参数" ansible ios_cn:ios_tw  -i ./hosts   -m ping ansible all  -i ./hosts   -m ping #all表示应用所有的服务器 #subprocess 调用ansible命令,没有s…
ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用. 我们常用的几个模块: copy    file      cron   group   user    yum   service   script    ping     command     raw    get_url    synchroni…
playbook 相当于多个命令的编排组合然后一起运行,类似写脚本.在学习 playbook 之前需要了解 yaml 格式. 编写playbook的步骤: 定义主机与用户 编写任务列表 执行 playbook 当然 playbook 支持拆分多个文件,并且可以使用多种维度的封装,例如定义变量.任务.处理程序等,鼓励代码/文件复用. 下面是安装 nginx 和 ntp server 的示例. 1.文件目录结构 [root@localhost ansible_demo]# tree nginx_nt…