ansible 命令行 (ad hoc)简洁运维
2021-11-23 本文已影响0人
偷油考拉
Introduction to ad hoc commands — Ansible Documentation
以sudo执行
[sysadmin@VM_201_11_centos ansible]$ ansible logsvr -m command -a 'id' -u sysadmin -k -b -K
SSH password:
SUDO password[defaults to SSH password]:
10.98.201.18 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)
10.98.201.17 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)
创建 user,设置密码
ansible sqoop -m user -a "name=hdfs shell=/bin/bash uid=2000"
ansible sqoop -m shell -a "echo "hdfs" | passwd --stdin hdfs"
ansible logsvr -m user -a "name=audit shell=/bin/bash uid=2001" -u sysadmin -k -b -K
ansible logsvr -m user -a "name=secure shell=/bin/bash uid=2002" -u sysadmin -k -b -K
ansible logsvr -m group -a "name=guest gid=3000 state=present" -u sysadmin -k -b -K
ansible logsvr -m user -a "name=yinwenbin shell=/bin/bash uid=3001 group=guest" -u sysadmin -k -b -K
ansible logsvr -m user -a "name=gengfeng shell=/bin/bash uid=3002 group=guest" -u sysadmin -k -b -K
ansible logsvr -m shell -a "echo "Audit" | passwd --stdin audit" -u sysadmin -k -b -K
ansible logsvr -m shell -a "echo "Secure" | passwd --stdin secure" -u sysadmin -k -b -K
拷贝文件
ansible sqoop -m copy -a "src=/root/hadoop/hadoop-my.tar.gz dest=/opt/hadoop-my.tar.gz"
ansible sqoop -m copy -a "src=/root/hadoop/profile.d/sqoop.sh dest=/etc/profile.d/sqoop.sh mode=0644"
解压文件
ansible sqoop -m unarchive -a "src=/opt/hadoop-my.tar.gz dest=/opt owner=sqoop group=sqoop remote_src=yes"
修改文件属性
ansible hive -m file -a "path=/opt/hadoop-2.8.5/ state=directory owner=hdfs group=hdfs recurse=yes"
ansible nn -m file -a "path=/nfsmount/dfs/nn state=directory owner=hdfs mode=0755"
配置service
ansible all -m service -a "name=chronyd state=restarted enabled=yes"
结果输出精简显示,一个主机一行
ansible UAT -m command -a 'id admin' -u root -k -o
结果输出到文件
#输出到一个文件
ansible UAT -m command -a 'id admin' -u root -k -o > uat.txt
#输出到目录,每个主机一个文件
ansible UAT -m command -a 'id admin' -u root -k -o -t ./
shell 安装jdk
ansible sitms -m shell -a 'curl http://118.25.164.69/hfs/install/jdk/install_jdk11.sh |sh' -u root -k
ansible sitms -m shell -a 'source /etc/profile && java -version' -u root -k