基础配置 /etc/ansible/ansible.cfg 更多常用配置文件参考:https://segmentfault.com/a/1190000020522428
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [defaults] log_path = /var/log/ansible.log
/etc/ansible/hosts 1 2 3 4 5 6 7 8 9 10 11 12 [base] ubuntu-base [doris_all:children] doris_fe doris_be [doris_fe] doris-fe [doris_be] doris-be-[01:03]
Host匹配方式 1 2 3 4 5 web_server:!http1:&http2 http*:web_server web_server[0] web_server[0:3] '~(web|http)*'
常用命令 用户名密码验证方式 1 2 3 4 ansible web_server -a "shell命令" -k ansible web_server -a "shell命令" -u username -k ansible web_server -a "apt-get -y update" --become --become-method sudo -K
多进程并发执行 1 ansible web_server -a "/sbin/reboot" -f 10
Ansible模块 Shell操作 1 2 ansible web_server -m shell -a "grep '/sbin/nologin' /etc/passwd | wc -l" ansible web_server -m shell -a 'echo $PATH'
文件操作 1 2 3 4 5 6 ansible web_server -m copy -a "src=/etc/hosts dest=/tmp/" ansible web_server -m file -a "dest=/tmp/hosts mode=600 owner=xuad group=xuad" ansible web_server -m file -a "dest=/tmp/test mode=755 owner=xuad group=xuad state=directory" ansible web_server -m file -a "dest=/tmp/test.txt mode=755 owner=xuad group=xuad state=touch" ansible web_server -m file -a "src=/root/test.txt dest=/tmp/test.txt state=link" ansible web_server -m file -a "dest=/tmp/test.txt state=absent"
文件编辑 1 2 3 4 ansible web_server -m lineinfile =a "path=/etc/hosts line='0.0.0.0 facebook.com'" -b --become-method sudo -K ansible doris_all -m lineinfile -a "path=/etc/hosts line='' insertafter=.*ip6-allrouters" -b --become-method sudo -K ansible doris_all -m lineinfile -a "path=/etc/hosts state=absent regex=^$" -b --become-method sudo -K ansible doris_all -m lineinfile -a "dest=/etc/fstab regexp='^(/swap.img.*)' line='# \1' backrefs=yes state=present" -b --become-method sudo -K
软件安装 yum系列安装(federal、centos、redhat)
1 2 3 4 5 ansible web_server -m yum -a "name=wget state=present" ansible web_server -m yum -a "name=wget state=latest" ansible web_server -m yum -a "name=wget state=removed" ansible web_server -m yum -a "name=wget state=absent" ansible web_server -m yum -a "name=wget state=installed"
apt系列安装(debian、ubuntu)
1 2 3 ansible doris_all -m apt -a "name=openjdk-8-jdk update_cache=yes" -b --become-method sudo -K ansible doris_all -m apt -a "name=openjdk-11-jdk state=absent" -b --become-method sudo -K ansible doris_all -m apt -a "autoremove=yes" -b --become-method sudo -K
用户名密码 1 2 3 ansible web_server -m user -a "name=andy state=absent" ansible web_server -m user -a "name=andy state=present" ansible web_server -m user -a 'name=andy password="<crypted password here>"'
转载请注明出处: Ansible常用命令 原文地址: https://www.xiaotanzhu.com/%E8%BF%90%E7%BB%B4/ansible-commands.html