Ansible学习笔记
Ansible 1、安装: yum install -y ansible 2、使用: cd /etc/ansible/ vim hosts ansible all -m ping ansible all -a "date" -f 并行默认5, -m 模块默认命令, -a 命令 ansible all -m command -a "service httpd status" 3、ansible-doc -l 将本地文件批量上传到服务器 ansible-doc -s copy ansible dbservers -m copy -a "src=/root/tmp_test_20180207.txt dest=/tmp/" #-f强制可替换已存在文件 报错: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!" ansible dbservers -m command -a "rpm -q |grep libselinux-python" ansible dbservers -m command -a "yum install -y libselinux-python" 再次执行成功 ansible dbservers -m command -a "getenforce" #是enforce,需要关闭selinux ansible dbservers -m command -a "ls -l /tmp/tmp_test_20180207.txt" 4、ansible all -m cron -a 'name="custom job" minute=*/5 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 210.72.145.44"' 5、开机自动启动: ansible all -a "chkconfig --list iptables" ansible all -a "service iptables status" 6、YAML [root@db10 ~]# cat test.yaml - hosts: all remote_user: root tasks: - name: add a group group: gid=1000 name=testgroup system=no - name: excute a command command: /bin/date 7、上传并执行脚本的两条语句 ansible all -m copy -a "src=/root/test.sh dest=/tmp/ owner=root group=root mode=0755" ansible all -m shell -a "sh /tmp/test.sh" ######脚本中的命令一定要写全路径 -m 常用模块 command copy shell service cron file blockinfile 8、查看启动的端口: ansible all -a "ss -tnl"