小炭猪

一夕一绽一缕芳,一生一叹一痕沙

0%

Ansible常用命令

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

多进程并发执行

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

用户名密码

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>"'