Saturday, May 10, 2025
Alternative Way
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
Alternative Way
No Result
View All Result

Ansible Playbook: Complete Beginners’s Guide

Gordon James by Gordon James
October 3, 2021
in Latest
0 0
0
Home Latest

In the previous lesson, you learned how to use the Ansible ad hoc commands to perform a single task on managed hosts. In this tutorial, you’ll learn how to automate multiple tasks on managed hosts by creating and running accessible playbooks.

To better understand the differences between Ansible ad hoc and Ansible playbooks, you can consider the Ansible ad hoc playbooks as Linux commands and the playbooks as bash-scripts.

Any special commands are ideal for tasks that are not carried out frequently, such as keeping servers operational, receiving system information, etc.

On the other hand, Ansible Playbooks are ideal for automating complex tasks such as installing system patches, deploying applications, setting up a firewall, managing users, etc.

Note that in this GitHub repository, I’ve included all the playbooks, scripts, and files I’m going to talk about in this series.

Before following this Authorized Reading Guides manual, please review the settings in section 1 of the Authorized Reading Guides series.

Make your first approved game

The playbooks are written in YAML format (YAML = Yet Another Markup Language). In case you are unfamiliar with YAML, I have included the main syntax rules of YAML in the following figure, so you can easily follow all the examples in the manual of the game:

Ansible Playbook: Complete Beginners’s Guide

You should also keep in mind that YAML files should also have the extension .yaml or .yml. Personally, I prefer the .yml because it’s less crowded and I’m lazy.

In addition, the YAML is sensitive to nicks. It is recommended to use a two-dimensional fingerprint in YAML, but YAML will track any fingerprint system used by the file if it is consistent.

It is uncomfortable to keep pressing two spaces on the keyboard, so do yourself a favor and add the following line to the ~/.vimrc file

autocmd FileType yaml setlocal ai ts=2 sw=2 et al.

This converts tabs to two spaces each time you work with the YAML file. Did you like Jim’s practical advice? You can get this book for advanced advice from Wims.

The mastery of Wim Quick – Jowica Ilitz

When leaving Vim, you can quickly switch from WTF to OMG in the shortest possible time.

Jovica Ilic

http://server.digimetriq.com/wp-content/uploads/2020/10/Ansible-Playbook-Complete-Beginnerss-Guide.jpg

Now, let’s make your first piece. Create a file named firstplaybook.yml in the project folder with the following contents :

[[protected email] play] $ cat firstplaybook.yml
—
– name: play first
hosts: all tasks
:
– Name: create a new file to
: Path
: /tmp/foo.conf
Mode : 0664 Owner
: elliot
Condition: touch

This playbook works on all hosts and uses a file module to create a file named /tmp/foo.conf; you also need to set the mode to: 0664 and the owner: the elliot parameter of the module to set the access rights to the file and the owner of the file. Finally, set the Status: Tap to make sure the file is created if it does not already exist.

To start a playbook, use the asible-playbook command followed by the playbook’s filename:

enable-playbook firstplaybook.yml

Here is the full result of the above command:

$able-playbook firstplaybook.yml.

PLAY [first game] **************************************************************

TASK [Collection of Facts] *********************************************************
ok:
ok:
ok:
ok: [Knot 2]

TASK [create new file] *******************************************************
changed :
is changed :
is changed :
is changed: [Knot 2]

Game record *********************************************************************
node1: ok=2 modified=1 unreachable=0 failed=0 saved=0 ignored=0
node2 : ok=2 modified=1 unreachable=0 failed=0 skipped=0 saved=0 ignored=0
node3: ok=2 modified=1 unreachable=0 failed=0 skipped=0 saved=0 ignored=0
node4: ok=2 modified=1 unreachable=0 failed=0 skipped=0 saved=0 ignored=0

The result of the Playbook is very clear. For now, pay particular attention to change=1 in the PLAY RECAP summary, which means that a change has been successfully made to the node to be maintained.

Let’s run the following ad hoc command to make sure /tmp/foo.conf is actually created on all managed hosts:

[[email protected] plays]$ asible all -m command -a ls -l /tmp/foo.conf
node4 | CHANGED | rc=0 >>
-rw-rw-rw— 1 elliot root 0 Oct 25 03:20 /tmp/foo.conf
node1 | CHANGED | rc=0 >>
-rw-rw-rw-rw—. 1 Elliot root 0 Oct 25 03:20 /tmp/foo.conf
node2 | Modified | rc=0 >>
-rw-r—. 1 Elliot root 0 October 25 03:20 /tmp/foo.conf
node3 | Modified | rc=0 >>
-rw-r—. 1 Elliot root 0 Oct 25 03:20 /tmp/foo.conf

Note that you can also use the ad hoc command Ansible, which does the same as the first version of playbook.yml :

possibly all -m file -a path=/tmp/foo.conf mode=0664 owner=elliot state=touch

