首页 > 其他分享 >A-Ansible示例

A-Ansible示例

时间:2023-02-23 12:33:57浏览次数:43  
标签:tmp name 示例 dest ansible state Ansible

示例-ping

ansible -k all -m ping

ansible -k all -m command -a "ping -c 2 www.baidu.com"
示例-date
ansible -k -i /etc/ansible/hosts all -m command -a "date"
示例-df-h
ansible -k 172.24.77.242 -m command -a "df -h"
示例-copy
ansible -k all -m copy -a 'src=/etc/passwd dest=/tmp/ mode=755 owner=root'
示例-Fetch:从远程主机提取文件至主控端, copy相反,目前不支持目录
ansible srv -m fetch -a ‘src=/root/a.sh dest=/data/scripts’
unarchive:解包解压缩,有两种用法 :

1、将ansible主机上的压缩包在本地解压缩后传到远程主机上, 设置copy=yes.

2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no

ansible srv -m unarchive -a 'src=foo.tgz dest=/var/lib/foo'
ansible srv -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible srv -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
示例-Archive:打包压缩
ansible all -m archive -a 'path=/etc/sysconfig dest=/data/sysconfig.tar.bz2 format=bz2 owner=wang mode=0777'
示例-Hostname:管理主机名
ansible node1 -m hostname -a “name=websrv”
示例-content:content表示文件内容,desc表示目标文件,owner指定拥有者,backup=yes开启备份
ansible -k all -m copy -a 'content="hello world" dest=/tmp/jay.txt mode=755 owner=root'

ansible -k all -m copy -a 'content="hellow world" dest=/tmp/jay.txt backup=yes mode=755 owner=root'
示例-YUM:name表示安装的软件名,state表示状态,常见state=installed安装、absent卸载
ansible web -k -m yum -a "name=tree state=installed"

ansible all -k -m yum -a "name=tree state=absent"

ansible all -k -m yum -a "name=tree state=installed disable_gpg_check=no"
示例-file:path表示目录的名称或路径 ,state=directory表示创建目录
ansible -k 172.24.77.* -m file -a "path=/tmp/`date +%F` state=directory mode=755"

ansible -k 172.24.77.* -m file -a "path=/tmp/jay.txt state=touch mode=755"
示例-fuser:name表示用户名称,Home表示其家目录,指定其shell
ansible -k 172.24.77.* -m user -a "name=jay home=/tmp/"

ansible -k 172.24.77.* -m user -a "name=jay home=/tmp/ shell=/sbin/nologin"

ansible -k 172.24.77.* -m user -a "name=jay state=absent force=yes"
示例-cron:基于cron模块,创建crontab任务计划,backup=yes表示开启备份,备份文件存放于客户端/tmp/
ansible web -k -m yum -a "name=ntpdate state=installed"

ansible -k web -m cron -a "minute=1 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' job='/usr/sbin/ntpdate 139.224.227.121'"

ansible -k web -m cron -a "name='Ntpdate server for sync time' state=absent "

ansible -k web -m cron -a "minute=1 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='/usr/sbin/ntpdate pool.ntp.org'"
示例-synchronize:
ansible web -k -m yum -a "name=rsync state=installed"

ansible -k web -m synchronize -a 'src=/tmp/ dest=/tmp/'

ansible -k all -m synchronize -a 'src=/tmp/ dest=/tmp/' compress=yes delete=yes rsync_opts=--no-motd,--exclude=.txt'
示例-shell:-m shell指定模块为shell,远程执行shell脚本,远程执行脚本也可用采用script模块,并把执行结果追加至客户服务器/tmp/var.lgo文件,warn屏蔽警告信息
ansible -k all -m shell -a "/bin/sh /tmp/test.sh>>/tmp/var.log"

ansible -k all -m shell -a "mkdir -p 'date + %F' chdir=/tmp/ state=directory warn=no"

ansible -k all -m shell -a "ps -ef|grep httpd"

ansible -k all -m shell -a "crontab -l"
示例-service:远程重启httpd服务
ansible -k all -m service -a "name=httpd state=restarted"

