[英]Boto3 Cli: How to pass the variable content in USERDATA to php script?
I need to substitute the server name in the user data with the RDS ENDPOINT. 我需要用RDS ENDPOINT替换用户数据中的服务器名称。
I can get the RDS endpoint but not sure how to substitute that in the php file properly. 我可以得到RDS端点,但不确定如何在php文件中正确替换它。
Here is how I get the RDS endpoint: 这是我获取RDS端点的方法:
instances = source.describe_db_instances(DBInstanceIdentifier=db_instance)
rds_host = instances.get('DBInstances')[0].get('Endpoint').get('Address')
Another way is also: 另一种方法是:
RDS=aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"
Next I need to pass the RDS variable content to the php script shown below: 接下来,我需要将RDS变量内容传递给如下所示的php脚本:
echo "define('DB_SERVER', $RDS);" >> /var/www/html/dbinfo.php
Here is what I have. 这就是我所拥有的。
UserData="""#!/bin/bash
RDS=aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"
echo $RDS > /var/www/html/test.php
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
cd /var/www/html
echo "<?php phpinfo(); ?>" > /var/www/html/hello.php
echo "<?php " >/var/www/html/dbinfo.php
echo "define('DB_SERVER', $RDS);" >> /var/www/html/dbinfo.php
echo "define('DB_USERNAME', 'username');" >> /var/www/html/dbinfo.php
echo "define('DB_PASSWORD', 'pass');" >> /var/www/html/dbinfo.php
echo "define('DB_DATABASE', 'dbname');" >> /var/www/html/dbinfo.php
echo "\$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);">> /var/www/html/dbinfo.php
echo "if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();">> /var/www/html/dbinfo.php
echo "Successfully connected to the RDS instance; $rds_host" >> /var/www/html/dbinfo.php
echo "?>" >> dbinfo.php
"""
Basically, I need to get the content of rds_host and echo it to the line in the php script where $rds_host is shown. 基本上,我需要获取rds_host的内容并将其回显到显示$ rds_host的php脚本中的行。
Also tried the following: 还尝试了以下方法:
UserData="""#!/bin/bash
yum update –y
yum install httpd php mysql php-mysql git -y
service httpd start
chkconfig httpd on
groupadd www
usermod -a -G www ec2-user
chown -R root:www /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} +
find /var/www -type f -exec chmod 0664 {} +
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
cd /var/www/html
echo "<?php phpinfo(); ?>" > /var/www/html/hello.php
$rds_host='aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"'
echo "<?php " >/var/www/html/dbinfo.php
echo "define('DB_SERVER', $rds_host);" >> /var/www/html/dbinfo.php
echo "define('DB_USERNAME', 'username');" >> /var/www/html/dbinfo.php
echo "define('DB_PASSWORD', 'dbpass');" >> /var/www/html/dbinfo.php
echo "define('DB_DATABASE', 'dbname');" >> /var/www/html/dbinfo.php
echo "\$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);">> /var/www/html/dbinfo.php
echo "if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();">> /var/www/html/dbinfo.php
echo "Successfully connected to the RDS instance; $rds_host" >> /var/www/html/dbinfo.php
echo "?>" >> dbinfo.php
"""
) )
and checking the cloud-init-output.log shows: 并检查cloud-init-output.log显示:
/var/lib/cloud/instance/scripts/part-001: line 17: =aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address": command not found
Cloud-init v. 0.7.6 finished at Mon, 07 May 2018 23:36:15 +0000. Cloud-init v.0.7.6已于2018年5月7日星期一23:36:15 +0000完成。 Datasource DataSourceEc2. 数据源DataSourceEc2。 Up 15.83 seconds 最多15.83秒
Try something like: 尝试类似:
#!/bin/bash
yum update –y
yum install -y httpd24 php70 mysql56-server php70-mysqlnd
.....
rds_host=$(aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address")
echo "<?php " > /var/www/html/dbinfo.php
echo "define('DB_SERVER', $rds_host);" >> /var/www/html/dbinfo.php
etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.