A Bit of Everything 1 Flashcards
Install Python on managed nodes
ansible -u root -i inventory ansible3 –ask-pass -m raw -a ‘yum install python3’
{Look up ansible modules for ping
what is the url for module info?
show documentation for ping
locate the file for the ping module and view it
show how to use parameters in a playbook - basically just use a command that just shows how to do things with the module.
ansible-doc -l | grep ping
docs.ansible.com
ansible-doc ping
grab the file location and go there
ansible-doc -s ping
FOR URL
quick links (On Right)
Ansible Package Docs Home or choose modules and plugins index}
install a list of items
httpd
vsftpd
nmap
- name: using lists
hosts: all
tasks:- name: Install Packages
yum:
name:- httpd
- vsftpd
- nmap
state: lateset
- name: Install Packages
What are the two ways you can use to create a multiline screen
”| >”
>
Setup httpd and then test if it’s working
- name: Install and Start HTTPD
hosts: ansible2
tasks:- name: Install HTTPD
yum:
name: httpd
state: latest - name: Start HTTPD
service:
name: httpd
state: started
enabled: yes - name: Open port 80
firewalld:
service: http
state: enabled
permanent: True - name: Restart Firewalld
service:
name: firewalld
state: restarted
- name: Install HTTPD
- name: Test HTTPD accessability
hosts: localhost
tasks:- name:
uri:
url: http://ansible2
- name:
…
How would you print your ansible configuration?
How would you show only the configuration parts that are being implemented? If there is something that doesn’t show up here there might be a typo.
How do you show all the collections you currently have?
How do you install a collection
ansible-config view
ansible-config dump –only-changed
ansible-galaxy collection list
ansible-galaxy collection install this.collection
Show facts for ansible2
Show facts
ansible ansible2 -m setup
Via Ansible facts show
hostname
distribution
ipv4
network interfaces
storage devices
size of /dev/sda1
version distribution
ansible_facts[‘hostname’]
ansible_facts[‘distribution’]
ansible_facts[‘default_ipv4’][‘address’]
ansible_facts[‘intefaces’]
ansible_facts[‘devices’]
ansible_facts[‘devices’][‘sda][‘partitions’][‘sda1’]
ansible_facts[‘distribution_version’]
Create a playbook that checks if the memory of the managed node is about 50 megs
What form of measurement is disk space measured in?
debug:
msg: ‘test’
when: ansible_facts[‘memory_mb’][‘real’][‘free’] > 50
disks are measured in bytes
Create a playbook that says ‘using CentOS 8.1’ if the distribution is 8.1 and the distribution is centos
debug:
msg: ‘using CentOS 8.8’
when: ansible_facts[‘distribution_version’] == ‘8.1’ and ansible_facts[‘distribution’] == ‘CentOS’
Create Custom Facts and store them on the managed hosts
This should give variables for web packages, ftp packages and then their services
Show how you would call that information in a playbook
Show Custom Facts
/etc/ansible/facts.d
[packages]
web_package = httpd
ftp_package = vsftpd
[services]
web_service = httpd
ftp_service = vsftpd
create a playbook to store them, file should end with .fact
The file should be called listing68.fact and stored in /etc/ansible/facts.d
{{ ansible_facts[‘ansible_local’][‘listing68’][‘packages’][‘web_package’] }}
ansible all -m setup -a ‘filter=ansible_local’
Create Custom Facts in under a group called software.
The facts should be for a package, service, state, and enabled = True.
Check if you can see the facts
Install a package Using all these custom facts
Use a playbook to copy the file over
vi custom.facts
[packages]
package = httpd
service = httpd
state = started
enabled = true
ansible all -m setup -a ‘filter=ansible_local’
vi install.yml
- name: install
hosts: all
tasks:
- name install
dnf:
name: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘package’] }}”
state: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘state’] }}”
- name: Start Service
service:
name: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘service’] }}”
state: {{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘state’] }}
enabled: {{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘enabled’] }}
Create a variable file, it should be in it’s own directory.
Use that variable file to install a package
mkdir /vars/
vi /vars/common
my_package: nmap
my_ftp_service: vsftpd
my_file_service: smb
- name: Install
hosts: ansible2
vars_files: vars/common
tasks:- name: Install
dnf:
name: “{{ my_package }}”
state: latest
- name: Install
create variables for ansible2
create variables for nodes
In project folder:
mkdir host_vars
vim host_vars/ansible2
package: httpd
mkdir group_vars
vim group_vars/nodes
package: vsftpd
What do you call lists and dictionaries in Ansible
array - list
dictionary - hash
Create a variable file called users-dictionary
One dictionary should be named linda and should contain a username, shell, and home directory..
Call these all in a playbook and print it to stdout
mkdir /vars
cd /vars
vi users-dictionary
users:
linda:
username: linda
homedir: /home/linda
shell: /bin/bash
lisa:
username: lisa
homedir: /home/linda
shell: /bin/bash
Name 5 important Magic Variables
hostvars - all hosts in inventory and their assigned variables
groups - All groups in inventory
group_names - List groups this host is currently a member of
inventory_hostname - Specifies inventory hostname of current host
inventory_file - Name of current inventory file that is used.
ansible localhost -m debug -a “var=hostvars[‘ansible1’]”
Create a password file
use the password in it to create a playbook name secret.yml
run the playbook
run the playbook with you manually typing in the password
touch /root/.passfile
chmod 600 /root/passfile
ansible-vault –vault-password-file=/root/.passfile secret.yml
ansible-playbook –vault-password-file=/root/.passfile secret.yml
ansible-playbook –ask-vault-pass secret.yml
ansible-playbook –vault-id @prompt
Via Ansible Vault, encrypt, decrypt, change password and edit a file
ansible-vault encrypt this.yml
ansible-vault decrypt this.yml
ansible-vault rekey this.yml
ansible-vault edit this.yml
What are vault-ids?
use them in a playbook by creating two variable files with vault-ids, give them two separate password files and run the playbook
How do you manually enter passwords for all the encrypted files that have vault-ids?
cd vars/
ansible-vault create common_one –vault-id sercret1@/path/to/passfile
package: httpd (contents of common_one)
ansible-vault create common_two –vault-id sercret2@/path/to/passfile
cd ../
vi test.yml
- name: test
hosts: all
var_files:- vars/common_one
- vars/common_two
ansible-playbook test.yml \
– vault-id sercret1@/path/to/passfile
– vault-id sercret2@/path/to/passfile
TO MANUALLY ENTER PASSWORDS AS THEY COME IN
ansible-playbook test.yml –vault-id @prompt
What are the 6 register keys?
cmd - the command that was used
rc - return code
stderr - error message generated by the command
stderr_lines - error messages shown by line
stdout - command output
stdout_lines
When would you use loops vs a list?
list - you can use this for a module like yum because it will go through the list:
yum:
name:
- this
- that
You would use a loop with service: since it can’t iterate through your list
Loop through a list to start services for httpd and nmap
- name: Service
hosts: ansible2
tasks:- name: Service
service:
name: “{{ item }}”
state: started
loop:- httpd
- firewalld
- name: Service
Create a variable called ‘services’ and start the services listed in it via a loop
- name: Service
hosts: ansible2
vars:
services:
- httpd
- firewalld
tasks:- name: Service
service:
name: “{{ item }}”
state: started
loop: “{{ services }}”
- name: Service