Part 2 - Section 3 and further Flashcards

1
Q

Create a playbook to ping your localhost

A

vi first.yml

”—”
- name
hosts: localhost
tasks:
- name: “test connectivity
ping:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Check the syntax of your playbook

Run your playbook without affecting anything

A

ansible-playbook –syntax-check first.yml

dry run - runs like it would normally but won’t affect anything
ansible-playbook –check first.yml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create playbook that outputs “Hello World” on localhost

A

vi heloworld.yml

  • name: My Second Playbook
    hosts: localhost
    tasks:
    • name: Print Hello World
      debug: msg=”Hello World”

ansible-playbook helloworld.yml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a playbook that pings the local host and also prints “Hello World”

A

vi mtask.yml
“—”
- name: Running Two Tasks
hosts: localhost
tasks:
- name: Test Connectivity
ping:

- name: Print Hello World
  debug: msg="Hello World" "..." ansible-playbook mtask.yml
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Install a package and start it via playbook

A
  • name Install HTTPD
    hosts: localhost
    tasks:
    • name: Install Apache
      yum:
      name: httpd
      state: present
    • name: Start Httpd
      service:
      name: httpd
      state: started
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create a webservers group of clients in your hosts file for 192.168.10.12
192.168.10.15-22

How would you specify a different hosts file in a different location using ansible-playbook

A

[webservers]
192.168.10.12
192.168.10.[15:22]

ansible-playbook -i /home/me/hosts

If you have hosts in another directory, in your playbook just list it as the name of the inventory. When referencing with “-i” put the complete path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you don’t have a fqdn for your clients that you want in a hostfile, how can you setup an Alias for them?

A

[servers]
server1 ansible_ssh_host=192.168.10.142

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between static and dynamic hosts in a config file

A

dynamic - dhcp clients

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Run a command to see what’s in your hostfile

A

ansible-inventory –list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Establish a connection to remote clients
ping all clients with ansible
Show all their uptime

A

in hosts
[webservers]
192.168.10.142

ansible-inventory –list

ssh-keygen
ssh-copy-id 192.168.10.142

ansible all -m ping

ansible -a “uptime” all

a = arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Check remote client connectivity for all inventory via a playbook

Basically just ping everything in your hosts file

A

vi clientstatus.yml

-name: “Connectivity check”
hosts: all
tasks:
- name: Test Connectivity
ping:

ansible-playbook clientstatus.yml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Copy a file over to multiple clients via a playbook
The file should be owned by the delsinm user and group. Permissions should be 0644

A

echo “data” > some.cfg
vi copy.yml

  • name Copy
    hosts: all
    tasks:
    • name: copy
      become: True
      copy:
      src: /home/delsinm/some.cfg
      dest: /tmp
      owner: delsinm
      group: delsinm
      mode: 0644

become: True - run this as any user
owner: who will the owner be of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Change the file permissions of a file via playbook

A

From any client
touch /home/delsinm/linux2

vi filepermissions.yml
- name: Change file permissions
hosts: all
tasks:
- name: change file permissions
file:
path: /home/delsinm/linux2
mode: a+w (do this or octal)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Check file/directory status

A

vi checkFS.yml
- name FS checks
hosts: localhost
tasks:
- name: Check file status and attributes
stat:
path: /etc/hosts
register: fs

- name: Show result
  debug:
    msg: File attributes {{ fs }}

register - container to put output in, you can call is whatever you want

msg - This is just what will display on your output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create and remove a directory and add a file to it. Give it permissions and an owning user and group.
Stat the file - check if the file was created

A

vi crfile.yml
- name: Create and remove file
hosts: all
tasks:
- name: Create a directory
file:
path: /tmp/seinfeld
owner: delsinm
group: delsinm
mode: 0770
state: directory

 - name: create a file
    file: /tmp/seinfeld/jerry
    state: touch
 
 - name:  stat new file
   stat:
     path: /tmp /seinfeld/jerry
     registry: jf

 - name: Show file status
  debug: 
      msg: File status and attributes {{ jf }}

 - name: Remove file
  file: 
    path: /tmp/seinfeld/jerry
    state: absent
			
		- name: Remove directory
		  file: 
			  path: /tmp/seinfeld
				state: absent
			become: true

The msg here will display on the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Create a file and add text to it.

A

vi addtext.yml
- name: Create a file and add text
hosts: localhost
tasks:
- name: Create a file
file:
path: /home/delsinm/file1.txt
state: touch

  • name: Add text
    blockinfile:
    path: /home/delsinm/file1.txt
    block: George is on of the main Characters of the Seinfeld show and he is Jerry’s best friend.

If you want it indented the same way you type, do this

block: |
this that
and the
other

17
Q

Via playbook:
Install httpd
start httpd
open the port in firewall
restart firewalld

install the posix collection

A

ansible-galaxy collection install ansible.posix

  • name: setup httpd and open firewall port
    hosts: all
    tasks:
    • name: install httpd
      yum:
      name: httpd
      state: present
    • name: start httpd
      service:
      name: httpd
      state: started
    • name: open port 80
      firewalld:
      service: http
      permanent: True
      state: enabled
    • name: Restart firewalld
      service:
      name: firewalld
      state: reload

permanent - enabled after system reboot