ansible -k all -m service -a "name=network args=eth0 state=restarted"

ansible -k all -m service -a "name=nfs enabled=yes runlevel=3,5"
PlayBook远程主机安装Nginx Web服务,PlayBook代码如
cd /etc/ansible/
vim install-nginx.yml
-hosts:all
remote_user:root
tasks:
-name:install-nginx-devel
yum:name=pcre-devel,pcre,zlib-devel state=installed
-name:install-nginx-process
shell:cd /usr/local/src;wget http://nginx.org/download/nginx-1.16.1.tar.gz;tar -xzf nginx-1.16.1.tar.gz;cd nginx-1.16.1;./configure --prefix=/usr/local/nginx
;make;make install
添加执行权限
chmod +x install-nginx.yml
开始执行
./install-nginx.yml
List:列表,其所有元素均使用“-” 打头

Dictionary:字典,通常由多个key与value构成

name: Example Developer
job: Developer
skill: Elite
也可以将key:value放置于{}中进行表示,用,分隔多个key:value
{name: Example Developer, job: Developer, skill: Elite}
YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构( Structure)通过空格来展示,序列( Sequence)里的项用"-"来代表, Map里的键值对用":"分隔
name: John Smith
age: 41
gender: Male
spouse:
name: Jane Smith
age: 37
gender: Female
children:
- name: Jimmy Smith
age: 17
gender: Male
- name: Jenny Smith
age 13
gender: Female

标签:tmp,name,示例,dest,ansible,state,Ansible
From: https://blog.51cto.com/u_13688209/6081016

相关文章

  • A-Ansible-ssh配置
    SSH密钥配置yum-yinstallopenssh-clientsssh-keygen-trsaGeneratingpublic/privatersakeypair.Enterfileinwhichtosavethekey(/root/.ssh/id_rsa):##回车......
  • A-Ansible参数
    ansible命令执行过程加载自己的配置文件默认/etc/ansible/ansible.cfg加载自己对应的模块文件,如command通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程......
  • A-Ansible-Playbook参数
    Playbook核心元素Hosts#执行的远程主机列表Tasks#任务集Variables#内置变量或自定义变量在playbook中调用Templates#模板,可替换模板文件中的变量并实现一些简单逻辑......
  • C#websocket及时通信协议的实现方法示例
    传统“长轮询”实现Web端即时通讯的问题WebSocket出现之前,Web端为了实现即时通讯,所用的技术都是Ajax轮询(polling)。轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发......
  • Ansible 多机自动化工具 初学笔记
    此文档仅张聪明同学个人笔记;新负责KTH-RPLClusterGPU的漫漫学习长路EnglishDocs:https://docs.ansible.com/ansible/latest/index.htmlhttps://gits-15.sys.kth.se/......
  • Ansible 快速入门到放弃
    Ansible快速入门到放弃最是人间留不住,朱颜辞镜花辞树。1-Ansible简介Ansible是一个配置管理和配置工具,它使用SSH连接到服务器并运行配置好的任务,服务器上只......
  • ansible中map
    一、简介ansible中的filter:map,其实是jinja2中的filterpython中map(func,iter)返回func与每个元素计算后的迭代器,iter是个可迭代对象ansible中的map和python中的基......
  • 前端分页代码示例
    teacher.jsimportrequestfrom'@/utils/request'exportdefault{//1讲师列表(条件查询)getTeacherList(query){returnrequest({ur......
  • hadoop - hadoop2.6 伪分布式 - eclipse 中 配置 和 示例 wordcount
    1.配置eclipse   1.1下载linux版的eclipse ​​百度云-大数据资料专辑​​  1.2解压   可以手动提取,也可以命令提取:yuan@LABELNET:~/JAVA$sudotar-zx......
  • ansible playbook role及调试
    目录ansibleplaybookrole及调试role简介创建role步骤role中变量设置使用default定义变量使用命令行的变量在palybbok.yml文件直接定义变量在var中定义自己的变量pre_tas......