Reusable playbooks, Ansible roles

So, in this blog I have told you about “what is Ansible, playbooks, hosts?” now it is the time to go further with roles. In the world of programming we have a principle called DRY which is “Don’t repeat yourself” means don’t do any work again and again. For that we try to make a process that we have to write the code one time and we can use it again and again without rewriting the whole code again and again.

In the era of automation Ansible also gives us a certain technique that if we need a part of playbook multiple files then we can write it inside a role and use it again and again. It also helps us to reduce the complexity of the code like it is always better to break down a big playbook in a small parts and use it.

Basically for the role we define it inside a roles directory. inside this directory we define or every role and inside the role their will be multiple directories below you can see it –

$ tree .
.
├── roles
│   └── webservers
│       ├── defaults
│       ├── files
│       ├── handlers
│       ├── meta
│       ├── tasks
│       ├── templates
│       └── vars
└── web.yml

Roles expect files to be in certain directory names. Roles must include at least one of these directories, however it is perfectly ok to exclude any which are not being used. When in use, each directory must contain a main.yml file, which contains the relevant content:

  • tasks – contains the main list of tasks to be executed by the role.
  • handlers contains handlers which may be used by the roles or elsewhere in the code.
  • defaults contains variables for the roles.
  • vars contains other variables for the roles.
  • files contains files which will deploy by the roles.
  • templates contains templates which can be deployed via roles.
  • meta contains meta data.

So, now see by example how roles helps us in the various aspects of automation. Below the code is without roles –


---
- hosts: webservers
  gather_facts: yes
  remote_user: root
  become: true
  tasks:
    - name: Ensure group {{ item }} exist
    group:
      name: "{{ item }}"
      state: present
    loop:
      - mozilla
      - dgplug
  - name: add several users to several group
    user:
      name: "{{ item.name }}"
      state: present
      groups: "{{ item.groups }}"
    loop:
      - { name: 'testuser1', groups: 'mozilla' }
      - { name: 'testuser2', groups: 'dgplug' }
  - name: remove several users to several group
    user:
      name: "{{ item.name }}"
      state: absent
      groups: "{{ item.groups }}"
    loop:
      - { name: 'testuser1', groups: 'mozilla' }
      - { name: 'testuser2', groups: 'dgplug' }

Now see how we can implement this via roles –

# web.yml
---
- hosts: webservers
  become: true
  roles:
    - role: '/home/aniruddha/Desktop/ansible-playbook/roles/webservers'

# roles/webservers/tasks/add_group/main.yml
---
tasks:
  - name: Ensure group {{ item }} exist
  group:
    name: "{{ item }}"
    state: present
  loop:
    - mozilla
    - dgplug

# roles/webservers/tasks/add_user_to_group/main.yml
---
tasks:
  - name: add several users to several group
  user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'mozilla' }
    - { name: 'testuser2', groups: 'dgplug' }

# roles/webservers/tasks/remove_user_from_group/main.yml
---
tasks:
  - name: Remove several users from different group
  user:
    name: "{{ item.name }}"
    state: absent
    group: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'mozilla' }
    - { name: 'testuser2', groups: 'dgplug' }

Thank you 🙂

