A-Ansible示例
原创
©著作权归作者所有:来自51CTO博客作者Jaygan的原创作品,请联系作者获取转载授权,否则将追究法律责任
示例-pingansible -k all -m ping
ansible -k all -m command -a "ping -c 2 www.baidu.com"
示例-dateansible -k -i /etc/ansible/hosts all -m command -a "date"
示例-df-hansible -k 172.24.77.242 -m command -a "df -h"
示例-copyansible -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表示其家目录,指定其shellansible -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
开始执行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