I have a simple playbook:
- name: "Ensure /etc/host contains '127.0.0.1'"
lineinfile:
name: /etc/host
line: "127.0.0.1"
state: present
check_mode: yes
register: conf
- name: "Use java role playbook"
roles:
- role: java
if_exist: conf
My question: Will this conf variable be visible and assigned to if_exist?
First you can use shell module to read the file and register the output, then use "when" to search for a specific content in the registered output of the file and base of the result run the task.
- name: cat
shell: "cat /etc/host "
register: conf
- include_role:
name:
when: conf.stdout.find('127.0.0.1') != 1
It's possible even without cat/grep/awk, just use Ansible's lookup and search.
vars:
out: "{{ lookup('file', '/etc/host') }}"
tasks:
- name: "Use java role playbook"
include_role:
name: Java
when: out is search("127.0.0.1")
Related
I am trying to preserve a command output from one dynamic host group to another one but get a variable is undefined error in my ansible playbook.
My playbook is dynamically getting the primary and secondary hosts by running a command. I want to use the output that I get from the secondary host on the primary with a when statement. Here's what I have.
- hosts: all
serial: 1
tasks:
- name: run curl command to determine primary/secondary
command: /tmp/status.sh
register: apis_op
- name: run curl command to determine primary/secondary
debug: var=apis_op.stdout_lines
- name: If node is primary add to primary
add_host:
name: "{{ ansible_hostname }}"
groups: temp_primary
when: apis_op.stdout == "primary"
- name: run curl command to determine primary/secondary
debug: var=groups
- name: If node is secondary add to secondary
add_host:
name: "{{ ansible_hostname }}"
groups: temp_secondary
when: apis_op.stdout == "secondary"
- name: run curl command to determine primary/secondary
debug: var=groups
- hosts: temp_secondary
tasks:
- name: Run schell script
command: /tmp/runthis.sh
become: yes
become_user: root
register: op
- hosts: temp_primary
tasks:
- name: Run schell script
command: /tmp/testthis.sh
become: yes
become_user: root
when: hostvars['temp_secondary']['op'] == "{false}"
When I run this I get a hostvars['temp_secondary'] is undefined error. Can I not use hostvars with temporary/dynamic host groups?
here is my _main.yml _
---
- name: run show version on switch
cli_command:
command: show version
register: show
- name: print command output
debug: var=show.stdout_lines[0]
- name: Shutdown TOR-A port
cli_command:
command: "{{ item }}"
register: command_output
with_items:
- "config"
- "interface Tengigabitethernet 0/35"
- "shutdown"
- name: Debug command output
debug:
msg: "{{ command_output }}"
How do I access port 36,7,8, and so on on the same switch and issue shutdown on mentioned switch ports in a sequence?
I have to repeat the same activity for another switch TOR-B , the task should be identical.However , I am not sure how to dynamically achieve the objective in one yaml file.Any help will be appreciated!
The switch credentials are saved in hosts file
The following should basically do the job (Note: I do not have any cli compatible switch to test against).
Create a shutdown_ports.yml file with the following content. This file can be at the same level of you playbook, or inside your tasks folder in role if you include it from there.
---
- name: "shutdown port"
cli_command:
command: "{{ item }}"
with_items:
- "interface Tengigabitethernet 0/{{ port }}"
- "shutdown"
Use that file as an include in a loop like in the following playbook example
---
- name: shutdown same ports on all switches
hosts: group_containing_my_switches
vars:
shutdown_ports:
- 35
- 36
- 7
- 8
tasks:
- name: switch to config mode
cli_command:
command: config
- name: shutdown all referenced ports
include_tasks: shutdown_ports.yml
loop: "{{ shutdown_ports }}"
loop_control:
loop_var: port
An other possible solution is to dynamically create an array containing all command to send and play all those in a loop. Example playbook again
---
- name: shutdown same ports on all switches
hosts: group_containing_my_switches
vars:
shutdown_ports:
- 35
- 36
- 7
- 8
tasks:
- name: Add config mode as first command
set_fact:
cli_switch_commands:
- config
- name: Add commands for each port shutdown
vars:
current_port_command: "interface Tengigabitethernet 0/{{ item }}"
set_fact:
cli_switch_commands: "{{ cli_switch_commands + [current_port_command, 'shutdown'] }}"
loop: "{{ shutdown_ports }}"
- name: Send all commands to switch
cli_command:
command: "{{ item }}"
loop: "{{ cli_switch_commands }}"
I am very confused, in the play book where actually we put single hyphen (-). I found similar threads here, but still confused, so decided to draft one new. I have read it will be used to indicate start of a list item. again i have difficulty in understanding where is the start of list and where is start of dictionary.
Can some experts explain me where should i put hyphen in below code. and why is that?
---
connection: local
gather_facts: false
hosts: rtr
tasks:
name: "read configs"
read_csv:
path: "{{ aws_config }}"
register: aws_requests
run_once: true
debug:
msg: "{{ aws_requests.list }}"
name: "display awsconfigs requests"
run_once: true
name: "set awsconfigs requests"
run_once: true
set_fact:
aws_configs: "{{ aws_requests.list }}"
name: "build template"
template:
dest: "{{ config_filename }}"
lstrip_blocks: true
src: "{{ template }}"
I recommend that you read the "Intro to playbooks", which should answer your questions, but below is a summary.
As you noted correctly, hyphens are list items in YAML. YAML documents start with ---, which is why there are hyphens at the start of the file.
The starting point for any Ansible playbook is the playbook itself in the file. The playbook file itself may contain one or more so-called "plays", each as its own list element. Each play typically contains a hosts and a tasks part. In many playbooks, there is just one "play", so your typical minimal playbook looks like this:
---
- hosts: webservers
tasks:
- name: Task 1
...
As you can see above, each "play" then has a list of tasks, each starting with a hyphen. So in the following example there are two tasks, each with a name and the module (yum and service in this case):
---
- hosts: webservers
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: ensure apache is running
service:
name: httpd
state: started
Each Ansible module has different arguments, so you'll need to check the modules documentation for each one how to specify these arguments.
So the correct version for your playbook above would look like this:
---
- connection: local
gather_facts: false
hosts: rtr
tasks:
- name: "read configs"
read_csv:
path: "{{ aws_config }}"
register: aws_requests
run_once: true
- name: "display awsconfigs requests"
debug:
msg: "{{ aws_requests.list }}"
run_once: true
- name: "set awsconfigs requests"
run_once: true
set_fact:
aws_configs: "{{ aws_requests.list }}"
- name: "build template"
template:
dest: "{{ config_filename }}"
lstrip_blocks: true
src: "{{ template }}"
I have this code in my playbook:
- hosts: standby
remote_user: root
tasks:
- name: replace hostname in config
replace:
path: /opt/agentd.conf
regexp: #\s+Hostname\=
replace: Hostname={{hname}}
backup: yes
- name: add database array in files
lineinfile:
path: /opt/zabbix_agent/share/scripts/{{ item }}
line: 'DBNAME_ARRAY=( {{dbname}} )'
insertafter: DB2PATH=/home/db2inst1/sqllib/bin/db2
backup: yes
with_items:
- Connections
- HadrAndLog
- Memory
- Regular
- name: restart service
shell: /etc/init.d/agent restart
register: command_output
become: yes
become_user: root
tags: restart
- debug: msg="{{command_output.stdout_lines}}"
tags: set_config_st
it will replace # Hostname= in a config file with Hostname= givenhostname and add an array in 4 scripts. array is the name of given database. then it will restart the agent to apply the changes.
when i run this command:
ansible-playbook -i /Ansible/inventory/hostfile /Ansible/provision/nconf.yml --tags set_config_st --extra-vars "hname=fazi dbname=fazidb"
i get this error:
first argument must be string or compiled pattern
i searched a bit but couldn't find the reason. what should i do?
The problem is in this line:
regexp: #\s+Hostname\=
You have to quote the regex because YAML comments start with #, so everything after the # will be ignored by ansible and that is why the error message occures.
So the correct line should be:
regexp: '#\s+Hostname\='
or
regexp: "#\s+Hostname\="
I think the problem is with indention. Please try as below.
- hosts: standby
remote_user: root
tasks:
- name: replace hostname in config
replace:
path: /opt/agentd.conf
regexp: #\s+Hostname\=
replace: Hostname={{hname}}
backup: yes
- name: add database array in files
lineinfile:
path: /opt/zabbix_agent/share/scripts/{{ item }}
line: 'DBNAME_ARRAY=( {{dbname}} )'
insertafter: DB2PATH=/home/db2inst1/sqllib/bin/db2
backup: yes
with_items:
- Connections
- HadrAndLog
- Memory
- Regular
- name: restart service
shell: /etc/init.d/agent restart
register: command_output
become: yes
become_user: root
tags: restart
- debug: msg="{{command_output.stdout_lines}}"
tags: set_config_st
I have a playbook in the format below:
---
- hosts: myIP
tasks:
- name: Install a yum package in Ansible example
yum:
name: ThePackageIWantToInstall
state: present
where myIP and ThePackageIWantToInstall are variables.
When the job template runs, I would like the user in the extra variables popup to be able to go with:
myIP = 192.168.1.1
ThePackageIWantToInstall = nano
As the documentation doesn't provide an example of supplying a variable via a job template, is this possible?
Yes.
- name: Do The Thing
hosts: "{{ foo }}"
roles:
- "{{ role }}"
Need mustaches and quotes.
to run from popup
(I don't use this, but it was suggested as an edit, thanks...)
foo: value
I have achieved similar thing with add_hosts. Here iam not installing package but creating file with name passed from command line. Any number of hosts (separated by commas can be passed from command line).
# cat addhost2.yml
- hosts: localhost
gather_facts: no
tasks:
- add_host:
name: "{{ item }}"
groups: hosts_from_commandline
with_items: "{{ new_hosts_passed.split(',') }}"
- hosts: hosts_from_commandline
tasks:
- name: Ansible create file with name passed from commandline
file:
path: "/tmp/{{ filename_from_commandline }}"
state: touch
# ansible-playbook -i hosts addhost2.yml --extra-vars='new_hosts_passed=192.168.3.104,192.168.3.113 filename_from_commandline=lathamdkv'
Hope this helps