For more details on the File module, see the Authorized Documentation page:

Secure email playback: $able-doc file.

Starting several games with the authorized game book

You have only created one game that contains a task on the first game booklet. A set can contain multiple sets, and each set can contain multiple tasks.

Let’s create a playbook called multiple-plays.yml with the following content:

[[protected email] plays] $ cat multiple-plays.yml
—
– name: plays hosts
first: all tasks
:
– Name : tmux
Install the :
Name: tmux
Status: currently

– Name: Create archive
: Path
: /var/log
dest: /tmp/logs.tar.gz Format
: gz

– Name: Second game
Host: Node4
Tasks :
– Name: set git
apt :
Name: git
Status: currently

There are two moves in this game:

  • First set (contains two tasks) – works on all hosts.
  • The second set (contains one task) – it starts only on the node4.

Note that I used a package manager module in the first set because it is a generic package manager module and it automatically detects the default package manager on managed hosts. In the second game I used the apt module because I only run it on the Ubuntu host (node 4).

There are also yum and dnf modules that run on CentOS and RHEL systems.

I also used the archiving module to create the compressed gzip-archive /tmp/logs.tar.gz that contains all the files in /var/log.

Come on, start the multiplayer game:

$able-playbook multiple-plays.yml plays.

PLAY [first game] **************************************************************

TASK [install tmux] ************************************************************
amended :
amended:
modified:
modified: node1

TASK [Create Archive] ******************************************************* changed: [knots2] changed: [knots3] changed: [knots1] changed: [knots4]

PLAY [second game] *************************************************************.

TASK [set git] *************************************************************
modified : [node4].

GAME RECORD *********************************************************************
node1: ok=2 modified=2 unreachable=0 failed=0 saved=0 ignored=0
node2: ok=2 modified=2 unreachable=0 failed=0 saved=0 ignored=0
node3: ok=2 modified=2 unreachable=0 failed=0 ignored=0
node4: ok=3 modified=3 unreachable=0 failed=0 ignored=0 saved=0

Everything looks good. You can quickly check the existence of the /tmp/logs.tar.gz archive on all nodes by executing the following ad hoc command:

[[email protected] plays]$able all -m command -a file -s /tmp/logs.tar.gz
node4 | CHANGED | rc=0 >>
/tmp/logs.tar.gz: data compressed by gzip, was /tmp/logs.tar, last modified About 25. 04:40:46 October 2020, maximum compression
node1 | CHANGED | rc=0 >>
/tmp/logs.tar.gz : Data compressed by gzip, was /tmp/logs.tar, last modified : Sunday the 25th. 04:40:47 October 2020, maximum compression, original size 107458560
node3 | CHANGED | rc=0 >>
/tmp/logs.tar.gz : data compressed by gzip, was /tmp/logs.tar, latest changes : Approximately 25. 04:40:47 October 2020, maximum compression, original size 75560960
node2 | CHANGED | rc=0 >>
/tmp/logs.tar.gz : The gzip compressed data was /tmp/logs.tar, last modified: Sunday the 25th. October 04:40:47 2020, maximum compression, original size 52326400

In addition, I recommend that you read the following authorized pages of the documentation and the Examples section:

[[e-mail protected] plays] $ ansible-doc package
[[e-mail protected] plays] $ ansible-doc archive
[[e-mail protected] plays] $ ansible-doc apt
[[e-mail protected] plays] $ ansible-doc yum

Checking your playbooks (before you start)

Although I’ve already shown you the steps to using Ansible games, it’s always a good idea to check your game before using it. This ensures that your booklet is free of possible errors.

You can use the –syntax-check option to check your playbook for syntax errors:

$able-playbook –syntax-check firstplaybook.yml
playbook: firstplaybook.yml

You can also use the -check option to try out your playbook before launching it:

$ ansible-playbook – check firstplaybook.yml

PLAY [first game] **************************************************************

TASK [Collection of Facts] *********************************************************
ok:
ok:
ok:
ok: [Knot 2]

TASK [create new file] *******************************************************
ok :
ok :
ok :
ok : [node 3].

GAME RECORD *********************************************************************
node1: ok=2 modified=0 unreachable=0 failed=0 saved=0 ignored=0
node2: ok=2 modified=0 unreachable=0 failed=0 saved=0 ignored=0
node3: ok=2 modified=0 unreachable=0 failed=0 ignored=0
node4: ok=2 modified=0 unreachable=0 failed=0 ignored=0 ignored=0 ignored=0

Note that if you dry the book, no changes will be made to the managed nodes.

Using the -list options, you can create a list of the owners of each piece in your game book:

$ ansible-playbook –list hosts multiple-plays.yml

Game book: Multi-plays.yml

Game no. 1 (all): First game TAGS : Model
: [all] hosts
(4) :
node4
node2
node1
node3

second set of TAGS : Model
: [node4] Host (1) :
node4

You can also include the tasks for each game in your game book using the –tasks list option:

