0% found this document useful (0 votes)
8 views3 pages

Ansible Setup and Playbook Guide

The document discusses Ansible and provides examples of using Ansible to manage hosts and run tasks. It covers installing and configuring Ansible, creating inventory files, using modules to manage services, and writing playbooks with multiple plays and tasks.

Uploaded by

magi9999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Ansible Setup and Playbook Guide

The document discusses Ansible and provides examples of using Ansible to manage hosts and run tasks. It covers installing and configuring Ansible, creating inventory files, using modules to manage services, and writing playbooks with multiple plays and tasks.

Uploaded by

magi9999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Chap1

A machine acting as a control node must have Python 2.6 or 2.7 installed.
Managed hosts must have Python 2.4 or later installed to run
Ansible, which includes Red Hat Enterprise Linux 5, 6, and 7 hosts.
The winrm Ansible connection plugin allows Microsoft Windows machines to be managed
hosts.

host inventory file


[webservers]
localhost ansible_connection=local
[Link]
[Link] ansible_connection=ssh ansible_user=ftaylor
[Link]
[db-servers]
[Link]
[Link]

The default location for the host inventory file is /etc/ansible/hosts


server[01:20].[Link] - all hosts named [Link] through
[Link]
ansible [Link] --list-hosts
ansible [Link],test,[Link] -i myinventory --list-hosts
ansible '*.[Link]' -i myinventory --list-hosts
ansible -h or ansible --help
ansible --version
ansible lab:datacenter1 -i myinventory --list-hosts ; OR of two groups
ansible lab:&datacenter1 -i myinventory --list-hosts ;AND of two groups
ansible 'all:!datacenter1' -i myinventory --list-hosts ;ALL except datacenter1
group.
Define the location of the configuration file with the $ANSIBLE_CONFIG.
Use ansible --version to find the active configuration file or the below
ansible servers --list-hosts -v
export ANSIBLE_CONFIG=/home/student/dep-config/[Link]
---------------------------------------------------------------
[defaults]
remote_user=devops
inventory=inventory
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=True
-----------------------------------------------
ansible host-pattern -m module -a 'argument1 argument2' [-i inventory]
sudo cat /etc/sudoers.d/devops
devops ALL=(ALL) NOPASSWD: ALL
---------------------------------------
ansible localhost -m copy -a 'content="Managed byAnsible\n" dest=/etc/motd' -u
devops
--become --become-user root

ansible everyone -m command -a 'cat /etc/motd' -u


devops
----------------------------------------------------
chapter 3 Playbooks
ansible-doc -l
ansible-doc yum
ansible-doc -s yum ( snippet option)
ansible localhost -m yum -a "name=tree state=present"
yum list installed tree
ansible localhost -m service -a "name=httpd state=restarted"
ansible localhost -m uri -a "url=[Link]
modules for user and service management can be found under the SystemsModules
and modules for database administration can be found under Database Modules.
ansible localhost -m yum -a "name=tree state=present"
ansible localhost -m service -a "name=httpd state=restarted"
ansible [Link] -m service -a "name=httpd enabled=yes
state=started" -b
------------------------------------------------------------------------
- name: Idempotent approach with copy module
copy:
dest: /etc/[Link]
content: "nameserver [Link]\n"
-----------------------------------------------------------
# This is a simple playbook with a single play
- name: a simple play
hosts: [Link]
user: remoteuser
become: yes
become_method: sudo
become_user: root
tasks:
- name: first task
service: name=httpd enabled=true
- name: second task
service: name=sshd enabled=true
...
-----------------------------------------------------------
tasks:
- name: first task single line formating
service: name=httpd enabled=true state=started
---------------------------------------------------------
tasks:
- name: first task example of Multi Line formating
service: name=httpd
enabled=true
state=started
-----------------------------------------------------
tasks:
- name: first task dictionary format
service:
name: httpd
enabled: true
state: started
---------------------------------------------------------------
----------------------------------------------------------
# This is a simple playbook with two plays
- name: first play
hosts: [Link]
tasks:
- name: first task
service:
name: httpd
enabled: true
- name: second play
hosts: [Link]
tasks:
- name: first task
service:
name: mariadb
enabled: true
...
---------------------------------------------------------
ansible-playbook -C [Link] ( to execute a dry run )
ansible-playbook --step [Link] ( step execution)

Common questions

Powered by AI

The winrm Ansible connection plugin allows Ansible to interact with Microsoft Windows machines by enabling remote management through the Windows Remote Management (WinRM) protocol, bridging the gap for managing non-Linux hosts .

The `ansible` command can list specific groups of hosts using patterns like `ansible washington1.example.com --list-hosts`, or by specifying groups with syntax such as `ansible labhost1.example.com,test,192.168.2.2 -i myinventory --list-hosts`. It also supports combination conditions like `ansible lab:datacenter1 -i myinventory --list-hosts` for OR and `ansible lab:&datacenter1 -i myinventory --list-hosts` for AND .

Defining a `remote_user`, such as `remote_user=devops`, in Ansible specifies the default user for SSH connections to hosts, ensuring consistent access across tasks. This setting interacts with inventory specifications to streamline user management and avoid per-task user configuration .

Ansible supports different task formatting styles in playbooks, including single-line formatting, multi-line formatting, and dictionary formatting. Each style offers different readability and maintainability advantages; single-line is compact, multi-line is more readable for complex operations, and dictionary is explicit, highlighting task parameters clearly .

The playbook structure in Ansible allows for the segmentation of automation tasks into distinct plays. This separation facilitates logical grouping by host or task type, improving workflow efficiency by enabling simultaneous handling of different server roles, such as web servers and database servers, within a single organized document .

Ansible automates user management tasks by interfacing with existing Linux tools like `yum` or `service` through specialized modules. The `SystemModules` group handles user-related operations, ensuring integration with system-level user management functionalities .

Privilege escalation in Ansible can be configured under the `[privilege_escalation]` section of the configuration file. Essential settings include `become=True`, `become_method=sudo`, `become_user=root`, and `become_ask_pass=True`, which allow tasks to run with elevated privileges when required .

The location of Ansible's configuration file can be set by the environment variable `ANSIBLE_CONFIG`, such as `export ANSIBLE_CONFIG=/home/student/dep-config/myansible.cfg`. To verify the active configuration file, `ansible --version` can be used to display the current configuration .

Control nodes must have Python 2.6 or 2.7 installed, while managed hosts can run Ansible with Python 2.4 or later, which includes compatibility with Red Hat Enterprise Linux versions 5, 6, and 7 .

A dry run of an Ansible playbook can be executed using the `-C` option, such as `ansible-playbook -C webserver.yml`. This allows users to simulate the playbook's execution without making any changes, helping to verify and test the playbook's operations before live execution .

You might also like