ansible常用命令

3年前 (2021-11-25) faer615 ansible, 自动化运维 已收录 490℃

点击数:154

ansible 主要参数
-u username 指定连接的用户名
-i inventory_file 指定使用inventory文件位置,默认/etc/ansible/hosts
-m module 指定使用的模块,默认为command,常见模块有command、shell、script、yum、copy等
-f 10 指定并发数,并发量大的时候提高该值
--sudo [-k] 需要root权限实行时使用 -k为输入密码
-a 指定模块的参数
 
01. 远程拷贝文件
ansible all -m copy -a 'src=/etc/bashrc dest=/root/bashrc mode=755 owner=root'
 
02. 拷贝目录
ansible all -m copy -a 'src=/root dest=/root mode=755 owner=root'
 
03. 安装软件包
ansible node1 -m yum -a 'name=sysstat,screen,ntpdate state=installed'
 
04. 查看默认支持模块
ansible all --connection=local -m setup | less
 
05. 语法检查
ansible-playbook --syntax-check flanneld.yaml
 
1.ansible-doc -s shell 帮助模块。
 
2.command:命令模块,默认模块。用 于执行远程命令。
ansible all -a 'date'
 
3.cron模块:部署状态(state):
present:安装
absent:移除
示例:
添加计划任务
ansible ks1 -m cron -a 'minute="*/10" job="/bin/echo ansible-working" name="Ansible test cron job state=present"'
 
移除计划任务:
ansible ks1 -m cron -a 'minute="*/10" job="/bin/echo ansible-working" name="Ansible test cron job" state=absent'
 
4.user模块:
ansible all -m user -a 'name="user1" state=persent'
 
5.group模块:
ansible all -m group -a 'name=mysql gid=306 system=yes'
ansible all -m user -a 'name=mysql uid=306 system=yes group=mysql'
 
6.copy模块:
ansible all -m copy -a 'src=/root/install.log dest=/tmp/install.log owner=root mode=644'
src=: 定义本地源文件路径,相对路径或者绝对路径
dest=: 定义远程目标文件路径,绝对路径
content:直接生成文件内容
ansible all -m copy -a 'content="Hello Ansible\nHi nihao" dest=/tmp/abc.txt'
 
7.file模块:设置文件的属性
path=: 指定文件路径,等同于 name或者dest
ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/install.log'
 
创建符号链接:
src=:源文件
path:符号链接文件路径
ansible all -m file -a 'path=/tmp/install.log src=/tmp/install.log state=link'
 
8.ping模块:测试连通性
ansible all -m ping
 
9.service模块:指定服务的运行状态,开机启动httpd服务
enabled=:是否是开机自动启动,true/false
name=:服务器名称
state=:状态,started,stopped,restarted
ansible all -m service -a 'enable=true name=htppd state=started'
 
10.shell模块:复杂命令时使用shell模块
 
11.script模块:远程执行本地脚本,将本地脚本复制到远程主机并执行。
ansible all -m script -a '/root/a.sh'
 
12.yum模块:程序包安装
name=: 指明安装程序包,可以带版本
state:present 安装,latest 最新版,absent 卸载。
ansible all -m yum -a 'name=nginx'
ansible all -m yum -a 'name=nginx state=absent'
 
13.setup:收集远程主机的facts
ansible all -m setup
被管理主机信息会反馈给管理主机。
 
14.过滤
 
我的需求是当某一个任务执行失败时,应该中断后面的任务,而不是跳过。这个需要结合fail模块来实现了。
我们将/tmp/test.sh脚本输出改回"failed"。删除/tmp下的fstab文件。修改register.yml文件
---
- hosts: test
  remote_user: root
  tasks:
    - command: /tmp/test.sh
      register: result
    - name: if stdout chekck failed,interrupt execution
      fail: msg="check failed"
      when: result.stdout == "failed"
    - file: path=/tmp/test  owner=root group=root mode=0755 state=directory
    - copy: src=/etc/fstab dest=/tmp/fstab owner=root group=root mode=0644
 
playbook忽略错误
ignore_errors: yes
 
过滤
ansible -m setup -a 'filter=ansible_hostname' my_host
 
批量替换字符
ansible abc -m lineinfile -a 'dest=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
 
显示主机列表
ansible all --list-host
 

faer615
博主

这货来去如风,什么鬼都没留下!!!

相关推荐

评论已关闭!