ansible pass shell command result to variable
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.
my idea was to do something like this:
- name: generate repli-id
shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
register: slave_repli
- debug: var=slave_repli.stdout_lines
- name: rewrite
template: src=templates/root.j2 dest=/root/test.conf
so I get the id generated
TASK [debug] *******************************************************************
task path: /Users/miwi/ansible/roles/test/main.yml:32
ok: [mysqls5slave] =>
"slave_repli.stdout_lines": [
"3698"
]
ok: [mysqls5master] =>
"slave_repli.stdout_lines": [
"3699"
]
my question is now how do I pass it over to my var file
slave_server_id: slave_server_id
ansible
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |Â
up vote
1
down vote
favorite
I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.
my idea was to do something like this:
- name: generate repli-id
shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
register: slave_repli
- debug: var=slave_repli.stdout_lines
- name: rewrite
template: src=templates/root.j2 dest=/root/test.conf
so I get the id generated
TASK [debug] *******************************************************************
task path: /Users/miwi/ansible/roles/test/main.yml:32
ok: [mysqls5slave] =>
"slave_repli.stdout_lines": [
"3698"
]
ok: [mysqls5master] =>
"slave_repli.stdout_lines": [
"3699"
]
my question is now how do I pass it over to my var file
slave_server_id: slave_server_id
ansible
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.
my idea was to do something like this:
- name: generate repli-id
shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
register: slave_repli
- debug: var=slave_repli.stdout_lines
- name: rewrite
template: src=templates/root.j2 dest=/root/test.conf
so I get the id generated
TASK [debug] *******************************************************************
task path: /Users/miwi/ansible/roles/test/main.yml:32
ok: [mysqls5slave] =>
"slave_repli.stdout_lines": [
"3698"
]
ok: [mysqls5master] =>
"slave_repli.stdout_lines": [
"3699"
]
my question is now how do I pass it over to my var file
slave_server_id: slave_server_id
ansible
I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.
my idea was to do something like this:
- name: generate repli-id
shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
register: slave_repli
- debug: var=slave_repli.stdout_lines
- name: rewrite
template: src=templates/root.j2 dest=/root/test.conf
so I get the id generated
TASK [debug] *******************************************************************
task path: /Users/miwi/ansible/roles/test/main.yml:32
ok: [mysqls5slave] =>
"slave_repli.stdout_lines": [
"3698"
]
ok: [mysqls5master] =>
"slave_repli.stdout_lines": [
"3699"
]
my question is now how do I pass it over to my var file
slave_server_id: slave_server_id
ansible
ansible
edited Oct 17 '16 at 10:10
Jeff Schaller
33.8k851113
33.8k851113
asked Oct 17 '16 at 9:56
Andre Lange
612
612
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:
mkdir -p /etc/ansible/facts.d
cat <<EOF >/etc/ansible/facts.d/mysql.fact
#!/bin/sh
echo "[mysql]"
echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
EOF
chmod +x /etc/ansible/facts.d/mysql.fact
Run ansible -m setup your.mysql.ip.address
to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id']
defined.
Say you'ld want ansible to deploy this fact, you may do something like:
- name: install fact
copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
register: fact_installed
- name: reload facts
setup: filter=ansible_local
when: fact_installed is defined and fact_installed.changed == True
After which, you may include your template, referring to your server_id
variable.
add a comment |Â
up vote
0
down vote
You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:
- local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file
add a comment |Â
up vote
0
down vote
You have two options to use the generated value in your template:
Option 1: Use slave_repli.stdout_lines
in your template:
# in templates/root.j2:
...
slave_server_id: slave_repli.stdout_lines[0]
...
Option 2: Assign a variable
In your playbook:
- name: rewrite
vars:
slave_server_id: " slave_repli.stdout_lines[0] "
template:
src: templates/root.j2
dest: /root/test.conf
In your template:
# in templates/root.j2:
...
slave_server_id: slave_server_id
...
I prefer the second approach as it's much cleaner.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:
mkdir -p /etc/ansible/facts.d
cat <<EOF >/etc/ansible/facts.d/mysql.fact
#!/bin/sh
echo "[mysql]"
echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
EOF
chmod +x /etc/ansible/facts.d/mysql.fact
Run ansible -m setup your.mysql.ip.address
to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id']
defined.
Say you'ld want ansible to deploy this fact, you may do something like:
- name: install fact
copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
register: fact_installed
- name: reload facts
setup: filter=ansible_local
when: fact_installed is defined and fact_installed.changed == True
After which, you may include your template, referring to your server_id
variable.
add a comment |Â
up vote
0
down vote
Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:
mkdir -p /etc/ansible/facts.d
cat <<EOF >/etc/ansible/facts.d/mysql.fact
#!/bin/sh
echo "[mysql]"
echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
EOF
chmod +x /etc/ansible/facts.d/mysql.fact
Run ansible -m setup your.mysql.ip.address
to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id']
defined.
Say you'ld want ansible to deploy this fact, you may do something like:
- name: install fact
copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
register: fact_installed
- name: reload facts
setup: filter=ansible_local
when: fact_installed is defined and fact_installed.changed == True
After which, you may include your template, referring to your server_id
variable.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:
mkdir -p /etc/ansible/facts.d
cat <<EOF >/etc/ansible/facts.d/mysql.fact
#!/bin/sh
echo "[mysql]"
echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
EOF
chmod +x /etc/ansible/facts.d/mysql.fact
Run ansible -m setup your.mysql.ip.address
to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id']
defined.
Say you'ld want ansible to deploy this fact, you may do something like:
- name: install fact
copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
register: fact_installed
- name: reload facts
setup: filter=ansible_local
when: fact_installed is defined and fact_installed.changed == True
After which, you may include your template, referring to your server_id
variable.
Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:
mkdir -p /etc/ansible/facts.d
cat <<EOF >/etc/ansible/facts.d/mysql.fact
#!/bin/sh
echo "[mysql]"
echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
EOF
chmod +x /etc/ansible/facts.d/mysql.fact
Run ansible -m setup your.mysql.ip.address
to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id']
defined.
Say you'ld want ansible to deploy this fact, you may do something like:
- name: install fact
copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
register: fact_installed
- name: reload facts
setup: filter=ansible_local
when: fact_installed is defined and fact_installed.changed == True
After which, you may include your template, referring to your server_id
variable.
answered Nov 17 '16 at 1:29
SYN
1,814414
1,814414
add a comment |Â
add a comment |Â
up vote
0
down vote
You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:
- local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file
add a comment |Â
up vote
0
down vote
You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:
- local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:
- local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file
You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:
- local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file
answered Apr 5 at 5:50
Pedreiro
84
84
add a comment |Â
add a comment |Â
up vote
0
down vote
You have two options to use the generated value in your template:
Option 1: Use slave_repli.stdout_lines
in your template:
# in templates/root.j2:
...
slave_server_id: slave_repli.stdout_lines[0]
...
Option 2: Assign a variable
In your playbook:
- name: rewrite
vars:
slave_server_id: " slave_repli.stdout_lines[0] "
template:
src: templates/root.j2
dest: /root/test.conf
In your template:
# in templates/root.j2:
...
slave_server_id: slave_server_id
...
I prefer the second approach as it's much cleaner.
add a comment |Â
up vote
0
down vote
You have two options to use the generated value in your template:
Option 1: Use slave_repli.stdout_lines
in your template:
# in templates/root.j2:
...
slave_server_id: slave_repli.stdout_lines[0]
...
Option 2: Assign a variable
In your playbook:
- name: rewrite
vars:
slave_server_id: " slave_repli.stdout_lines[0] "
template:
src: templates/root.j2
dest: /root/test.conf
In your template:
# in templates/root.j2:
...
slave_server_id: slave_server_id
...
I prefer the second approach as it's much cleaner.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You have two options to use the generated value in your template:
Option 1: Use slave_repli.stdout_lines
in your template:
# in templates/root.j2:
...
slave_server_id: slave_repli.stdout_lines[0]
...
Option 2: Assign a variable
In your playbook:
- name: rewrite
vars:
slave_server_id: " slave_repli.stdout_lines[0] "
template:
src: templates/root.j2
dest: /root/test.conf
In your template:
# in templates/root.j2:
...
slave_server_id: slave_server_id
...
I prefer the second approach as it's much cleaner.
You have two options to use the generated value in your template:
Option 1: Use slave_repli.stdout_lines
in your template:
# in templates/root.j2:
...
slave_server_id: slave_repli.stdout_lines[0]
...
Option 2: Assign a variable
In your playbook:
- name: rewrite
vars:
slave_server_id: " slave_repli.stdout_lines[0] "
template:
src: templates/root.j2
dest: /root/test.conf
In your template:
# in templates/root.j2:
...
slave_server_id: slave_server_id
...
I prefer the second approach as it's much cleaner.
answered Sep 11 at 16:22
mhutter
557210
557210
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f316927%2fansible-pass-shell-command-result-to-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password