summaryrefslogtreecommitdiff
path: root/ansible_stuff.org
diff options
context:
space:
mode:
authorboom2 <blizzack@blizzack.com>2023-12-08 14:12:33 -0500
committerboom2 <blizzack@blizzack.com>2023-12-08 14:12:33 -0500
commitd8a7729358a2fd3b911022e45d0197fda3e5da94 (patch)
treef75d69bcd7b08a9bebf325c037e4557c61a958e2 /ansible_stuff.org
parent2fd6a845dfe9ed6b3189c247928dc87d8f76d01a (diff)
- add exeriments for:
- roles - variables - inventory changes - config changes
Diffstat (limited to 'ansible_stuff.org')
-rw-r--r--ansible_stuff.org197
1 files changed, 189 insertions, 8 deletions
diff --git a/ansible_stuff.org b/ansible_stuff.org
index 99f00db..04c860f 100644
--- a/ansible_stuff.org
+++ b/ansible_stuff.org
@@ -504,10 +504,18 @@ https://www.lifewire.com/download-free-books-3482754
https://assets.digitalocean.com/books/how-to-manage-remote-servers-with-ansible.pdf
https://www.digitalocean.com/community/cheatsheets/how-to-use-ansible-cheat-sheet-guide
+https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html
To execute a command on a node, use the -a option followed by the
-command you want to run, in quotes.
+command you want to run, in quotes. some of these don't use a -m
+<module_name> bcuz they're using the default 'command' module. we'll
+want to be careful using the 'command' module cuz it DOES NOT SUPPORT
+extended shell syntaxes like piping and redirects (although shell
+variables will always work). If your command requires shell-specific
+syntax, use the shell module instead.
+
+https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html
some more examples of an adhoc commands that can be run:
@@ -523,6 +531,14 @@ tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
cgroup_root tmpfs 8.0M 0 8.0M 0% /sys/fs/cgroup
/dev/sda1 ext4 89M 82M 73K 100% /boot
+-- or --
+
+we could've run the above like so:
+
+ ansible all -m ansible.builtin.shell -a "df -Th"
+
+ https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html
+
dpierre@boom2:~$ ansible all -a "uptime"
@@ -531,10 +547,10 @@ dpierre@boom2:~$ ansible all -a "uptime"
- copying files that are owned by myself
-ansible all -m copy -a "src=./file.txt dest=~/myfile.txt"
+ansible all -m ansible.builtin.copy -a "src=./file.txt dest=~/myfile.txt"
- change file permissions
-ansible all -m file -a "dest=/var/www/file.txt mode=600 owner=sammy group=sammy" --become -K
+ansible all -m ansible.builtin.file -a "dest=/var/www/file.txt mode=600 owner=sammy group=sammy" --become -K
- invoking services
ansible all -a "/sbin/reboot" --become -K
@@ -558,10 +574,14 @@ https://www.digitalocean.com/community/cheatsheets/how-to-manage-multiple-server
we use the '-m' switch on ansible commands to reference a command via
the 'command module' to execute that command on the remote server(s).
-ansible all -m setup
-ansible all -m setup -a "gather_subset=min"
-ansible all -m setup -a " filter=*ipv* "
-ansible all -m setup > ../bbox-system.json
+ansible all -m ansible.builtin.setup
+ansible all -m ansible.builtin.setup -a "gather_subset=min"
+ansible all -m ansible.builtin.setup -a " filter=*ipv* "
+ansible all -m ansible.builtin.setup > ../bbox-system.json
+
+ansible localhost -m ansible.builtin.setup
+
+https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html
- to list out the tasks of a playbook:
@@ -601,5 +621,166 @@ playbook: 12-playbook-copy-directory.yml
and sometimes you want to limit the group or single server that
should be the target for that setup. You can use -l (limit) to set
up the target group or server in that play.
+ - REMEMBER... the 'hosts' w/in the playbook must be using a group
+ of some kind for this to work...or else you'll receive a message
+ like "skipping: no hosts matched"
+
+ ansible-playbook -l dev-stations 12-playbook-copy-directory.yml
+
+** <2023-11-30 Thu>
+
+researching 'roles' and how to use them.
+
+the issue i have now is that i made a simple playbook that copies
+files on to the host as expected...but i want to change where those
+files are placed depending on the host type. i think roles will help
+me w/ this.
+
+https://stackoverflow.com/questions/32101001/ansible-playbooks-vs-roles
+
+
+https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html
+- the above URL shows an example of using a conditional on which tasks
+ to run on a host depending on the host os. ansible grabs this info
+ by calling 'ansible_facts' (grabbing the 'setup') on the host.
+
+ - i grabbed the setup info on each host like so:
+
+ ansible localhost -m ansible.builtin.setup > ansible_setup_on_control_node.json
+ ansible dev -m ansible.builtin.setup > ansible_setup_on_dev_node.json
+
+ - the setup info looks very similar for both in that there's no real
+ way to discern a managed from a control host simply by reading the json
+ - there are differences in the ip address and machine name but not
+ much else
+
+- wtf are 'magic variables' (special variables)
+ https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables
+
+ maybe this is my answer to finding something that describes the
+ control vs managed node
+
+ ansible docs are not very good at explaining this stuff very well !!!
+
+ https://stackoverflow.com/questions/18839509/where-can-i-get-a-list-of-ansible-pre-defined-variables
+ - this is not a bad way of pulling these 'magic variables'
+
+ - these are what i need !!
+
+ ansible -m ansible.builtin.debug -a 'var=hostvars' localhost
+ ansible -m ansible.builtin.debug -a 'var=hostvars.localhost' localhost
+
+ - i'll get different values for different hosts w/ these
+ variables:
+
+ - via my one managed host i see:
+
+ "inventory_hostname": "192.168.0.220",
+ "inventory_hostname_short": "192.168.0.220",
+
+ - via my control host i see:
+
+ "inventory_hostname": "localhost",
+ "inventory_hostname_short": "localhost"
+
+ - so it appears i could use the above 'magic variables' in a
+ conditional statement
+
+- role
+ - bash_configs
+ - tasks
+ - main.yml
+ - localhost.yml
+ - other_hosts.yml
+
+** <2023-12-04 Mon>
+
+- experimented w/ using the 'inventory_hostname' above by using an
+ example from here:
+ https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html
+
+- grrr... ansible documentation from redhat is not really good if you
+ want a slight tutorial on how to work w/ roles. it's great at push
+ out facts about 'roles' or other constructs but putting it together
+ it a good example is quite terrible IMHO.
+
+- this is a great example of a simple role
+ https://github.com/cesarvr/ansible-role-hello-world
+ - the inet needs more simple examples like this !!
+
+** <2023-12-05 Tue>
+
+- How to Use Ansible Roles to Abstract your Infrastructure Environment
+ https://www.digitalocean.com/community/tutorials/how-to-use-ansible-roles-to-abstract-your-infrastructure-environment
+
+- i decided im gonna create my roles directory like so: '~/.ansible/roles/'
+ - [X] i need to update 'roles_path' in '~/.ansible.cfg' to point to
+ this 'roles' directory
+- [X] create 'bash_config' role
+
+- had all sorts of issues w/ including files in roles.
+
+ The vars_files key is a play directive. It defines a list of files to
+ read from to load variable data. These files are read and parsed at
+ the time the playbook itself is parsed. Just as with including tasks
+ and handlers, the path is relative to the file referencing the file.
+
+- this is kind of what i want to do...
+ https://serverfault.com/questions/589734/ansible-can-i-use-vars-files-when-some-files-do-not-exist
+ https://stackoverflow.com/questions/53253879/ansible-vars-files-vs-include-vars
+ https://stackoverflow.com/questions/36134552/use-multiple-var-files-in-ansible-role
+
+** <2023-12-07 Thu>
+
+- worked w/ 'var_files' and that did not do what i expected
+- playing w/ 'group_vars' directory
+ https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#id44
+ - this seemed to work nicely for me. i created
+ '06-playbook-print-group-vars.yml' as an exmple
+ - i had to create the following files inside of '/home/dpierre/.ansible'
+
+ ├── group_vars
+ │   └── all
+ ├── host_vars
+ │   └── localhost
+
+ - with the following guts:
+
+ # /home/dpierre/.ansible/group_vars/all
+ prefix_dir: ~/
+
+ # /home/dpierre/.ansible/host_vars/localhost
+ prefix_dir: ~/tmp/temp_files/
+
+ - i also added the following at the top '/home/dpierre/.ansible/host'
+
+ # /home/dpierre/.ansible/hosts
+ ...
+ localhost ansible_connection=local
+
+
+- looking again at this:
+ https://www.middlewareinventory.com/blog/run-ansible-playbook-locally/
+ - i think i just need to employ one of these strategies for running
+ a playbook locally and stop fighting friction
+ - i believe i'm more interested in methods 3 and 4
+ - another link w/ some examples:
+ https://nixzie.com/run-ansible-playbook-locally/#Run_Ansible_Playbook_Locally_Using_Local_Action
+- this lil argument on SO states that idiomatic roles in ansible
+ should not possess roles:
+ https://stackoverflow.com/questions/36134552/use-multiple-var-files-in-ansible-role
+ - so...this explains why trying to add different variablse for the
+ hosts in roles was painful...its bcuz its in the wrong place
+
+** <2023-12-08 Fri>
+
+- continued my tests of copying files and found the correct way is to
+ use hosts and/or group vars that can be kept in the playbook or w/
+ the 'hosts'/'inventory' file.
+
+- made the correct changes and now copied the files to localhost in
+ the correct place
+
+- make copy of ~/.ansible into repo for reference:
- ansible-playbook -l dev-stations 12-playbook-copy-directory.yml
+ rsync -avzhrP ~/.ansible* ~/repos/ansible_repo/home/