ansible安装和常用命令
安裝
brew安装
1 | # 安装 |
yum安装
1 | # 添加EPEL源 |
配置
1 | vi /etc/ansible/ansible.cfg |
1 | host_key_checking=False |
1 | # 配置的主机需要配置免密登录 |
1 | # 单个主机 |
ansible命令
ansible命令又被称为ad-hoc命令,他主要是用来快速在多个远程机器Hosts上执行单个任务(task)
列出所有主机
ansible all --list-hosts
ping
默认用户是当前机器的用户ansible all -m ping
指定用户ansible all -m ping -u root
执行shell指令
下面是执行了ls命令
ansible all -m shell -a ls
复制文件到主机
ansible -i /etc/ansible/hosts test -m copy -a "src=/tmp/ansible.tar.gz dest=/home/root/ owner=root group=root mode=0777"
解压文件
ansible -i test -m unarchive -a 'src=/etc/bin/ansible.tar.gz dest=/usr/local copy=no mode=0755'
ansible -i /etc/ansible/hosts test -m shell "tar -zxvf /etc/bin/ansible.tar.gz -C /usr/local"
批量杀掉进程
ansible test -m shell -a "ps -ef | grep zabbix |grep -v grep |awk '{print \$2}' | xargs kill -9"
设置cron
ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
建立组
ansible all -m group -a 'gid=2017 name=a'
建立用户
ansible all -m user -a 'name=aaa groups=aaa state=present'
删除用户
ansible all -m user -a 'name=aaa groups=aaa remove=yes'
在节点上安装httpd
ansible all -m yum -a "state=present name=httpd"
在节点上启动服务,并开机自启动
ansible all -m service -a 'name=httpd state=started enabled=yes'
执行远程命令
ansible all -m command -a 'uptime'
执行主控端脚本
ansible all -m script -a '/root/test.sh'
执行远程主机的脚本
ansible all -m shell -a 'ps aux|grep zabbix'
类似shell
ansible all -m raw -a "ps aux|grep zabbix|awk '{print $2}'"
创建软链接
ansible all -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
删除软链接
ansible all -m file -a "path=/tmp/resolv.conf state=absent"
复制文件到远程服务器
ansible all -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
在节点上运行hostname
ansible all -m raw -a 'hostname|tee'
将指定url上的文件下载到/tmp下
ansible all -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
帮助
ansible --help
ansible-playbook命令
ansible-playbook主要用来执行ansible的playbook。他可以用来在多个远程机器Hosts上执行一个或多个任务(task),这个命令执行时可以指定一个或多个playbook的名字。
ansible-playbook playbook.yml [options]
1 | -u REMOTE_USER, --user=REMOTE_USER # ssh 连接的用户名 |
ansible-config
这个命令主要用来初始化或者查看当前机器ansible的配置信息
ansible-config [-h] [-version] [-v] {list,dump,view,init}
ansible-inventory
该命令主要用于显示或者导出ansible配置好的Inventory
ansible-inventory {--list/--host/--graph}
ansible-doc
该命令主要用于通过获取当前安装的plugin的一些api
ansible-doc [plugin [plugin ...]]