Most Missed Flashcards
(60 cards)
On all nodes create a directory for ansible’s sudo configuration. Allow it to not have to use a password
How is ansible tower different here?
vi /etc/sudoers.d/ansible
ansible ALL=(ALL) NOPASSWD:ALL
Ansible Tower allows you to store ansible’s password securely so it can use sudo
MOD SHOULD BE 0440
validate: /usr/sbin/visudo -cf %s
In your inventory, create servers in two groups, then combine the two groups into a group named server
Create 16 servers (1-16) that are ungrouped
Force one of your servers to use 192.168.10.1 everytime
[web]
ansible1 ansible_host=192.168.10.1
[db]
ansible2
[servers:children]
web
db
server[1:16]@example.com
Show hosts in an inventory called inventory
Show all hosts in the inventory file
ansible-inventory -i /inventory –list-hosts
–list will do so in JSON format
ansible -i inventory all –list
Create the ansible.cfg file
All of this can be used per playbook
[defaults]
remote_user = ansible
host_key_checking = false
inventory = inventory
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[defaults] - generic info
[privilege_escalation] - How ansible user should require admin privileges to connect to managed hosts
remote_user - user used to connect to managed device
host_key_checking - Should ssh host keys be checked
Show all hosts in this inventory
Show all hosts that aren’t a part of a group
Show a hierarchical overview of the inventory
Show the contents in json format
ansible -i inventory all –list-hosts
ansible -i inventory ungrouped –list-hosts
ansible-inventory -i inventory –graph
ansible-inventory -i inventory –list
Say you don’t have python installed on a managed node. How would you install python?
ansible -u root -i inventory ansible3 –ask-pass -m raw -a ‘yum install python3’
Using variables, create a user named ‘Lisa’ the name of the task should contain the variable as well as an ANSIBLE FACT that shows the hostname of the machine you’re running the playbook on.
What are ansible facts?
- name: Add Users
hosts: ansible1
vars:
users: lisa
tasks:- name: Create user {{ users }} on host {{ ansible_hostname }}
user:
name: “{{ users }}”
- name: Create user {{ users }} on host {{ ansible_hostname }}
…
FACTS - automatically set variables.
Create Cutsom Facts in under a group called software.
The facs 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 variables for ansible2
create variables for nodes
REMEMBER: You do not have to specify the file, ansible will know what variables to grab based off of the hosts key.
In project folder:
mkdir host_vars
vim host_vars/ansible2
package: httpd
mkdir group_vars
vim group_vars/nodes
package: vsftpd
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
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?
Vault-ids are given to multiple files you want to use with different passwords in your playbook.
The vauld-ids, let ansible know what password file corresponds to the encrypted file you specify.
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
Delete your LV, VG and partition as well as the fstab entry for it
Create it all again
Example in System
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
Create a file with multivalued variables. You should have a variable named ‘users’ and the should contain three items of linda lisa and anna. These will have usernames, homedirectories and shells for the users. Import the variable file and loop through user creation for the users.
users:
- username: linda
homedir: /home/linda
shell: /bin/bash
- username: lisa
homedir: /home/lisa
shell: /bin/bash - username: anna
homedir: /home/linda
shell: /bin/bash
- name: Create Users
hosts: ansible2
vars_files:- vars/user-dictionary.yml
tasks: - name: Create Users
user:
name: “{{ item[‘username’] }}”
home: “{{ item[‘homedir’] }}”
shell: “{{ item[‘shell’] }}”
loop: “{{ users }}”
Here we can see that users is what it’s looping through, so it’s looping through the list of dictionaries. First it goes to the first list item and grabs the username, homedir, and shell, next it goes back to the top and goes through the second item in the list, etc.
- vars/user-dictionary.yml
What are the main conditionals you can test?
variable is defined - if the var exists
variable is not defined - if the variable doesn’t exist
ansible_distribution in distributions - first variable is present in list mentioned as second
EXAMPLE:
when: ansible_os_family in [‘Debian’, ‘RedHat’, ‘Suse’]
variable - variabe is true, 1, or yes
not variable - variable is false, 0, or no
key == ‘value’
key > ‘value’
key <= ‘value’
key > ‘value’
key >= ‘value’
key != value
Create a playbook, have it ask the user a question on what package to install and store it in a variable.
Create a variable with a list of packages.
If the defined package doesn’t exist in the list, let the user know
- name: Testing with the IN statement
hosts: all
vars_prompt:- name: my_answer
prompt: Which package do you want to install?
vars:
supported_packages:- httpd
- nmap
gather_facts: false
tasks:
- name: Something
debug:
msg: ‘You are trying to install a supported package’
when: my_answer in supported_packages
- name: my_answer
Make all handlers run before an error popped up
force_handlers
Stop playbook when a failed task occurs
any_erros_fatal
Create a playbook that prints a fail message when the word ‘word’ is found in an echo command but continues going
- name: Update the kernel
hosts: all
register_errors: yes
tasks:- name: Print
command: echo hello world
ignore_errors: true
register: command_result - name: Error
fail:
msg: Command has failed
when: “‘world’ in command_result.stdout” - name: See if we get here
debug:
msg: second task executed
- name: Print
What directive can we used to make sure that the output of a command never comes out as ‘changed’ only ‘ok’ and ‘failed’
changed_when: false
REMEMBER BLOCKS CAN’T LOOP
Create a block of tasks
First it will remove a file
If there are any issues with this task failing, create a file in /tmp called ‘rescuefile’ and allow the playbook to complete.
Next, regardless of success or failure of the first task, have a task run make a log message.
There should be a message noting everything that’s happening in the playbook.
- name: using blocks
hosts: all
tasks:- name: Intended to be successful block
block:- name: remove file
shell:
cmd: rm /var/www/html/index.html - name: print status
debug:
msg: ‘block was operated’ - name: create a file
rescue:- name: create a file
shell:
cmd: touch /tmp/rescuefile - name: print rescue status
debug:
msg: ‘Rescue complete’
always:- name: log message
shell:
cmd: logger hello
- name: log message
- name: always print this message
debug:
msg: logger update
- name: create a file
- name: remove file
- name: Intended to be successful block
Register a files stats and make a condition based on one of the pieces of info. If it is not met, force the playbook to fail
- stat:
path: /tmp/temporary
register: fs - debug:
msg: “{{ fs }}”
Register a files stats and make a condition based on one of the pieces of info. If it is not met, force the playbook to fail
- command: touch /tmp/statfile
- name stat file
stat:
path: /tmp/statfile
register: fs - fail:
msg: ‘unexpected file mode’
when: st.stat.mode != ‘0640’
Create a file named /tmp/hosts and add the below lines to it:
192.168.4.110 host1.example.com
192.168.4.110 host1.example.com
- name: Add Hosts hosts: all tasks: - name: Create file file: path: /tmp/hosts state: touch - name: Add junk blockinfile: path: /tmp/hosts block: | 192.168.4.110 host1.example.com 192.168.4.110 host1.example.com state: present