19 thoughts on “Reusable playbooks, Ansible roles

  1. It is the best time to make some plans for the future and
    it is time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or tips.
    Maybe you can write next articles referring to this article.
    I want to read even more things about it! Hi, i read your blog occasionally and i
    own a similar one and i was just curious if you get a
    lot of spam responses? If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me insane so any help is very much appreciated.
    I’ve been surfing online more than 3 hours today, but I never found any interesting
    article like yours. It’s beautiful worth enough for me.
    Personally, if all web owners and bloggers made good content material as you probably did,
    the internet will probably be much more helpful than ever before.
    http://alienware.com

    Like

  2. You really make it seem so easy with your presentation but I find this matfter to be really something that Ithibk I would never
    understand. It seems tooo complicated and very broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

    Like

  3. cheap
    https://titwanktoy.blogspot.com/2019/07/variation-of-chemically-altered-hemp.html
    That’s the message Samsung is sending in Year 2 of the great smartwatch experiment.

    It was just last year that the company started pushing the idea of the tech
    loaded, smartphone driven watch with the Galaxy Gear.

    Every weekend i used to pay a visit this website, for the reason that i
    wish for enjoyment, as this this web page conations in fact nice funny information too.

    Like

  4. Subscription Open up an W88 bill to appreciate all our on line
    gambling gaming and promotions enjoyment of high
    quality at incredibly excellent value. We know in profitable our respected
    buyers with various kind of deposit promotions and bonuses.

    Affiliate marketers

    Be our companion and make very high commission payment amounts every 4 weeks by traveling members to W88.

    W88 strives to supply a station of enjoyment towards the consumer in a optimistic way.
    We have now available key safety measures to promote and ensure reliable
    betting.

    Stability

    A Thorough and Safe and sound Gambling Method. The seclusion of your into is critical to us and we conform to tight confidentiality and privacy plans.

    Sports activities Playing & Dwell Wagering

    This site offers all of the athletic along the
    lines of English language Most recognized League, Spanish language
    La Liga, Italian Serie A, UEFA Champions League, French Ligue 1, German Bundesliga 1,
    NCAA, NFL and NBA Adult females Baseball, Tennis,
    Strategy 1 and much more. Our company offers up to 4,000 Survive Playing football matches monthly for your own betting pleasure.

    On the net Internet casino

    Have fun playing gambling games on the web.
    Look at Roulette, Blackjack, Footage Poker, Progressives, others and Slots plus get a probability to earn much
    more Casino Jackpots on W88.

    Live Casino

    We have comprehensive range of supplies. These comprise of Are located Car dealership Internet casino with
    Live life Reception look at, lovely Oriental traders, Reside Baccarat, Reside Sic Bo,
    Live your life Dragon Tiger and Live Roulette, Web-based Gambling establishment Slot machine games and uncomplicated to learn Keno video game titles.

    Advertising campaigns Downpayment Welcome and Bonus Benefit supply you
    with for all those new associates. Reload Cash and Bonus Incentives for present
    potential customers also

    Help Middle

    Pay a visit to our FAQ post for athletics gambling and
    gaming support. It has aid on accounts opening, withdrawal, technical and deposit
    guidance.

    Transaction Options

    Our company offers very good choice for shoppers to
    put in and withdraw any time you triumph by means ofNETELLER and Moneybookers, Worldwide Loan company Switch, European Union and others.
    We certainly have the whole thing, terrific mmorpgs start with [W88](https://www.w888clubm.com “W88”).

    Call Us

    We are available 24/7 via Live Chat, Telephone and E-mail, if you need any help or have any questions about Live betting or our casino
    games.

    Like

  5. I savor, cause I found just what I used to be having a look for.

    You’ve ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

    Like

  6. ไฮไลท์เด็ด Fun88 คาสิโน Fun88 คาสิโน ออนไลน์ นั้นมีการอัพเดทและปรับปรุงเกมส์ใหม่ๆ อย่างต่อเนื่อง ด้วยรูปแบบที่แปลกใหม่ เต็มไปด้วยสีสันจากเกมของค่ายดังระดับโลกอย่าง
    Fun88 Playtech
    รวมไปถึงเรื่องของการเพิ่มโปรโมชั่นในการแจกโบนัสต่างๆ ให้กับสมาชิกที่เข้ามาร่วมสนุก มีระบบขั้นตอนวิธีการเงินฝาก-ถอนเงินที่สุดแสนจะสะดวกรวดเร็ว ทันใจ ซึ่งด้วยเหตุผลนี้เอง จึงทำให้มีนักพนันจำนวนมาก เข้ามาใช้บริการ คาสิโนสด Fun88 กันอย่างไม่ขาด ทำให้สมาชิกทั่วเอเชียได้รับความพึงพอใจ และต่างก็ยกให้เราเป็นสุดยอดคาสิโนออนไลน์ในเอเชีย [fun88](https://www.fun88no1.com/%E0%B8%82%E0%B8%B1%E0%B9%89%E0%B8%99%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%AA%E0%B8%A1%E0%B8%B1%E0%B8%84%E0%B8%A3-fun88/ “fun88”)

    Like

  7. Good day! I just would like to offer you a big thumbs up
    for the great information you have got right here on this post.
    I’ll be returning to your blog for more soon.

    Like

  8. Awesome Web site, Continue the excellent work. Thanks.

    [http://como-aumentar-penis-pt.eu/atlant-gel.html](http://como-aumentar-penis-pt.eu/atlant-gel.html “http://como-aumentar-penis-pt.eu/atlant-gel.html”) [http://pastillasparalapotencia2017.ovh/](http://pastillasparalapotencia2017.ovh/ “http://pastillasparalapotencia2017.ovh/”) [http://integratorimuscoli.eu/anabolicrx24.html](http://integratorimuscoli.eu/anabolicrx24.html “http://integratorimuscoli.eu/anabolicrx24.html”) [http://tabletki-na-stawy2020.eu/artrovex.html](http://tabletki-na-stawy2020.eu/artrovex.html “http://tabletki-na-stawy2020.eu/artrovex.html”) [http://pastillas-para-la-impotencia-masculina.eu/biobelt.html](http://pastillas-para-la-impotencia-masculina.eu/biobelt.html “http://pastillas-para-la-impotencia-masculina.eu/biobelt.html”) http://peniksen-pidennys-fi.eu/erogan.html

    Like

  9. Awesome Web site, Continue the excellent work.
    Thanks.
    [http://como-aumentar-penis-pt.eu/atlant-gel.html](http://como-aumentar-penis-pt.eu/atlant-gel.html “http://como-aumentar-penis-pt.eu/atlant-gel.html”) [http://pastillasparalapotencia2017.ovh/](http://pastillasparalapotencia2017.ovh/ “http://pastillasparalapotencia2017.ovh/”)
    [http://integratorimuscoli.eu/anabolicrx24.html](http://integratorimuscoli.eu/anabolicrx24.html “http://integratorimuscoli.eu/anabolicrx24.html”) [http://tabletki-na-stawy2020.eu/artrovex.html](http://tabletki-na-stawy2020.eu/artrovex.html “http://tabletki-na-stawy2020.eu/artrovex.html”) [http://pastillas-para-la-impotencia-masculina.eu/biobelt.html](http://pastillas-para-la-impotencia-masculina.eu/biobelt.html “http://pastillas-para-la-impotencia-masculina.eu/biobelt.html”) http://como-aumentar-penis-pt.eu/atlant-gel.html

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s