ip | 系统 | |
---|---|---|
localhost(装有ansible) | 192.168.44.128 | rhel8 |
node2 | 192.168.44.131 | rhel8 |
node3 | 192.168.44.132 |
结构树一览
[root@localhost ~]# tree . . ├── anaconda-ks.cfg └── yum ├── ansible.cfg ├── inventory └── yum.yml
准备环境
[root@localhost ~]# mkdir yum [root@localhost ~]# cd yum/ [root@localhost yum]# cp /etc/ansible/ansible.cfg . [root@localhost yum]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.44.131 node2 192.168.44.132 node3 [root@localhost yum]# vim ansible.cfg inventory = ./inventory [root@localhost yum]# vim inventory [centos] node2 [redhat] node3 [root@localhost yum]# cd ~ [root@localhost ~]# ssh-keygen -t rsa [root@localhost ~]# ssh-copy-id root@192.168.44.131 [root@localhost ~]# ssh-copy-id root@192.168.44.132 [root@localhost ~]# ansible node2 -m ping [root@localhost ~]# ansible node3 -m ping
编写yum的playbook
[root@localhost ~]# vim yum/yum.yml --- - hosts: all vars: baseurl_8: https://mirrors.aliyun.com/epel/8/Modular/x86_64/ baseurl_7: https://mirrors.aliyun.com/epel/7/x86_64/ tasks: - name: yum config for 8 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 loop: - BaseOS - AppStream when: > ( ansible_facts["distribution"] == "RedHat" and ansible_facts["distribution_major_version"] == "8" ) or ( ansible_facts["distribution"] == "CentOS" and ansible_facts["distribution_major_version"] == "8" ) - name: yum config for 7 yum_repository: name: base baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/ enabled: yes gpgcheck: no mode: 0644 file: base description: base state: present when: > ( ansible_facts["distribution"] == "RedHat" and ansible_facts["distribution_major_version"] == "7" ) or ( ansible_facts["distribution"] == "CentOS" and ansible_facts["distribution_major_version"] == "7" ) - name: yum config epel for 8 yum_repository: name: epel baseurl: "{{ baseurl_8 }}" enabled: yes gpgcheck: no mode: 0644 file: epel description: epel state: present when: > ( ansible_facts["distribution"] == "RedHat" and ansible_facts["distribution_major_version"] == "8" ) or ( ansible_facts["distribution"] == "CentOS" and ansible_facts["distribution_major_version"] == "8" ) - name: yum config epel for 7 yum_repository: name: epel baseurl: "{{ baseurl_7 }}" enabled: yes gpgcheck: no mode: 0644 file: epel description: epel state: present when: > ( ansible_facts["distribution"] == "RedHat" and ansible_facts["distribution_major_version"] == "7" ) or ( ansible_facts["distribution"] == "CentOS" and ansible_facts["distribution_major_version"] == "7" )
运行playbook
[root@localhost ~]# cd yum/
[root@localhost yum]# ansible-playbook yum.yml
查看效果
//redhat8 [root@node2 ~]# yum repolist Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Repository BaseOS is listed more than once in the configuration Repository AppStream is listed more than once in the configuration repo id repo name AppStream AppStream BaseOS BaseOS epel epel //redhat7 [root@node3~]# yum repolist Loaded plugins: product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. repo id repo name status base base 10,072 epel epel 105 repolist: 10,177