自定义ansible 配置文件:
[root@ansible ~]# mkdir /var/ooxx
[root@ansible ~]# cd /var/ooxx/
[root@ansible ooxx]# vim ansible.cfg
[defaults]
inventory = myhosts //指定文件
host_key_checking = False
:wq
[root@ansible ooxx]# vim myhosts
[app1]
web1
db1
[app2]
web2
db2
[app]
cache
:wq
检测:
[root@ansible ooxx]# ansible app1 --list-hosts
hosts (2):
web1
db1
[root@ansible ooxx]# ansible app --list-hosts
hosts (1):
cache
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
动态主机:
是通过脚本获取:(脚本要求json格式)
json 意思是JavaScript对象表示法,它是一种基亍文本,独立于语言的轻量级数据交换格式。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ansible 批量执行:
– host-pattern 主机戒定义的分组
– -M 指定模块路径
– -m 使用模块,默认 command 模块
– -a or --args 模块参数
– -i inventory 文件路径,戒可执行脚本
– -k 使用交亏式登彔密码
– -e 定义变量
– -v 详绅信息,-vvvv 开吭 debug 模式
ansible 批量执行命令:(需要输入密码)
[root@ansible ~]# ansible all -m command -a 'uptime' -k
[root@ansible ~]# ansible all -m command -a 'free -m' -k
[root@ansible ~]# ansible all -m command -a 'id' -k
[root@ansible ~]# ansible all -m command -a 'df -h' -k
ansible 批量执行命令:(免密执行)
密码写入配置文件会不安全,可以使用公钥技术。
[root@ansible .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@ansible .ssh]# pwd
/root/.ssh
[root@ansible .ssh]# key=$(cat id_rsa.pub)
[root@ansible .ssh]# echo $key
[root@ansible .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k
SSH password:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
测试:
[root@ansible .ssh]# ansible all -m ping
[root@ansible ~]# ansible all -m command -a 'free -m' -k