准备工作
[root@wang yum]# ssh-copy-id root@192.168.170.137 #设置免密登入 [root@wang yum]# cat /etc/hosts #配置清单文件 192.168.170.134 apache 192.168.170.137 cos7 将ansible配置文件f复制到yum目录下 [root@wang yum]# cp /etc/lamp/inventory [root@wang yum]# cp /etc/ansible/ansible.cfg . 尝试能否ping通 [root@wang yum]# ansible cos7 -m ping cos7 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" 查看清单 [root@wang yum]# ansible all --list-hosts hosts (2): apache cos7
剧本内容
[root@wang yum]# cat yum.yml --- - hosts: all vars: baseurl: https://mirrors.aliyun.com/epel/8/Modular/x86_64/ major_version: ansible_facts["distribution_major_version"] tasks: - name: yum {{ major_version }} loop: - BaseOS - AppStream yum_repository: name: "{{ item }}" baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/ enabled: yes gpgcheck: no mode: 0644 file: "{{ item }}" description: "{{ item }}" state: present when: - ansible_facts["distribution"] == "RedHat" - ansible_facts["distribution_major_version"] == "8" - name: yum centos7 yum_repository: name: bash baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/ enabled: yes gpgcheck: no mode: 0644 file: bash description: base state: present when: - ansible_facts["distribution"] == "CentOS" - ansible_facts["distribution_major_version"] == "7" - name: yum epel yum_repository: name: epel baseurl: "{{ baseurl }}" enabled: yes gpgcheck: no mode: 0644 file: bash description: epel state: present
验证
[root@apache ~]# ll /etc/yum.repos.d/
总用量 12
-rw-r--r--. 1 root root 122 1月 14 09:50 AppStream.repo
-rw-r--r--. 1 root root 113 1月 14 09:50 BaseOS.repo
[root@cos7 ~]# ll /etc/yum.repos.d/
总用量 4
-rw-r--r--. 1 root root 2523 1月 14 20:42 CentOS-Base.repo