以下为markdown格式:
# 客户端
# 1.准备工作
## 1.1安装openssh-server和python
`apt install openssh-server pytnon`
## 1.2允许root登录
修改 /etc/ssh/sshd_config 中 PermitRootLogin 的值为yes
默认值为 prohibit-password
## 1.3重启openssh服务
`systemctl restart ssh.service`
# 管理端
# 1安装及配置ansible
## 1.1生成密钥对
使用root登录后执行 ssh-keygen 生成密钥对
## 1.2对客户端加入公钥
`ssh-copy-id -i .ssh/id_rsa.pub root@192.168.13.71`
## 1.3安装ansible
`apt install ansible`
## 1.4配置ansible
编辑 /etc/ansible/ansible.cfg
# 2使用ansible命令
## 2.1配置inventory文件
编辑inventory文件hosts
/etc/ansible/hosts
[mysrv]
192.168.13.71
## 2.2执行命令
ping hosts文件中全部的主机,-o为输出结果为1行
`ansible all -m ping -o`
复制test.txt文件到客户端
文件模式600,所属用户为user1,所属组为user1group
可以用file模块替代copy模块
`ansible mysrv -m copy -a 'src=test.txt dest=~/test.txt mode=600 owner=user1 group=user1group' -o`
利用username的用户去对mysrv组中的电脑进行重启
参数-f 10为fork出10个子进程(每次执行10台电脑)
`ansible mysrv -a "/sbin/reboot" -u username --sudo --ask-sudo-pass -f 10`
使用user模块可以方便的创建账户,删除账户,或是管理现有的账户:
`ansible all -m user -a "name=foo password=<crypted password here>"`
`ansible all -m user -a "name=foo state=absent"`
利用apt安装nginx,需输入密码
`ansible one -m apt -a "name=nginx state=present" --sudo --ask-sudo-pass`
利用apt卸载nginx,需输入密码
`ansible one -m apt -a "name=nginx state=absent" --sudo --ask-sudo-pass`
# 3使用playbook
## 3.1检测语法
`ansible-playbook --syntax-check the.yml`
## 3.2测试运行
`ansible-playbook -C the.yml`
## 3.3创建用户的例子
createuser.yml
```
---
- hosts: one
vars:
username: "testuser"
user: root
tasks:
- name: create user
user: name="{{ username }}"
```
执行playbook
`ansible-playbook createuser.yml`
## 3.3安装nginx的例子
nginx.yml
```
---
- hosts: one
remote_user: root
tasks:
- name: ensure nginx is at the latest version
apt: name=nginx state=latest
- name: ensure nginx is running
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
```
## 3.4状态类型
安装软件:state={present|latest}
卸载软件:state={absent}
服务状态:state={started|stoped|restarted}