[root@centos7 ~]# yum install -y ansible 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.bfsu.edu.cn * extras: mirrors.bfsu.edu.cn * updates: mirrors.neusoft.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 updates/7/x86_64/primary_db | 13 MB 00:00:10 没有可用软件包 ansible。 错误:无须任何处理
需要先安装
[root@centos7 ~]# yum install -y epel-release [root@centos7 ~]# yum install -y ansible配置机器信息
- root账号登录ansible机器,创建文件夹playbooks;playbooks目录下创建名为hosts的文件,内容如下,cdh-group是群组名,该群组内有一个机器配置信息,包含名称、IP地址,SSH端口,SSH账号密码等:
[root@centos7 playbooks]# pwd /root/playbooks [root@centos7 playbooks]# cat hosts [k8s-group] master0 ansible_host=172.70.10.161 ansible_port=22 ansible_user=root ansible_password=****** master1 ansible_host=172.70.10.162 ansible_port=22 ansible_user=root ansible_password=****** node1 ansible_host=172.70.10.163 ansible_port=22 ansible_user=root ansible_password=****** node2 ansible_host=172.70.10.164 ansible_port=22 ansible_user=root ansible_password=****** node3 ansible_host=172.70.10.165 ansible_port=22 ansible_user=root ansible_password=****** node4 ansible_host=172.70.10.166 ansible_port=22 ansible_user=root ansible_password=******
- laybooks目录下创建名为ansible.cfg的文件,内容如下,这是个ansible的配置文件,执行ansible命令时用到,这里面指定了主机信息在hosts文件中查找:
[root@centos7 playbooks]# cat ansible.cfg [defaults] inventory = ~/playbooks/hosts host_key_checking = False forks = 6体验
[root@centos7 playbooks]# ansible -i /root/playbooks/hosts all -m shell -a "free -g"
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
master0 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
master1 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
node2 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
node4 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
node3 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
node1 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 15 0 12 0 2 14
Swap: 7 0 7
体验playbook
(base) [root@node1 playbooks]# cat install_docker.yaml
- name: install docker
hosts: k8s-group
gather_facts: True
tasks:
- name: add repo
shell: cd ~ && mkdir tmp && cd ~/tmp && wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && mv docker-ce.repo /etc/yum.repos.d/
- name: install docker
yum: name=docker-ce
- name: add systemctl
shell: systemctl start docker && systemctl enable docker
执行:
(base) [root@node1 playbooks]# ansible-playbook install_docker.yaml [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details PLAY [install k8s] **************************************************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************************************************************ ok: [node4] ok: [node2] ok: [node1] TASK [add repo] ******************************************************************************************************************************************************************************************* changed: [node4] changed: [node2] changed: [node1] TASK [install docker] ************************************************************************************************************************************************************************************* ok: [node4] ok: [node2] ok: [node1] TASK [add systemctl] ************************************************************************************************************************************************************************************** changed: [node2] changed: [node4] changed: [node1] PLAY RECAP ************************************************************************************************************************************************************************************************ node1 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 node2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 node4 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
备注:
# ansible-playbook a.yml --syntax-check #检查yaml文件的语法是否正确 # ansible-playbook a.yml --list-task #检查tasks任务 # ansible-playbook a.yml --list-hosts #检查生效的主机 # ansible-playbook a.yml --start-at-task='Copy Nginx.conf' #指定从某个task开始运行



