ansible-playbook配置不同系统yum源(条件语句when)

1.准备工作

  • 操作环境
主机 ip 系统
node0(ansible) 192.168.94.142 rhel8
node4 192.168.94.131 centos7
node5 192.168.94.140 rhel8
  • yum任务目录结构
[root@node0 yum]# tree .
.
├── ansible.cfg
├── inventory
├── scripts
│   ├── centos7yum.sh
│   └── rhel8yum.sh
└── yum.yml

1.1配置清单

  • 映射ip
[root@node0 yum]# vim /etc/hosts 

...
192.168.94.131 node4
192.168.94.140 node5

  • 复制ansible配置文件到yum目录下
[root@node0 yum]# cp /etc/ansible/ansible.cfg .
[root@node0 yum]# vim ansible.cfg 


[defaults]
...
# some basic default values...

inventory      = ./inventory  #设定清单在当前目录下

  • 创建清单文件
[root@node0 yum]# cat inventory 

[yum]
node4
node5

  • 创建公钥
[root@node0 yum]# ssh-keygen -t rsa

  • 复制公钥到受控机
[root@node0 yum]# ssh-copy-id root@node4
[root@node0 yum]# ssh-copy-id root@node5

  • 查看清单
[root@node0 yum]# ansible all --list-hosts
  hosts (2):
    node4
    node5

  • 测试连接
[root@node0 yum]# ansible yum -m ping
node4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
node5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

2.yum剧本配置

2.1编写yum剧本

[root@node0 yum]# cat yum.yml 
---
- name: yum config
  hosts: yum
  gather_facts: yes 
  tasks:
    - name: base yum config for CentOS7 or lower
      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"] == "CentOS"
        - ansible_facts["distribution_major_version"] == "7"



    - name: epel yum config for CentOS7 or lower
      yum_repository:
        name: epel
        description: EPEL YUM repo
        file: epel
        baseurl: https://mirrors.aliyun.com/epel/7/x86_64
        gpgcheck: no
        mode: 0644
        state: present
      when: 
        - ansible_facts["distribution"] == "CentOS"
        - ansible_facts["distribution_major_version"] <= "7"
      notify:
        - makecache

    - name: yum config for RedHat8
      loop:
        - AppStream
        - BaseOS
      yum_repository:
        name: "{{ item }}" 
        description: "{{ item }}" 
        file: "{{ item }}"
        baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/ 
        gpgcheck: no
        mode: 0644
        state: present
      when: 
        - ansible_facts["distribution"] == "RedHat"
        - ansible_facts["distribution_major_version"] == "8"
      notify:
        - makecache

    - name: epel yum config for RedHat8
      yum_repository:
        name: epel
        description: EPEL YUM repo
        file: epel
        baseurl: https://mirrors.aliyun.com/epel/8/Modular/x86_64//
        gpgcheck: no
        mode: 0644
        state: present
      when: 
        - ansible_facts["distribution"] == "RedHat"
        - ansible_facts["distribution_major_version"] == "8"
      notify:
        - makecache

  handlers:
    - name: makecache
      shell: 'yum makecache'

3.运行验证

  • 运行剧本
[root@node0 yum]# ansible-playbook  yum.yml 

PLAY [yum config] ***************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [node4]
ok: [node5]

TASK [base yum config for CentOS7 or lower] *************************************************************************
skipping: [node5]
changed: [node4]

TASK [epel yum config for CentOS7 or lower] *************************************************************************
skipping: [node5]
changed: [node4]

TASK [yum config for RedHat8] ***************************************************************************************
skipping: [node4] => (item=AppStream) 
skipping: [node4] => (item=BaseOS) 
changed: [node5] => (item=AppStream)
changed: [node5] => (item=BaseOS)

TASK [epel yum config for RedHat8] **********************************************************************************
skipping: [node4]
changed: [node5]

RUNNING HANDLER [makecache] *****************************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because yum is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get
rid of this message.
changed: [node5]
changed: [node4]

PLAY RECAP **********************************************************************************************************
node4                      : ok=4    changed=3    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
node5                      : ok=4    changed=3    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 
  • 各受控机验证

node4

[root@node4 ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                 repo name                                                      status
base                                                    base                                                           enabled: 10,072
epel                                                    EPEL YUM repo                                                  enabled: 13,494
repolist: 23,566


  • node5
[root@node5 ~]# yum repolist all 
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.
repo id                                          repo name                                             status
AppStream                                        AppStream                                             enabled
BaseOS                                           BaseOS                                                enabled
epel                                             EPEL YUM repo                                         enabled
原文地址:https://www.cnblogs.com/fangxinxin/p/14274890.html