Tag Wint
Tag Wint

Reputation: 457

How to make Ansible notice host is the same and avoid task collisions

My inventory files keep the same logical number of hostsbut may differ in the number of ansible_host:

Case 1, three logical, hosts three ansible_hosts

all:                                                                                                                          
  hosts:                                                                                                                      
    db:                                                                                                        
      ansible_host: db.example.com                                                                                                   
    apps:                                                                                                            
      ansible_host: app.example.com                                                                                                   
    proxy: 
      ansible_host: proxy.example.com  

Case 2, three logical, hosts one ansible_host

all:                                                                                                                          
  hosts:                                                                                                                      
    db: &ahost                                                                                                       
      ansible_host: allinone.example.com                                                                                                   
    apps: 
      <<: *ahost
    proxy: 
      <<: *ahost

In my playbook there are some common tasks applicable for all, like yum

  - name: "Install OS packages"                                                                                               
    yum: pkg={{item}} state=installed                                                                                         
    with_items:                                                                                                               
      - p1
      - p2
      - ... (quite a lot)

they may collide when running since ansible tries to run them in parrallel which causes yum session locks.

I wonder how can I tell ansible to skip installing os packages on apps and proxy once it has already started yum on db in Case 2. (Other than swithcing off the parrallelism)

Upvotes: 0

Views: 692

Answers (1)

Aleksandr Rogozin
Aleksandr Rogozin

Reputation: 114

You can take advantage of Ansible File module. Prior to you Install OS Packages step, you can create a temporary file.

- name: "Create temporary file to lock yum"
  file:
    path: /tmp/yum.lock
    state: present

Next, you will create a task to check if lock file exists. We expect for it to exist, once it was created during the first connection.

- name: "Check if yum lock file exists"
    stat:
      path: /tmp/yum.lock
    register: yum_lock

Then, modify your task:

  - name: "Install OS packages"                                                                                               
    yum: pkg={{item}} state=installed  
    when: yum_lock.stat.exists == False                                                                                       
    with_items:                                                                                                               
      - p1
      - p2
      - ... (quite a lot)

Finally, add a cleanup task to remove yum lock file.

- name: "Delete temporary file to lock yum"
  file:
    path: /tmp/yum.lock
    state: absent

Upvotes: 0

Related Questions