Where to place requirements.yml for Ansible and use it to resolve dependencies?

后端 未结 1 1624
逝去的感伤
逝去的感伤 2020-12-16 20:56

I am new to ansible and was exploring dependent roles. documentation link

What I did not come across the documentation was- where to place the requirements.yml

1条回答
  •  暖寄归人
    2020-12-16 21:39

    Technically speaking, you could put your requirements.yml file anywhere you like as long as you reflect the correct path in your ansible-galaxy install command.

    Meanwhile, if you ever want to run your playbooks from Ansible Tower/Awx, I suggest you stick to the Ansible Tower requirements and put your requirements.yml file in /roles/requirements.yml

    Regarding dependencies between roles, ansible-galaxy is able to follow them by itself when they are encountered during installation. So you don't need to specify all of them in your requirements.yml, only top level ones. You just need to specify your dependencies correctly in each external roles.

    In meta/main.yml for role1

    dependencies:
      - src: https://my.scm.com/my-ansible-roles/role2.git
        scm: git
        version: master
        name: role2
      - src: https://my.scm.com/my-ansible-roles/role3.git
        scm: git
        version: master
        name: role3
    

    In meta/main.yml for role2

    dependencies:
      - src: https://my.scm.com/my-ansible-roles/role4.git
        scm: git
        version: master
        name: role4
      - src: https://my.scm.com/my-ansible-roles/role5.git
        scm: git
        version: master
        name: role5
    

    roles/requirements.yml

    ---    
    - src: https://my.scm.com/my-ansible-roles/role1.git
      scm: git
      version: master
      name: role1
    

    To be as exhaustive as possible, this is what I now usually do on my projects to handle dependencies locally as well as local/project only roles

    Basic project structure

    ansible-project-dir
    └─── roles
    |    └─── locally-versionned-role1
    |    └─── locally-versionned-role2
    |    └─── ...
    |    └─── requirements.yml
    |    └─── .gitignore
    └─── ansible.cfg
    └─── playbook1.yml
    └─── playbook2.yml
    

    ansible.cfg

    I force roles search and downloads in the local roles directory by setting roles_path = roles, so user can use ansible-galaxy install without the -p parameter.

    roles/requirements.yml

    Already discussed above. Just list dependencies to top-level external (i.e. not versionned in the project) as galaxy role name or as git uris. If you need to fully checkout those roles to later make git commits/push on them, you can use ansible-galaxy install -g -f -r roles/requirements

    roles/.gitignore

    # Ignore everything in roles dir
    /*
    # Except:
    # the .gitignore file
    !.gitignore
    # the requirements file
    !requirements.yml
    # Readme if you have one
    !README.md
    # and any specific role we want to version locally
    !locally-versionned-role*/
    
    
    

    0 讨论(0)
提交回复
热议问题