$able-playbook –list-tasks multiple-plays.yml — [Email protected] play

Game book: Multi-plays.yml

Game no. 1 (all): First game TAGS : Jobs
:
Installation tmux TAGS :
Create TAGS archive : : : []

Second set of TAGS: Jobs
:
Parameters of the anti-corruption strategy: [].

You can also visit the Man Game Book page for a full list of options.

Reuse and reading tasks

You can write several books with general tasks. In this case it is best to create a file with a list of all the common tasks, after which you can reuse it in your playbooks.

For demonstration purposes, we create a file called group-tasks.yml, which contains the following tasks

[[protected email] plays]$ cat group-tasks.yml
– name : Create developer group
:
name : Developer
– Name : Create security group
:
name : Security
– name : name of the financial group:: Finances

You can now use the import_tasks module to perform all tasks in groupstasks.yml in your first playbook as follows:

[[protected email] play] $ cat firstplaybook.yml
—
– name: play first
hosts: all tasks
:
– Name: create a new file to
: Path
: /tmp/foo.conf
Mode : 0664 Owner
: elliot
Condition: touch

– Name: Creating groups
import_tasks: group-tasks.yml

You can also use the import_playbook module to reuse the entire playbook. For example, you can create a new playbook called re-use-playbook.yml, which will have the following content

[[Protected Email] plays] $ cat re-use-playbook.yml
—
– Name : Reuse of the script: all tasks : – Name: Reboot server
:
msg Server restart …

– Name: Start the first playbook
import_playbook: firstplaybook.yml

Also note that you can only import the game book at a new level of the game, i.e. you cannot import the game into another game.

You can also use the included module to reuse tasks and playbooks. For example, you can replace the import_playbook operator with the include operator as follows

[[Protected Email] plays] $ cat re-use-playbook.yml
—
– Name : Reuse of the script: all tasks : – Name: Reboot server
:
msg Server restart …

– Name: Run the first game booklet
.

The only difference is that the input sheets are pre-processed during the reading analysis. On the other hand, the statements are incorporated into the piece as they are found during the performance. So the input is generally static, while the recording is dynamic.

Performing selective tasks and playing withPermitted play

You can choose not to run an entire Playbook and perform certain tasks or replay them in the Playbook. You can use labels for this.

For example, you can mark the Git installation job in the playbook’s multiple-plays.yml file as follows

[[protected email] plays] $ cat multiple-plays.yml
—
– name: plays hosts
first: all tasks
:
– Name : tmux
Install the :
Name: tmux
Status: currently

– Name: Create archive
: Path
: /var/log
dest: /tmp/logs.tar.gz Format
: gz

– Name: Second game
Host: Node4
Tasks :
– Name: set git
apt :
Name: git
Status: current tags
: git

You can now use the –tags option, followed by the name of the Git tag, to perform only the Git installation task:

[email protected] play] $able-playbook multiple-plays.yml –tags git

PLAY [first game] **************************************************************

GAME [Second Game] *************************************************************.

TASK [install git] *************************************************************
ok: [node4].

Game record *********************************************************************
node4: ok=1 modified=0 unreachable=0 failed=0 skipped=0 saved=0 ignored=0

As you can see, the first two games have been missed and only the installation movement has started. You can also see change=0 in PLAY RECAP, because git is already on node 4.

You can mark a document in the same way.

All right! That brings us to the end of the manual of the Ansible Playbooks. Stay with us for the next lesson as you learn to work with variables, facts, and possible registers. Don’t forget to become a member of the Linux Handbook.

Related Tags:

modoboa install,modoboa docker,modoboa review,modoboa ldap,modoboa port,modoboa ssl certificate

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
ShareTweet
Gordon James

Gordon James

Next Post
Sony Will Record Voice Chat on PlayStation 5 and PS 4

Sony Will Record Voice Chat on PlayStation 5 and PS 4

  • Trending
  • Comments
  • Latest
How To Get Free Internet On Android Without Service

How To Get Free Internet On Android Without Service

March 10, 2022
🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

October 3, 2021

Fix: Notifications not working on Nova Launcher

October 3, 2021
How to Fix OpenVPN Connected but Not Changing IP Address

How to Fix OpenVPN Connected but Not Changing IP Address

October 3, 2021

Setting Up Directory Sync Between On-Premises Active Directory with Microsoft 365 Azure AD

0
🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

0
🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

0
🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

0
What to Know About Car Shipping Services

What to Know About Car Shipping Services

May 7, 2025
CS2 Skins-Why Trade Them?

CS2 Skins-Why Trade Them?

May 7, 2025
Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

May 6, 2025
The Ultimate Seiko Watch Gift Guide

The Ultimate Seiko Watch Gift Guide

May 1, 2025

There's always an alternative Way!
Find us at 4145 Zolynthian Street, Vylorthos, QP 78425
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use

© 2022 - Alternative Way

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • Travel News

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.