ansible基础元素
一.模块安装 #服务端 yum install ansible #客户端 yum install -y libselinux-python (被监控的机器可安装) ? 二. 常用模块 command 不指定模块默认command模块 #chdir---在执行这个命令前,先切换目录 [[email?protected] ansible]# ansible 172.16.1.31 -m command -a "chdir=/etc/ pwd" 172.16.1.31 | SUCCESS | rc=0 >> /etc #creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行 [[email?protected] ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf hostname" 172.16.1.41 | SUCCESS | rc=0 >> skipped,since /etc/rsyncd.conf exists [[email?protected] ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.123456 hostname" 172.16.1.41 | SUCCESS | rc=0 >> backup #参数:removes---判断一个文件是否存在,如果不存在,后面的命令就不会执行 [[email?protected] ansible]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.conf hostname" 172.16.1.41 | SUCCESS | rc=0 >> backup [[email?protected] ansible]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.1212213123 hostname" 172.16.1.41 | SUCCESS | rc=0 >> skipped,since /etc/rsyncd.1212213123 does not exist #参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息 ansible 172.16.1.41 -m command -a "ls" 172.16.1.41 | SUCCESS | rc=0 >> anaconda-ks.cfg dead.letter ? shell模块(万能模块) [[email?protected] ansible]# ansible 172.16.1.41 -m shell -a "ls;pwd" 172.16.1.41 | SUCCESS | rc=0 >> anaconda-ks.cfg ?
? ? 文件类型模块: [[email?protected] ansible]# ansible 172.16.1.41 -m copy -a "src=/tmp/file01.txt dest=/tmp/ backup=yes" 172.16.1.41 | SUCCESS => { "backup_file": "/tmp/[email?protected]:33:19~","changed": true,"checksum": "029b054db136cc36d5605e3818305825ff4b8ffb","dest": "/tmp/file01.txt","gid": 0,"group": "root","md5sum": "434660b5ad7deeba8815349f71409405","mode": "0644","owner": "root","size": 6,"src": "/root/.ansible/tmp/ansible-tmp-1522683197.05-52744169892601/source","state": "file","uid": 0 } ? #参数:src---定义要推送数据信息 #参数:dest---定义将数据推送到远程主机什么目录中 [[email?protected] ansible]# touch /tmp/file01.txt [[email?protected] ansible]# ansible 172.16.1.41 -m copy -a "src=/tmp/file01.txt dest=/tmp/" 172.16.1.41 | SUCCESS => { "changed": true,"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709","md5sum": "d41d8cd98f00b204e9800998ecf8427e","size": 0,"src": "/root/.ansible/tmp/ansible-tmp-1522682948.27-60532389065095/source","uid": 0 } [[email?protected] ansible]# ansible 172.16.1.41 -m shell -a "ls -l /tmp/" 172.16.1.41 | SUCCESS | rc=0 >> total 24 -rw-r--r-- 1 root root 0 Apr 2 23:29 file01.txt ? file----文件属性修改/目录创建/文件创建 ansible 172.16.1.41 -m file -a "dest=/tmp/file01.txt owner=oldboy group=oldboy mode=600" 172.16.1.41 | SUCCESS => { "changed": true,"gid": 500,"group": "oldboy","mode": "0600","owner": "oldboy","path": "/tmp/file01.txt","uid": 500 } 参数:state---用于指定创建目录或文件 创建文件 ansible 172.16.1.41 -m file -a "dest=/tmp/file01.txt state=touch" 172.16.1.41 | SUCCESS => { "changed": true,"uid": 0 } 创建目录: ansible 172.16.1.41 -m file -a "dest=/tmp/dir01 state=directory" 172.16.1.41 | SUCCESS => { "changed": true,"mode": "0755","path": "/tmp/dir01","size": 4096,"state": "directory","uid": 0 } ? ? 包管理模块类型 #name:执行要安装软件的名称,以及软件的版本 #state:installed安装 absent(卸载) ansible 172.16.1.41 -m yum -a "name=iftop state=installed" ansible 172.16.1.41 -m yum -a "name=iftop state=absent" list:指定软件名称,查看软件是否可以安装,以及是否已经安装过了 ansible 172.16.1.41 -m yum -a "list=iftop" ? ? 系统模块类型 ansible 172.16.1.41 -m service -a "name=crond state=started enabled=yes" ? cron---定时任务模块 * * * * * /bin/sh /server/scripts/test.sh &>/dev/null minute=0-59 * */n,- hour day month weekday job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘ ? 添加定时任务 ansible 172.16.1.41 -m cron -a "minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘" ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘" ? 删除定时任务 ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘ state=absent" ansible 172.16.1.41 -m cron -a "name=oldboy01 state=absent" ? 注释定时任务 ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘ disabled=yes" ansible 172.16.1.41 -m cron -a "name=oldboy01 job=‘/bin/sh /server/scripts/test.sh &>/dev/null‘ disabled=no" ? day hour minute month weekday name:任务名 special_time :指定什么时候执行 state:确认任务计划是创建还是删除 absent删除 user 哪个用户
? ? 总结ansible颜色信息: ? [[email?protected] ~]# ansible -i /etc/ansible/h test -m ping 10.0.0.5 | SUCCESS => { "changed": false,"ping": "pong" } 10.0.0.6 | SUCCESS => { "changed": false,"ping": "pong" } ? ansible_ssh_user #指定用户
[[email?protected] ~]# cat /etc/ansible/hosts [test] lb1 ansible_ssh_host=10.0.0.5 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 [[email?protected] ~]# ansible test -m ping lb1 | SUCCESS => { "changed": false,"ping": "pong" } [[email?protected] ~]# cat /etc/ansible/hosts [lb] lb1 ansible_ssh_host=172.16.1.5 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 lb2 ansible_ssh_host=172.16.1.6 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 [web] web1 ansible_ssh_host=172.16.1.7 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 web2 ansible_ssh_host=172.16.1.8 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 web3 ansible_ssh_host=172.16.1.9 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22 [[email?protected] ~]# ansible lb:web -m ping lb1 | SUCCESS => { "changed": false,"ping": "pong" } web2 | SUCCESS => { "changed": false,"ping": "pong" } web1 | SUCCESS => { "changed": false,"ping": "pong" } web3 | SUCCESS => { "changed": false,"ping": "pong" } lb2 | SUCCESS => { "changed": false,"ping": "pong" } ?
ansible lb -f 2 -m ping ansible lb -f 2 -m ping -vvv ?
[[email?protected] ~]# ansible lb --list hosts (2): lb1 lb2 [[email?protected] ~]# ansible lb --list-hosts hosts (2): lb1 lb2 ? ansible lb -a "df " ansible lb -m command -a "df " ? -f #并发数是cpu的偶数倍 2核 10个线程 #安装 ansible lb -m yum -a "name=redhat-lsb state=present" ansible lb -m yum -a "name=redhat-lsb state=installed" ? #安装最新版 ansible lb -m yum -a "name=redhat-lsb state=latest" ? #卸载 ansible lb -m yum -a "name=redhat-lsb state=absent" ansible lb -m yum -a "name=redhat-lsb state=removed" ? - state
Whether to install (`present‘ or `installed‘,`latest‘),or remove
(`absent‘ or `removed‘) a package.
`present‘ and `installed‘ will simply ensure that a desired package is
installed.
`latest‘ will update the specified package if it‘s not of the latest
available version.
`absent‘ and `removed‘ will remove the specified package.
(Choices: absent,installed,latest,present,removed)[Default: present]
? #查看系统版本号 [[email?protected] ~]# ansible lb -m command -a ‘lsb_release -a‘ lb2 | SUCCESS | rc=0 >> LSB Version::base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID:CentOS Description:CentOS release 6.5 (Final) Release: 6.5 Codename:Final ? #安装 nginx [[email?protected] ~]# ansible lb -m yum -a "name=nginx state=present" [[email?protected] ~]# ansible lb -m shell -a "chkconfig --list nginx" lb2 | SUCCESS | rc=0 >> nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off ? #启动nginx 并设置为开机启动 [[email?protected] ~]# ansible lb -m service -a "name=nginx state=started enabled=yes" lb1 | SUCCESS => { "changed": true,"enabled": true,"name": "nginx","state": "started" } [[email?protected] ~]# ansible lb -m shell -a "/etc/init.d/nginx status" lb1 | SUCCESS | rc=0 >> nginx (pid 30057) is running... ? #检查是否开机自启动 [[email?protected] ~]# ansible lb -a "chkconfig --list nginx" lb1 | SUCCESS | rc=0 >> nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |