1.构建ansible清单
[devops@bastion ansible]$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
一个是主配置文件,一个是清单
[root@bastion ~]# vim /etc/ansible/hosts
[root@bastion ~]# ansible all --list-hosts
hosts (2):
172.25.250.10
172.25.250.11
列出清单
[root@bastion ~]# ansible qige --list-hosts
hosts (2):
172.25.250.12
172.25.250.13
查看清单可以单独查看也可以查看所有
子清单的表示方法(嵌套清单)
[root@bastion ~]# ansible dad --list-hosts
hosts (4):
172.25.250.10
172.25.250.11
172.25.250.12
172.25.250.13
会包含所有组的清单
2.主机的规范化表示
[start:end]
[root@bastion ~]# ansible haoge --list
hosts (5):
172.25.250.10
172.25.250.11
172.25.250.12
172.25.250.13
172.25.250.14
(2)指定读取清单位置
[root@bastion ~]# vim /mnt/inventory
[root@bastion ~]# ansible -i /mnt/inventory all --list
[root@bastion ~]# ansible -i /mnt/inventory all --list
hosts (10):
server1.haoge.org
server2.haoge.org
server3.haoge.org
server4.haoge.org
server5.haoge.org
server6.haoge.org
server7.haoge.org
server8.haoge.org
server9.haoge.org
server10.haoge.org
[root@bastion ~]# ansible -i /mnt/inventory ungrouped --list
列出不在组中的清单
- 全部
: 或者
:& 与,并且
:! 非
2.配置中常用参数
ansible 清单中组名称 -m 模块 -u remote_user
在配置文件中
```bash
[root@bastion ~]# vim /etc/ansible/ansible.cfg
第一次检测时不需要输入密码
[root@bastion ~]# ansible haoge -m ping -k
SSH password:
需要输入密码
[root@bastion ~]# ansible haoge -m shell -a 'hostname'
172.25.250.12 | CHANGED | rc=0 >>
serverc.exam.com
172.25.250.13 | CHANGED | rc=0 >>
serverd.exam.com
172.25.250.11 | CHANGED | rc=0 >>
serverb.exam.com
172.25.250.10 | CHANGED | rc=0 >>
servera.exam.com
-a表示shell模块的参数
[root@bastion ~]# ansible haoge -m shell -a 'useradd haha'
172.25.250.13 | CHANGED | rc=0 >>
172.25.250.11 | CHANGED | rc=0 >>
172.25.250.12 | CHANGED | rc=0 >>
172.25.250.10 | CHANGED | rc=0 >>
haoge组的成员都新建一个用户haha
配置文件中的默认参数
ansible运行方式是把本地命令转换成python脚本上传到远程主机再执行最后删除
remote_user =
指定远程登录的用户身份
身份信息设定
连接后是否自动切换用户
设定切换用户的方式,通常是sudo
在受控主机中要切换到的用户
是否需要为become_method提示输入密码,默认为false
3.私有配置方法
(1)ssh的免密
首先主机给受控主机ssh密钥,详见ssh部分
然后在受控机上输入visudo,在100行左右更改
这样每台受控机都不需要密码
(2)个性化配置
每个人都给自己的ansible配置文件写,不然都写在一起参数会很混乱,无法使用
[root@bastion ~]# cp /etc/ansible/ansible.cfg /home/devops/ansible/ansible.cfg
把ansible主配置文件复制到一个属于自己的文件,一般保留以下内容就够用了。
可以自己配置仓库和路径等等,也可以指定自己的用户
[root@bastion ~]# touch /home/devops/ansible/inventory
自己创建一个用户仓库,把路径写到刚才自己的配置文件中
[root@bastion ~]# mkdir /home/devops/ansible/roles/
自己创建一个roles目录
[root@bastion ~]# cat /home/devops/ansible/inventory
[dev]
172.25.250.9
[test]
172.25.250.10
[prod]
172.25.250.11
172.25.250.12
[balancers]
172.25.250.13
[webservers:children]
prod
自己在自己的用户仓库写以上
这样的话,配置文件,用户仓库,roles都是自己的
但是必须要所在配置文件所在的目录中才可以
[root@bastion ~]# ansible all -m ping
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
这是不在目录中的结果
[root@bastion devops]# cd /home/devops/ansible/
[root@bastion ansible]# ansible all -m ping
172.25.250.13 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
切换到配置文件所在的目录中就可以检测到私有化的配置文件和用户仓库