Ansible常用命令

基础配置

/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]

# some basic default values...

## 主机清单配置文件
#inventory = /etc/ansible/hosts

##库文件存放目录
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/

## 临时py命令文件存放在远程主机目录
#remote_tmp = ~/.ansible/tmp

## 本机临时命令执行目录
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C

## 检查对应服务器的host_key,建议取消注释
#module_set_locale = False
log_path = /var/log/ansible.log

## 默认模块,可以修改shell模块
#module_name = command

/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 "shell命令" -u username --sudo [--ask-sudo-pass]
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" # chmod chown
ansible web_server -m file -a "dest=/tmp/test mode=755 owner=xuad group=xuad state=directory" # mkdir -p /tmp/test
ansible web_server -m file -a "dest=/tmp/test.txt mode=755 owner=xuad group=xuad state=touch" # touch /tmp/test.txt
ansible web_server -m file -a "src=/root/test.txt dest=/tmp/test.txt state=link" # ln -s
ansible web_server -m file -a "dest=/tmp/test.txt state=absent" # rm -rf

文件编辑

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 # remove empty lines
ansible doris_all -m lineinfile -a "dest=/etc/fstab regexp='^(/swap.img.*)' line='# \1' backrefs=yes state=present" -b --become-method sudo -K # Comment out one line.

软件安装

yum系列安装(federal、centos、redhat)

1
2
3
4
5
ansible web_server -m yum -a "name=wget state=present" # check if exist
ansible web_server -m yum -a "name=wget state=latest" # upgrade
ansible web_server -m yum -a "name=wget state=removed" # remove
ansible web_server -m yum -a "name=wget state=absent" # check if nonexist
ansible web_server -m yum -a "name=wget state=installed" # install

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" # remove user
ansible web_server -m user -a "name=andy state=present" # create on absence
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