Char03 Ansible 组件介绍
3.1 Ansible Inventory
# cat ansible_hosts 127.0.0.1 [TestSlavesMonkey] TestSlave[5:6] ansible_ssh_user=jenkins root@Ly:~# ansible -i ansible_hosts TestSlavesMonkey -m shell -a 'echo hello ansible' TestSlave5 | success | rc=0 >> hello ansible TestSlave6 | success | rc=0 >> hello ansible
ANSIBLE_HOST
3 动态 Inventory
就是Ansible 所有的Inventory文件里面的主机列表和变量信息都从外部拉取 ,只需要将ansible.cfg 中的inventory的定义值改为1个执行脚本
#ansible -i ansible_host.py TestSlave6:TestSlave5 -u jenkins -m ping TestSlave5 | success >> { "changed": false, "ping": "pong" } TestSlave6 | success >> { "changed": false, "ping": "pong" }
3.2 Ansible Ad-Hoc 命令
3.2.1 执行命令
默认的并发数由ansible.cfg中的forks值来控制,也可以通过-f 执行并发数
3.2.2 复制文件
# ansible -i ansible_hosts TestSlavesMonkey -m copy -a "src=/tmp/test.txt dest=/tmp owner=jenkins group=jenkins mode=777 backup=yes" -o TestSlave5 | success >> {"changed": true, "checksum": "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", "dest": "/tmp/test.txt", "gid": 1000, "group": "jenkins", "md5sum": "d8e8fca2dc0f896fd7cb4cb0031ba249", "mode": "0777", "owner": "jenkins", "size": 5, "src": "/home/jenkins/.ansible/tmp/ansible-tmp-1484640059.23-92200316607657/source", "state": "file", "uid": 1000} TestSlave6 | success >> {"changed": true, "checksum": "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", "dest": "/tmp/test.txt", "gid": 1000, "group": "jenkins", "md5sum": "d8e8fca2dc0f896fd7cb4cb0031ba249", "mode": "0777", "owner": "jenkins", "size": 5, "src": "/home/jenkins/.ansible/tmp/ansible-tmp-1484640059.27-191166608385755/source", "state": "file", "uid": 1000}
# ansible -i ansible_hosts TestSlavesMonkey -m shell -a 'md5sum /tmp/test.txt' -f 5 -o TestSlave5 | success | rc=0 | (stdout) d8e8fca2dc0f896fd7cb4cb0031ba249 /tmp/test.txt TestSlave6 | success | rc=0 | (stdout) d8e8fca2dc0f896fd7cb4cb0031ba249 /tmp/test.txt
3.2.3 包和服务管理
3.2.4 用户管理
echo absible | openssl passwd -l stdin
ansbile docker -m user -a 'name=shencan password='XXX"' -f 5 - o
ss TestSlave4 -l shencan
3.3 Ansible playbook
3.4 Ansible facts
facts 组件是Ansible 用于采集被管机器设备信息的一个功能,可以使用setup 模块查机器的所有的facts信息,可以使用filter来查看指定的信息
facts 组件默认已经收集了很多的设备基础信息
# ansible -i ansible_hosts TestSlave5 -m setup -u jenkins -a 'filter=ansible_all_ipv4_addresses' TestSlave5 | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.17.150.21" ] }, "changed": false }
1 使用facter扩展facts信息
2 使用ohai 扩展facts信息
3.5 Ansible role
3.6 Ansible galaxy